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




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