10 # 'rect' => SDL::Game::Rect->new(20, 20, 10, 10),
13 'color' => SDL::Color->new(-r => 0x00, -g => 0xcc, -b => 0x00),
27 my $app = SDL::App->new(
34 my $event = SDL::Event->new;
37 my $player = SDL::Rect->new( -top => 200, -left => 30, -w => 20, -h => 90);
38 my $fg_color = SDL::Color->new( -r => 0xcc, -g => 0xcc, -b => 0xcc );
43 while ($event->poll) {
44 my $type = $event->type;
45 exit if $type == SDL_QUIT;
47 if ($type == SDL_KEYDOWN && $event->key_name eq 'down') {
48 $player->y($player->y + 2);
55 $app->fill($player, $fg_color);
57 # if I uncomment this line, the window buttons go away!!! WTF???
58 $app->update($player);
65 This tutorial is intended to help you build your very own version of the Pong game and/or variations of it, using SDL Perl.
67 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.
69 =head2 Part 1: We start with a Rect
71 In Pong, the player controls a rectangle that moves up and down, so creating the rectangle looks like a good place to start:
73 my $player = SDL::Game::Rect->new({
80 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.
82 Wait. Did I say... I<<screen>>?
84 =head2 Part 0: "The Screen"
86 In SDL Perl, creating a window screen is very easy and straightforward:
91 my $app = SDL::App->new(
92 -title => 'Pong', # set window title
93 -width => 640, # window width
94 -height => 480, # window height
97 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.
99 =head3 Creating an (empty) event loop
101 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.
103 For this simple game we don't need a very sofisticated event loop, so let's create a simple one.
105 event_loop() while 1;
107 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:
112 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,
114 =head2 Part 1 (cont.) - Drawing our Rect on the screen
118 =head2 Part 2 - Our first event: tracking user movement
122 Now let's query some events!
124 First, we need to use the L<< SDL::Event >> module. Add this to the beginning of our code:
127 my $event = SDL::Event->new;
130 Now let's rewrite the C<< event_loop >> subroutine to take advantage of our event object. The new subroutine should look like this:
133 # first we poll if an event occurred...
134 while ($event->poll) {
136 # if there is an event, we check its type
137 my $type = $event->type
139 # handle window closing
140 exit if $type == SDL_QUIT;
147 =head3 Hey, don't move away from the court! Our first collision detection.
149 =head2 Part 3 - Enter "the Ball"
153 =head3 Some vetorial background
157 =head2 Part 4 - Collision Detection
161 =head2 Part 5 - Our hero's nemesis appears
165 =head3 (really) basic IA
169 =head2 Part 6 - Counting (and showing) the score