10 'rect' => SDL::Game::Rect->new(20, 20, 10, 10),
13 'color' => SDL::Color->new(-r => 0x00, -g => 0xcc, -b => 0x00),
28 my $app = SDL::App->new(
35 my $event = SDL::Event->new;
38 my $bg_color = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0x00 );
39 my $back = SDL::Rect->new( 0, 0, $app->width, $app->height);
40 my $player = SDL::Rect->new( 100, 30, 20, 90);
41 my $fg_color = SDL::Color->new( -r => 0xcc, -g => 0xcc, -b => 0xcc );
47 my $held_down_key = undef;
48 while ($event->poll || defined $held_down_key) {
49 my $type = $event->type;
50 exit if $type == SDL_QUIT;
52 if ($type == SDL_KEYDOWN || defined $held_down_key) {
53 if($event->key_name eq 'down' || $held_down_key eq 'down')
55 $player->y($player->y + 2) ;
56 $held_down_key = 'down'
58 if($event->key_name eq 'up' || $held_down_key eq 'up')
60 $player->y($player->y - 2) ;
65 if ($type == SDL_KEYUP)
67 $held_down_key = undef;
77 $app->fill($back, $bg_color);
78 $app->fill($player, $fg_color);
80 # if I uncomment this line, the window buttons go away!!! WTF???
89 This tutorial is intended to help you build your very own version of the Pong game and/or variations of it, using SDL Perl.
91 Just in case you live under a rock, Pong is one of the earliest arcade games, a true classic by Atari Inc. The game has two simple rectangles scrolling up and down trying to hit a (square) ball that bounces around, and could be thought of as a table tennis simulation.
93 =head2 Part 1: We start with a Rect
95 In Pong, the player controls a rectangle that moves up and down, so creating the rectangle looks like a good place to start:
97 my $player = SDL::Game::Rect->new({
104 That creates a new L<< SDL::Game::Rect >> object, a rectangle, with the given width/height dimensions and in the given top/left position of the screen.
106 Wait. Did I say... I<<screen>>?
108 =head2 Part 0: "The Screen"
110 In SDL Perl, creating a window screen is very easy and straightforward:
115 my $app = SDL::App->new(
116 -title => 'Pong', # set window title
117 -width => 640, # window width
118 -height => 480, # window height
121 That's it. If you run this code, you'll see a window appear and disappear almost instantly. Why doesn't it stay up? Well, the code is processed linearly, like usual programs are, and with no hidden magic. So, you basically said "create a window" and then the program ended - destroying the window. In order to keep it up and running, listening for events, you need an event loop.
123 =head3 Creating an (empty) event loop
125 An event loop is a simple infinite loop that captures events (like a key pressed or released from the keyboard, mouse movement, etc) and either does something about it or dispatches it to any object that might.
127 For this simple game we don't need a very sofisticated event loop, so let's create a simple one.
129 event_loop() while 1;
131 Yay, an infinite loop! Now we are free to define our very own event loop any way we want. Let's make it an empty sub for starters:
136 Ok. If you run it, you'll see your C<< $app >> window displayed until you force to shutdown the program by typing C<< Ctrl-C >> or something. Other than that, our event loop doesn't do anything,
138 =head2 Part 1 (cont.) - Drawing our Rect on the screen
142 =head2 Part 2 - Our first event: tracking user movement
146 Now let's query some events!
148 First, we need to use the L<< SDL::Event >> module. Add this to the beginning of our code:
151 my $event = SDL::Event->new;
154 Now let's rewrite the C<< event_loop >> subroutine to take advantage of our event object. The new subroutine should look like this:
157 # first we poll if an event occurred...
158 while ($event->poll) {
160 # if there is an event, we check its type
161 my $type = $event->type
163 # handle window closing
164 exit if $type == SDL_QUIT;
171 =head3 Hey, don't move away from the court! Our first collision detection.
173 =head2 Part 3 - Enter "the Ball"
177 =head3 Some vetorial background
181 =head2 Part 4 - Collision Detection
185 =head2 Part 5 - Our hero's nemesis appears
189 =head3 (really) basic IA
193 =head2 Part 6 - Counting (and showing) the score