LinuxDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


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

Run gcc again to re-compile it:



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

and then run it (see Figure 1):

./sdl-pong

Figure 1
Figure 1. Paddle and ball sprites

You have drawn the sprites on the screen, and our little Pong clone is starting to look like an actual game--except for the fact that there is no actual gameplay as of yet. Let's remedy that by making the paddles movable.

You need to start by defining some concept of speed: how far should a paddle be able to move in one iteration of the main loop? Let's add some new macro definitions below the others in the sdl-pong.c file:

// Default paddle speed for players
#define P1_SPEED 3
#define P2_SPEED 3

// See the dir parameter to movePaddle()
#define DIR_UP   1
#define DIR_DOWN 2

// Number of milliseconds the game will sleep at the end of the main loop;
// the higher the number, the slower the game speed. Note that you will
// probably want to change BALL_SPEED, P1_SPEED, and P2_SPEED when you change
// this.
#define GAME_SPEED 30

GAME_SPEED is simply the number of milliseconds that we will delay at the end of the main loop, which we are already doing, but will now do properly. First, add a game_speed member to the GameData structure:

  int running; // is the game running?
  int game_speed;   // the game speed

and initialize it:

  // Initialise game data
  game.running    = 1;
  game.game_speed = GAME_SPEED;

Then change the SDL_Delay() line from:

    SDL_Delay( 30 );

to:

    SDL_Delay( game.game_speed );

The reason for changing the delay time from a hard-coded number to a macro is so that you can easily change it at the top of the file and recompile for testing different settings. This is why I have made heavy use of macros thus far in this hack.

The P1_SPEED and P2_SPEED macros define the number of pixels that Player 1 or 2 (respectively) can move his paddle every GAME_SPEED number of milliseconds. You must also add two members to the GameData structure to represent this information:

  SDL_Rect p1;       // player 1's paddle
  SDL_Rect p2;       // player 2's paddle

  int      p1_speed; // player 1's paddle speed
  int      p2_speed; // player 2's paddle speed

(Yes, I did re-indent the comments beside the p1 and p2 members to line up with the new comments. That's how I roll.)

As a rule, whenever you add new members to the GameData structure, you also need to add code to initialize them:

  // Initialise game data
  game.running    = 1;
  game.game_speed = GAME_SPEED;
  game.p1_speed   = P1_SPEED;
  game.p2_speed   = P2_SPEED;
  game.num_rects  = 0;

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: