Writing games in Perl - Part 2 - Controlling the Ball

| No Comments | No TrackBacks

Following the
first post
on the subject of writing games in Perl, where we
created a bouncing ball (I know, it is a rectangle, but I trust your
imagination), this post is going to add something very important when
dealing with games: input.

Silveira Neto suggested that
I should include more specific instructions on how to start the game
(and maybe a video), so I recalled that I didn't mention that all the
sources for this posts (including the text) is currently hosted at a
github repository (if
you plan to contribute, please just ask me for commit permissions
instead of forking the repo).

So if you want to run the codes posted here, you first need to:

$ git clone http://github.com/ruoso/games-perl.git

You can check for updates by calling

$ git pull origin master

from inside the games-perl directory. Each directory inside
games-perl starts with the number of the post. The first post is
inside the 1-bouncing-ball directory and the second is in
2-controlling. To run the the first code just get inside the
first directory and call:

$ perl ball.pl

The second example code is based on the first, so the script name
is the same, so just get into the other directory and run the same
line. If you get an error like:

Can't locate SDL/Video.pm in @INC (@INC contains: /etc/perl
/usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/perl5
/usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10
/usr/local/lib/site_perl .) at ball.pl line 8.

It means you probably don't have the newest SDL, take a look at
the first
to see how to get the newest redesigned SDL.

Controlling the Ball

Enough for the introduction, let's get to the actual code. The
first thing we need is understanding SDL Events. If you ever
programmed GUI applications or even if you wrote some javascript you
are aware of how an event framework looks like. SDL is no exception,
you need to wait (or poll) for the events, and each event will contain
the information you need to figure out what happened.

In our case, we want to apply additional acceleration to the ball
whenever the arrow keys are pressed. But if we have an event-based
system, the way to figure out which of those four keys is currently
pressed is keeping a state mask and update it when you receive keydown
and keyup events.

So what we're going to do is to manipulate the acc_h
and acc_v ball attributes depending on the keydown and keyup
events. It might look complicated, but the only change we need is
(this is inside ball.pl main loop):

while (SDL::Events::poll_event($event)) {
if ($event->type == SDL_QUIT) {

} elsif ($type == SDL_KEYDOWN &&
$sevent->key_sym() == SDLK_LEFT) {

} elsif ($type == SDL_KEYUP &&
$sevent->key_sym() == SDLK_LEFT) {

} elsif ($type == SDL_KEYDOWN &&
$sevent->key_sym() == SDLK_RIGHT) {

} elsif ($type == SDL_KEYUP &&
$sevent->key_sym() == SDLK_RIGHT) {

} elsif ($type == SDL_KEYDOWN &&
$sevent->key_sym() == SDLK_UP) {

} elsif ($type == SDL_KEYUP &&
$sevent->key_sym() == SDLK_UP) {

} elsif ($type == SDL_KEYDOWN &&
$sevent->key_sym() == SDLK_DOWN) {

} elsif ($type == SDL_KEYUP &&
$sevent->key_sym() == SDLK_DOWN) {


So, this is it. Follows a small video of the game.

No TrackBacks

TrackBack URL: http://daniel.ruoso.com/cgi-bin/mt/mt-tb.cgi/164

Leave a comment

About this Entry

This page contains a single entry by Daniel Ruoso published on February 7, 2010 11:30 AM.

Writing games in Perl - part 1 - Bouncing Ball was the previous entry in this blog.

Google AI Challenge in Perl is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.