LinuxDevCenter.com

oreilly.comSafari Books Online.Conferences.

We've expanded our Linux news coverage and improved our search! Search for all things Linux across O'Reilly!

Search
Search Tips

advertisement

Listen Print Discuss Subscribe to Linux Subscribe to Newsletters

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 libSDL_ttf.so 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:

./sdl-pong

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).

Resources

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:

  • SDL Project home page

  • SDL library documentation

  • The Game Programming Wiki's SDL Tutorials

  • SDL Perl: Perl bindings for SDL

  • PyGame: Python bindings for SDL

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


Return to the Linux DevCenter.


Ready to hack the hack? Send your ideas in here.
You must be logged in to the O'Reilly Network to post a talkback.
Post Comment
Full Threads Oldest First

Showing messages 1 through 1 of 1.

  • Thanks for the lesson!
    2009-03-10 18:02:42  Dev_Brakiri [Reply | View]

    I followed you through all three parts, and am glad I did. This was as much a lesson in structured C-programming as in using SDL. (By the way, thanks for the hint to use sdl-config, that really saved my build process on FreeBSD 7.1). The "final" program looks a lot like some of the kernel modules I've browsed through, wishing they were written in Python. Now maybe I can understand them too.

    Did you ever see the ball bounce into the end of a paddle, then bounce in and out down the length of it? It looks like a zipper getting unzipped until it's done, until you move the paddle again. Probably a side-effect of the surface destruction you mentioned already.


Tagged Articles

Be the first to post this article to del.icio.us

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
O'Reilly FYI
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com