Retro Gaming Hacks, Part 3: Add a Ball and Score to Pong

by Josh Glover, contributor to Retro Gaming Hacks
01/05/2006

Now that we have moving paddles for our Pong clone, the only thing standing in the way of having some real fun is the fact that the ball does not in fact move. But this is a problem easily solved once you rediscover some basic geometry.

The first geometric law we will require is: slope equals rise over run. The ball is going to move in a straight line, so all we have to do to track its movement is, for every iteration of the main loop, change the y coordinate by the rise (or numerator) of the slope, and the x coordinate by the run (or denominator). The fact that the slope, a fractional sort of number, can be easily represented by two components is a big win for our integer-loving CPU. You can start by defining two more macros at the top of the sdl-pong.c file, where all of the macros live:

``````#define BALL_SPEED   6 // total change in x and y in the slope
#define SLOPE_MAX_DY 4 // the maximum change-in-y allowed in the slope``````

The macros will make a bit more sense once we add another structure definition (right above the `GameData` structure in the code):

``````// Structure definitions
typedef struct {

int dx;
int dy;

} Slope;``````

and a few new members to the `GameData` structure itself:

``````  int game_speed;   // game speed
int ball_speed;   // number of pixels the ball can move at once
int slope_max_dy; // maximum value for change-in-y component of the slope

SDL_Rect ball;  // ball
Slope    slope; // slope of the line representing the ball's path``````

Just as the game uses the `GameData` structure to hold a collection of related data, so will it use the `Slope` structure to keep track of all of the data necessary to keep track of the ball's movement: namely the change-in-x (run) and change-in-y (rise) components of the slope of the line along which the ball is currently moving. As always, you will need to initialize the new members of the `GameData` structure:

``````  // Initialise game data
game.running      = 1;
game.game_speed   = GAME_SPEED;
game.ball_speed   = BALL_SPEED;
game.slope_max_dy = SLOPE_MAX_DY;``````

You may be wondering if I forgot to initialize the `Slope`. Actually, I didn't; I just cannot do it in such a straightforward fashion because I want to introduce the one ingredient that makes a good game great: the element of chance. Why not randomly generate the starting `Slope` (within reason, of course)? You need to include two new headers at the top of the file:

``````// Standard library headers
#include <stdio.h>
#include <stdlib.h>
#include <time.h>``````

`stdlib.h` provides the `srand()` and `rand()` functions, and `time.h` provides the eponymous `time()` function, which you will need in the new `genSlope()` function. Add the definition of `genSlope()` between `cleanUp()` and `movePaddle()`, like so:

``void genSlope( GameData *game );``

and add the implementation between that of the same functions:

``````/* Function: genSlope()
*
* Randomly generates the slope of the vector of ball's travel.
*
* Parameters:
*
*   *game - game data
*/

void genSlope( GameData *game ) {

// Seed the random number generator with the current Unix timestamp
srand( time( NULL ) );

// Generate the change-in-y component of the slope randomly
game->slope.dy =
1 + (int)((float)game->slope_max_dy * rand() / (RAND_MAX + 1.0));

// The change-in-y component of the slope is
// whatever is left in the "budget"
game->slope.dx = game->ball_speed - game->slope.dy;

// Flip a coin for x and y directions
if ((int)(2.0 * rand() / (RAND_MAX + 1.0)))
game->slope.dx *= -1;
if ((int)(2.0 * rand() / (RAND_MAX + 1.0)))
game->slope.dy *= -1;

} // genSlope()``````
 Related Reading Retro Gaming Hacks Tips & Tools for Playing the Classics By Chris Kohler
 Pages: 1, 2, 3, 4, 5, 6

Linux Online Certification

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!