LinuxDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


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

Of course, if the ball goes off of the left or right side of the screen without colliding with a paddle, a point has been scored. For now, all SDL Pong does in response to such a joyous occasion is reset the sprites (this time asking resetSprites() to erase the sprites from their current location, by virtue of setting the second parameter to a true value), randomly generate a new slope for the ball, then return.



The last thing that moveBall() must do, if a point is not scored, is to draw the new location of the ball sprite.

Run gcc again to recompile it:

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

and then run the game (see Figure 1):

./sdl-pong

Figure 1
Figure 1. Pong, with moving ball

One Last Score, and then I'm Out

This section addresses the vexing lack of scoring in SDL Pong. So let's open a whole new can of worms: SDL_TTF, which is an extra SDL library that deals with TrueType fonts--that is what the "TTF" means. To open the can, include the string.h and SDL_ttf.h headers at the top of sdl-pong.c:

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

// SDL headers
#include <SDL.h>
#include <SDL_ttf.h>

Add some new macros, as well:

#define GAME_POINTS 10 // number of points to win the game

#define MSG_FONT "/usr/share/fonts/TTF/Vera.ttf" // font for messages
#define MSG_SIZE 18                              // font size
#define MSG_TIME 1500    // display duration of messages

GAME_POINTS is self-explanatory; the other three less so. Basically, we are going to use the TrueType font defined by the MSG_FONT macro to display some messages in MSG_SIZE-point type, and will display the messages for MSG_TIME milliseconds.

Note: depending on the whims of your Linux distribution or Unix flavor's package system, your TrueType fonts may not reside in the /usr/share/fonts/TTF directory, and you may not have the Vera.ttf font on your system. That is OK; there is nothing magical about /usr/share/fonts/TTF, and any TrueType font that you have on your system will work just fine. To find your TrueType fonts, either grep your X11 configuration file (most likely /etc/X11/XF86Config or /etc/X11/xorg.conf) for "FontPath", or run: find / -name \*.ttf.

Now, you will need a new structure definition:

// Structure definitions
typedef struct {

  int p1;
  int p2;

  int game_points;
  
} Score;

And while you are at it, add two new members to the GameData structure:

  Slope    slope; // slope of the line representing the ball's path
  Score    score; // score of the game

  TTF_Font *font; // message font

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.java.net
  • Linux Kernel Archives
  • Kernel Traffic
  • DistroWatch.com


  • Sponsored by: