Skip to content

Instantly share code, notes, and snippets.

@rblackwe
Forked from kthakore/zelda_mapper.pl
Created July 17, 2010 03:07
Show Gist options
  • Save rblackwe/479202 to your computer and use it in GitHub Desktop.
Save rblackwe/479202 to your computer and use it in GitHub Desktop.
=pod
Get the game.png from http://i.imgur.com/FxgMM.png
=head1 INSTALL
For windows get strawberry perl
Then do:
cpan pip Alien::SDL
pip http://sdlperl.ath.cx/releases/SDL-2.502.tar.gz
=cut
use strict;
use warnings;
use SDL::Event;
use SDL::Events;
use SDLx::Sprite;
use SDLx::Surface;
use SDLx::Controller;
my $background = SDLx::Sprite->new( image => 'game.png' );
$background->{cell} = { x => 160, y => 128 };
my $display = SDLx::Surface::get_display(
width => $background->{cell}->{x},
height => $background->{cell}->{y}
);
my $app = SDLx::Controller->new();
my $x = 0;
my $y = $background->{cell}->{y} - $background->h;
$background->{offset} =
[ $background->{cell}->{x} - $background->w , $y ];
sub on_event {
my $e = shift;
return 0 if $e->type == SDL_QUIT;
return 0 if $e->key_sym == SDLK_ESCAPE;
if ( $e->type == SDL_KEYDOWN ) {
$x += $background->{cell}->{x} if $e->key_sym == SDLK_LEFT;
$x -= $background->{cell}->{x} if $e->key_sym == SDLK_RIGHT;
$y -= $background->{cell}->{y} if $e->key_sym == SDLK_DOWN;
$y += $background->{cell}->{y} if $e->key_sym == SDLK_UP;
$y = $background->{offset}->[1] if $y < $background->{offset}->[1];
$x = $background->{offset}->[0] if $x < $background->{offset}->[0];
$y = 0 if $y > 0;
$x = 0 if $x > 0;
}
return 1;
}
sub on_show {
my $dt = shift;
$background->draw_xy( $display, $x, $y );
$display->update();
return 0;
}
$app->add_event_handler( \&on_event );
$app->add_show_handler( \&on_show );
$app->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment