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

Retro Gaming Hacks, Part 3: Add a Ball and Score to Pong

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

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