oreilly.comSafari Books Online.Conferences.


Retro Gaming Hacks, Part 3: Add a Ball and Score to Pong
Pages: 1, 2, 3, 4, 5, 6

The first thing to do is to add a point to the score of the player who just propelled the ball off of the other player's side of the screen. Then, write the three message strings using the standard C function snprintf(), provided by the string.h header file (see the PRINTF(3) manpage for details on how to use snprintf() if it is new to you). Now, call SDL_TTF's TTF_RenderText_Solid() function to turn these strings into graphics, draw those graphics onto the main screen using SDL_BlitSurface(), then use that old standby, SDL_UpdateRects(), to make sure the changes to the main screen are displayed to the user.

After all of this excitement, wait for MSG_TIME milliseconds to make sure the players have time to read the new score, and then erase the messages by filling their rectangles with the background color and calling SDL_UpdateRects() again.

Now, it is possible that the point that was just score was the game-winner. If so, we go through the string-printing, text-rendering, surface-blitting, rects-updating dance again, but this time, instead of saying "Player X scores!" say "Player X wins!" After this, wait twice the normal MSG_TIME to allow for the elaborate cursing that will surely ensue, and then set the running member of the GameData structure to a false value (C's only false value being 0, of course), and finally return. This, if you remember the organization of the main loop, will cause control to break out of the loop, and fall through to the end of the main() function, which cleans up and exits.

One more time, run gcc again to recompile your sdl-pong.c file, this time adding -lSDL_ttf to the command-line arguments so that the shared library gets linked in:

gcc -g -Wall -I/usr/include/SDL -o sdl-pong sdl-pong.c \
  -lSDL -lSDL_ttf

and then run the game:


Now you should have SDL Pong in all its glory, as shown in Figure 2.

Figure 1
Figure 2. Pong, with a scoring system

Hacking the Hack

Though it pains me to admit it, SDL Pong is not a perfect game. There is plenty of room for improvement, and I hope that some readers of this hack who are interested in playing around with SDL will pick up where I've left off.

If you're interested, here is a list, in no particular order, of suggested improvements. Use them as a jumping-off point, or not at all.

  • Allow the user to set the various bits of configuration data that exist in the GameData structure by passing arguments on the command line.

  • The collision detection algorithm I've used for figuring out when the ball has hit the paddle is not so great, as evidenced by the damage done to the paddle by the ball. This is simply because the ball moves between one and five pixels at a time in the x direction, and the paddles are five pixels wide, and happen to present an edge to the ball that starts at an x coordinate that is not usually divisible by the change-in-x component of the slope. Add math until it works properly.

  • This is related to collision detection: fix the bug that allows the ball to bounce off of the inside of a paddle, if it hits the top or bottom edge of the paddle just right.

  • Monochrome is so '70s. Colorize Pong.

  • Using flood-filled rectangles for graphics is also so '70s. Investigate SDL_image and make Pong use PNG, JPG, or GIF graphics. Better yet, let the user select the paddle and ball graphics, and scale them to the appropriate size dynamically.

  • My physics model is simplistic and boring. Add "english" to the ball depending on which direction, and maybe even how fast, the paddle is moving when it hits the ball.

  • Any game with power-ups and specials is better. Add effects like exploding balls, Hyper Power Zero Shots, time stopping, and so on. Better yet, make these power-ups obtainable between rounds by trading in "style points" or some such scoring invention.

  • Sound: SDL_mixer will let you provide an Ogg Vorbis soundtrack to the game, so you can add popping sound effects when the paddle strikes the ball. And maybe some crowd noise when a point is scored.

  • Add network play using SDL_net. Make sure to come up with some way to find games. For a challenge, encapsulate your network protocol in HTTP (it might take a week to play a point, but it would be cool).


If you're looking for more information to help you become a cross-platform, game-programming wizard, be sure to check out some of the following resources:

Josh Glover has been hacking code for as long as anyone can remember. He is employed as a Unix systems administrator by

Return to the Linux DevCenter.

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: