oreilly.comSafari Books Online.Conferences.


Retro Gaming Hacks, Part 2: Add Paddles to Pong
Pages: 1, 2, 3, 4, 5, 6

We will use the keyboard as our input device, which should work out quite nicely for a two-player game with only two controls per player. For Player 1's "up" and "down" controls, we pick the A and Z keys, and for Player 2, the ' and / keys. The only criteria asserted here is that the "up" key should be right above the "down" one, and that the controls for Player 1 and Player 2 be at opposite sides of the keyboard. Let's declare a new variable, *keystate, which will hold the keyboard state:

  // Declare variables
  GameData   game;     // game data
  SDL_Event  event;    // SDL events
  Uint8     *keystate; // keyboard state

We will query the keyboard state using the SDL function SDL_GetKeyState(), right after handling events in the main loop:

    // If we have been told to exit, do so now
    if (game.running == 0)

    // Move sprites about

    // Grab a keystate snapshot
    keystate = SDL_GetKeyState( NULL );

SDL_GetKeyState() returns an array, with a true value at each element that corresponds to a key that is currently being pressed. We will use the same SDLK_* macros that we used earlier to handle keyboard events, but this time as indexes into the *keystate array. Right below the call to SDL_GetKeyState(), add:

    // Has player 1 requested a move?
    if (keystate[SDLK_a])
      movePaddle( &game, 1, DIR_UP );
    else if (keystate[SDLK_z])
      movePaddle( &game, 1, DIR_DOWN );

    // Has player 2 requested a move?
    if (keystate[SDLK_QUOTE])
      movePaddle( &game, 2, DIR_UP );
    else if (keystate[SDLK_SLASH])
      movePaddle( &game, 2, DIR_DOWN );

See how reasonably the macros are named? And if you cannot guess one, you can always refer to the SDLKey documentation.

The only thing left to do is to define and implement the elusive movePaddle() function. In the function definitions section of your code, add:

// Function definitions
int  cleanUp( int err );
void movePaddle( GameData *game, int player, int dir );
void resetSprites( GameData *game, int erase );

And just before the implementation of the resetSprites() function, add:

/* Function: movePaddle()
 * Moves a player's paddle.
 * Parameters:
 *   *game   - game data
 *    player - player number
 *    dir    - direction of the move (DIR_UP or DIR_DOWN)

void movePaddle( GameData *game, int player, int dir ) {

  int new_y;
  int moved;

  SDL_Rect  tmp;
  SDL_Rect *rect = (player == 1 ? &(game->p1) : &(game->p2));

  int speed = (player == 1 ? game->p1_speed : game->p2_speed);
  // Compute the new y coordinate of the rectangle and the pixels moved
  new_y = (dir == DIR_UP ? (rect->y - speed) : (rect->y + speed));
  moved = (dir == DIR_UP ? (rect->y - new_y) : (new_y - rect->y));

  // If the move would take us off the top or bottom of the screen,
  // we may have to move less than speed
  if (dir == DIR_UP && new_y < 0) {

    new_y = 0;
    moved = rect->y - new_y;
  } // if (moving up less than speed)
  else if (dir == DIR_DOWN && new_y > SCREEN_HEIGHT - rect->h) {

    new_y = SCREEN_HEIGHT - rect->h;
    moved = new_y - rect->y;
  } // else if (moving down less than speed)

  // If we have not moved, just return
  if (moved == 0)
  // Erase the top or bottom line(s) of the paddle
  tmp.x = rect->x;
  tmp.y = (dir == DIR_UP ? (rect->y + rect->h - moved) : rect->y );
  tmp.w = rect->w;
  tmp.h = moved;

  SDL_FillRect( game->screen, &tmp, game->black );
  game->rects[game->num_rects++] = tmp;

  // Apply the new y coordinate of the rectangle
  rect->y = new_y;

  // Draw the new bottom or top line(s) of the paddle
  tmp.y = (dir == DIR_UP ? rect->y : (rect->y + rect->h - moved) );
  SDL_FillRect( game->screen, &tmp, game->white );
  game->rects[game->num_rects++] = tmp;

} // movePaddle()

movePaddle() takes three parameters: the ubiquitous GameData structure, the number of the player that is moving (i.e., 1 or 2), and the direction of movement (either DIR_UP or DIR_DOWN). The first thing we do in the function is grab the SDL_Rect structure and speed corresponding to the player that has moved, and assign them to local variables, as shown in this excerpt from movePaddle():

  SDL_Rect *rect = (player == 1 ? &(game->p1) : &(game->p2));

  int speed = (player == 1 ? game->p1_speed : game->p2_speed);

I make use here of C's ternary operator, which can be expressed as:

<condition> ? <true_expression> : <false_expression>

I use it just so that I can accomplish in one line what would take several using a standard if/else control structure:

  SDL_Rect *rect;
  if (player == 1)
    rect = &(game->p1);
    rect = &(game->p2);

If you ever get confused by a ternary operator, simply "unroll" it as above: the bit after the ? would come after the if, and the bit after the : would come after the else.

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow

Linux Online Certification

Linux/Unix System Administration Certificate Series
Linux/Unix System Administration Certificate Series — This course series targets both beginning and intermediate Linux/Unix users who want to acquire advanced system administration skills, and to back those skills up with a Certificate from the University of Illinois Office of Continuing Education.

Enroll today!

Linux Resources
  • Linux Online
  • The Linux FAQ
  • Linux Kernel Archives
  • Kernel Traffic

  • Sponsored by: