The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

SDL::Tutorial::Pong -- Get started pong

PONG TUTORIAL

This tutorial is intended to help you build your very own version of the Pong game and/or variations of it, using SDL Perl.

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.

CATEGORY

Tutorials

Part 1: We start with a Rect

In Pong, the player controls a rectangle that moves up and down, so creating the rectangle looks like a good place to start:

   my $player = SDL::Game::Rect->new({
                       -top    => 10,
                       -left   => 20,
                       -width  => 6,
                       -height => 32,
                });

That creates a new SDL::Game::Rect object, a rectangle, with the given width/height dimensions and in the given top/left position of the screen.

Wait. Did I say... <screen>?

Part 0: "The Screen"

In SDL Perl, creating a window screen is very easy and straightforward:

  use SDL;
  use SDL::App;

  my $app = SDL::App->new(
                 -title  => 'Pong',  # set window title
                 -width  => 640,     # window width
                 -height => 480,     # window height
          );

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.

Creating an (empty) event loop

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.

For this simple game we don't need a very sofisticated event loop, so let's create a simple one.

  event_loop() while 1;

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:

  sub event_loop {
  }

Ok. If you run it, you'll see your $app window displayed until you force to shutdown the program by typing Ctrl-C or something. Other than that, our event loop doesn't do anything,

Part 1 (cont.) - Drawing our Rect on the screen

# TODO

Part 2 - Our first event: tracking user movement

# TODO

Now let's query some events!

First, we need to use the SDL::Event module. Add this to the beginning of our code:

  use SDL::Event;
  my $event = SDL::Event->new;

Now let's rewrite the event_loop subroutine to take advantage of our event object. The new subroutine should look like this:

  sub event_loop {
      # first we poll if an event occurred...
      while ($event->poll) {

          # if there is an event, we check its type
          my $type = $event->type

          # handle window closing
          exit if $type == SDL_QUIT;
      }
  }

#TODO

Hey, don't move away from the court! Our first collision detection.

Part 3 - Enter "the Ball"

#TODO

Some vetorial background

#TODO

Part 4 - Collision Detection

#TODO

Part 5 - Our hero's nemesis appears

#TODO

(really) basic IA

#TODO

Part 6 - Counting (and showing) the score

#TODO