Skip to content

Instantly share code, notes, and snippets.

@djanatyn
Created August 8, 2011 04:40
Show Gist options
  • Save djanatyn/1131219 to your computer and use it in GitHub Desktop.
Save djanatyn/1131219 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl -w
use strict;
use warnings;
use SDL;
use SDLx::App;
use SDL::Event;
use SDL::Events;
my $app = SDLx::App->new(width => 600,
height => 480);
my $event = SDL::Event->new();
my $player_x = 20;
my $player_y = 20;
my $velocity_x = 0;
my $velocity_y = 0;
my $quit = 0;
sub pressKey {
my $key = SDL::Events::get_key_name($event->key_sym);
if ($key eq 'right') { $velocity_x = 1; }
if ($key eq 'left') { $velocity_x = -1; }
if ($key eq 'space') { print "not implemented yet"; }
if ($key eq 'q') { $quit = 1; }
}
sub releaseKey {
my $key = SDL::Events::get_key_name($event->key_sym);
if ($key eq 'right' || $key eq 'left') { $velocity_x = 0; }
}
sub grabEvents {
SDL::Events::pump_events();
while ( SDL::Events::poll_event($event) ) {
pressKey if $event->type == SDL_KEYDOWN;
releaseKey if $event->type == SDL_KEYUP;
}
}
sub updatePositions {
$player_x += $velocity_x;
$player_y += $velocity_y;
}
sub renderArt {
$app->draw_rect( [0,0,640,480], [0,0,0,255] );
$app->draw_rect( [$player_x,$player_y,5,5], [255,255,255,255] );
$app->update;
}
while (!$quit) {
grabEvents;
updatePositions;
renderArt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment