Now: Tutorial for Web and Software Design > OS > Linux > OS Content
> Retro Gaming Hacks, Part 2: Add Paddles to Pong [Bookmark it]
Retro Gaming Hacks, Part 2: Add Paddles to Pong

Retro Gaming Hacks, Part 2: Add Paddles to Pong

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;

Prev  [1] [2] [3] [4] [5] [6] Next

[Bookmark][Print] [Close][To Top]
  • Prev Article-OS:

  • Next Article-OS:
  • Related Materias
    How to Deploy Software Usi
    Mastering Windows New Fire
    Creating Visual Studio Pro
    Better Registry Searching
    Using Data Compression in 
    How to Remove Startup Prog
    Registry Hacks for Servers
    Build a Virtual Routed Net
    How To Recover from Regist
    The Ultimate Free Windows 
    Topics
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Graphic Design Tutorial
     

    Coreldraw Tutorial

      Illustrator Tutorial
      3D Graphics Articles
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial&Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial&Articles
     

    XML Style Tutorial

      AJAX Tutorial
      XML Mobile
    Flash Tutorial&Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial&Articles
     

    Linux Tutorial

      Symbian Tutorial
      MacOS Tutorial