Treehunter has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a very cheap and lame SDL game in perl mainly just out of being bored during certain periods of the day. I stubled across a problem and that is I can't seem to make it where the charater moves while the key is held down and stops moving when the key is released. I have tried various things so the code I'm attaching to this might have extra stuff that isn't exactly required.
use strict; use SDL; use SDL::App; use SDL::Rect; use SDL::Surface; use SDL::Color; use SDL::Event; my $hold = 0; my $key; my $right = 0; my $left = 0; my $up = 0; my $down = 0; my $x = 10; my $y; my $key2; my $gangsta = SDL::Surface->new( -name => 'gangsta.gif', ); my $ghetto = SDL::Surface->new( -name => 'ghetto.jpg', ); my $app = SDL::App->new( -height => $ghetto->height(), -width => $ghetto->width(), -depth => 16, -title => "China Man Shooter 0.2 (Gang Fights)", ); my $bg_rect = SDL::Rect->new( -height => $ghetto->height(), -width => $ghetto->width(), -x => 0, -y => 0, ); my $bg = SDL::Rect->new( -height => $app->height(), -width => $app->width(), -x => 0, -y => 0, ); my $gangsta_rect = SDL::Rect->new( -height => $gangsta->height(), -width => $gangsta->width(), -x => 0, -y => 0, ); my $gang = SDL::Rect->new( -height => $gangsta_rect->height(), -width => $gangsta_rect->width(), -x => 10, -y => 100, ); refresh(); my $event = new SDL::Event(); my %events = ( SDL_QUIT() => sub { exit(0); }, SDL_KEYDOWN() => sub { $event = shift; $key = $event->key_name(); current(); }, SDL_KEYUP() => sub { $event = shift; $key2 = $event->key_name(); }, ); $app->loop( \%events ); sub current { while ($hold == 0) { my $current_x = $gang->x(); my $current_y = $gang->y(); if ($key eq 'right') { for my $m ($current_x..($current_x + 5)) { $gang->x( $m ); refresh(); } } if ($key eq 'left') { for my $m ($current_x..($current_x - 5)) { $gang->x( $m ); refresh(); #I have no idea what im doing here %events->SDL_KEYUP(); if ($key eq $key2) { $hold = 1; } else { $hold = 0; } } } } } sub refresh { $ghetto->blit($bg_rect, $app, $bg); $gangsta->blit($gangsta_rect, $app, $gang); $app->update($bg, $gang); }
Basically I figured that In current I would run a while statment and some how check SDL_KEYUP to see when it would be equal to the previously pressed key but that hasn't worked. Any ideas?

Thanks for the Help its all works great now

Replies are listed 'Best First'.
Re: SDL Events hold key down
by chromatic (Archbishop) on Feb 07, 2005 at 00:31 UTC

    I've done this before by giving each sprite a velocity. For each appropriate key down event, add the appropriate values to the appropriate velocity components. For each appropriate key up event, add the opposite values. Then in your drawing routine, update the sprite's position based on its velocity and redraw it.

Re: SDL Events hold key down
by zentara (Cardinal) on Feb 07, 2005 at 13:12 UTC
    I think you are looking at it from the wrong perspective. You could do what you are doing by starting a timer, which runs a loop, when a key is pressed, then stop the loop when the key is released, but that is alot of trouble.

    I would just set a certain amount of "motion ( dx or dy )" to each key press, and set up a "repeat interval for the keys"). So when you hold the key down, you will get a succesion of events, which stop when the key is released.

    Here is a super simple example just to give you the idea. Instead of printing the repeating key code, just assign your motion.

    #!/usr/bin/perl use SDL::App; use SDL::Event; use SDL; ### Create A new Application window my $app = new SDL::App -width => 400, -height => 400; ### A simple event handler hash my $events = { SDL_KEYDOWN() => sub { my ($e) = @_; exit(0) if ( $e->key_sym() == SDLK_ESCAPE ); print $e->key_sym(),"\n"; }, SDL_QUIT() => sub { exit(); }, }; my $eventkr = new SDL::Event; $eventkr->set_key_repeat( 10, 10); ### Loop & process the events $app->loop($events); #$app->loop(); #no events, must kill to exit

    I'm not really a human, but I play one on earth. flash japh