Ping Pong Game in C Language

I hope you all enjoyed playing Tic-Tac-Toe with us, now its time to play Ping Pong. We will code it in C Language. Ping Pong in C is a simple game to play but not easy to program especially for beginners but dont

worry stay with the tutorial, do what it says and you will be done in no time.

 



Graphic Library Used - Turbo C/C++ BGI

Operating System Used - Windows XP Professional

Requirements:-

Install Turbo C++ 3.0 IDE and install it in folder C:\TC. Thats all now you are all done to start your games programming fantasies.

Getting Started:-


Go to C:\TC\Bin there you will find a executable file called TC execute it and IDE (What is IDE?) will load itself.

Now go to File Menu and choose New.

Now Press F2 to save the file. Give it name pingpong.c.

Now you are all set to rock.

Coding main( )

First thing a program should do is to include header files and to code main() only header we will be needing is graphics.h . Including unnecessary header files increase the size of the code and compile time. Next we

create a parameterless int returning main() with no code at present. Your Code should look like this -

#include <conio.h>

int main ()

{
           

return 0;

}
         

Intializing Graphics

Now we intialise graphics in main(), stop for a key press and then close the graphics. When we stop you should see a blank page and not any error. If you face an error, make sure your installation path is correct. All

our game activity occurs in this opening and closing of graphics. It will generally give you a 640*480 mode with VGA graphics. These graphics are good enough to code any descent looking games. If you get different

settings try to set them as above. Your code should be now -

#include <graphics.h>

int main ()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");       // Intializes Graphics
getch ();                                                      // Key Press
closegraph ();                                              // Close Graphics
return 0;
}

Game Data

Now we create data our game needs . In this game we need following data -

Score : Player A Score & Player B Score.

Bats : Length, Width, X Coordinate, Old Y Coordinate, New Y Coordinate (Since bat moves only up and down)

Ball : Radius,Speed in X Direction, Speed in Y Direction, Old X center , Old Y center, New X centre, New Y centre

You can change these parameters as you wish to modify your game but changing them may need a alteration in code otherwise it could lead to preety stupid behaviour of game and severe cases a crash (very

unlikely).

Now we use structure to store data and code will be as follow -

#include <graphics.h>

struct bat
{
 int length;
 int width;
 int xcoordinate;
 int new_ycoordinate;
 int old_ycoordinate;
}batA, batB;                  // Create Two Bats For Each User.

struct ball
{
 int radius;
 int speedx;
 int speedy;
 int old_xcenter;
 int old_ycenter;
 int new_xcenter;
 int new_ycenter;
}ball1;

struct score
{
  int score_A;
  int score_B;
}score_game;

int main ()

{
 int gdriver = DETECT, gmode;
 initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); // Intializes Graphics
 getch (); // Key Press
 closegraph (); // Close Graphics
 return 0;
}

Game Initializaton

Now we create a function which intializes all your data and create a board with non moving or static elements permanently and dynamic or moving elements to intial postion. We call this function through main only

once. You can modify parameters here to see the changes. If you are unable to understand what function does press SHIFT + F1 and see in index or you are free to contanct us..

You screen should now look like this -

Ping Pong

Code Now Becomes -

#include <graphics.h>

struct bat
{
 int length;
 int width;
 int xcoordinate;
 int new_ycoordinate;
 int old_ycoordinate;
}batA, batB; // Create Two Bats For Each User.

struct ball
{
 int radius;
 int speedx;
 int speedy;
 int old_xcenter;
 int old_ycenter;
 int new_xcenter;
 int new_ycenter;
}ball1;

struct score
{
  int score_A;
  int score_B;
}score_game;

void initialize () // Initialize the game.

{
 char tempstring [10]; //This String holds score in char format temporarily
 
 //Initialise Bat A
 
 batA.length = 20;
 batA.width = 2;
 batA.xcoordinate = 20;
 batA.new_ycoordinate = 237;
 batA.old_ycoordinate = 237;
 
 // Intialise Bat B
 
 batB.length = 20;
 batB.width = 2;
 batB.xcoordinate = 620;
 batB.new_ycoordinate = 237;
 batB.old_ycoordinate = 237;
 
 // Intialise Ball
 ball1.radius = 3;
 ball1.speedx = 4;
 ball1.speedy = 0;
 ball1.old_xcenter = 320;
 ball1.old_ycenter = 250;
 ball1.new_xcenter = 320;
 ball1.new_ycenter = 250;
 
 // Intialise Score
 score_game.score_A = 0;
 score_game.score_B = 0;
 
 // Set Background to white
 setbkcolor (BLACK);


 // Draw Ball at Initial Position
 setfillstyle (1,15);
 fillellipse (ball1.new_xcenter,ball1.new_ycenter,ball1.radius,ball1.radius);


 // Draw Bats at Intial Position
 bar (batA.xcoordinate,batA.new_ycoordinate,batA.xcoordinate+batA.width,batA.new_ycoordinate+batA.length);
 bar (batB.xcoordinate,batB.new_ycoordinate,batB.xcoordinate+batB.width,batB.new_ycoordinate+batB.length);


 // Display Score
 textcolor ();
 sprintf (tempstring,"A - %d",score_game.score_A);
 outtextxy (10,450,tempstring);
 sprintf (tempstring,"B - %d",score_game.score_A);
 outtextxy (590,450,tempstring);

}

int main ()

{
 int gdriver = DETECT, gmode;
 initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); // Intializes Graphics
 initialize ();
 getch (); // Key Press
 closegraph (); // Close Graphics
 return 0;
}
          

Game Engine


Now we take a big step forward. We move from static to dynamic and we design a game engine for us to do that. Now lets take a overview what is changing.

1. Bats move up and down for both players on thier will.

2. Ball moves between the two bats controlled by computer.

3. Score Changes when a player miss.

We introduce this function in main() and when this function returns the game exit.

All this occurs in our game engine function called play ( ). In this function there is a infinite while loop which exits only when player press exit command (Q Key). Now we discuss each part of game engine in details.


Moving Bats -

We pick four keys 2 for player A up and down and 2 for player B up and down . Let the keys be

A - Player A up

Z - Player A down

J - Player B up

M - Player B down

To check for input we use a function called inportb and for that we need header <dos.h>. We use 0X60 port designated to keyboard.

Then we use a switch case for action to be performed.

Action is performed by a function called movebat() which removes the last picture of the bat and place a new one. It also calculate the new coordinates of bat after moving.

The Code Now becomes -

#include <graphics.h>
#include <dos.h>

struct bat

{
 int length;
 int width;
 int xcoordinate;
 int new_ycoordinate;
 int old_ycoordinate;
}batA, batB; // Create Two Bats For Each User.

struct ball
{
  int radius;
  int speedx;
  int speedy;
  int old_xcenter;
  int old_ycenter;
  int new_xcenter;
  int new_ycenter;
}ball1;

struct score
{
  int score_A;
  int score_B;
}score_game;

void initialize () // Initialize the game.

{
 char tempstring [10]; //This String holds score in char format temporarily
 
 //Initialise Bat A
 batA.length = 20;
 batA.width = 2;
 batA.xcoordinate = 20;
 batA.new_ycoordinate = 237;
 batA.old_ycoordinate = 237;
 
 // Intialise Bat B
 batB.length = 20;
 batB.width = 2;
 batB.xcoordinate = 620;
 batB.new_ycoordinate = 237;
 batB.old_ycoordinate = 237;
 
 // Intialise Ball
 ball1.radius = 3;
 ball1.speedx = 4;
 ball1.speedy = 0;
 ball1.old_xcenter = 320;
 ball1.old_ycenter = 250;
 ball1.new_xcenter = 320;
 ball1.new_ycenter = 250;
 
 // Intialise Score
 score_game.score_A = 0;
 score_game.score_B = 0;
 
 // Set Background to white
 setbkcolor (BLACK);
 
 // Draw Ball at Initial Position
 setfillstyle (1,15);
 fillellipse (ball1.new_xcenter,ball1.new_ycenter,ball1.radius,ball1.radius);
 
 // Draw Bats at Intial Position
 bar (batA.xcoordinate,batA.new_ycoordinate,batA.xcoordinate+batA.width,batA.new_ycoordinate+batA.length);
 bar (batB.xcoordinate,batB.new_ycoordinate,batB.xcoordinate+batB.width,batB.new_ycoordinate+batB.length);
 
 // Display Score
 textcolor ();
 sprintf (tempstring,"A - %d",score_game.score_A);
 outtextxy (10,450,tempstring);
 sprintf (tempstring,"B - %d",score_game.score_A);
 outtextxy (590,450,tempstring);
}

void movebat (char input)

{
 switch (input)
   {
     case 'A' :
                if (batA.new_ycoordinate > 0) // Move only when bat is not touching the top so it doesnt jump out of screen.
                  {
                    batA.old_ycoordinate = batA.new_ycoordinate;
                    batA.new_ycoordinate --;
                    setfillstyle (1,0); // Remove last postion.
                    bar (batA.xcoordinate,batA.old_ycoordinate,batA.xcoordinate+batA.width,batA.old_ycoordinate+batA.length);
                    setfillstyle (1,15); // Display New postion.
                    bar (batA.xcoordinate,batA.new_ycoordinate,batA.xcoordinate+batA.width,batA.new_ycoordinate+batA.length);
                    break;
                  }
    
     case 'Z' :
               if (batA.new_ycoordinate+batA.length < 480) // Make sure bat doesnot go below the screen.
                  {
                    batA.old_ycoordinate = batA.new_ycoordinate;
                    batA.new_ycoordinate ++;
                    setfillstyle (1,0); // Remove last postion.
                    bar (batA.xcoordinate,batA.old_ycoordinate,batA.xcoordinate+batA.width,batA.old_ycoordinate+batA.length);
                    setfillstyle (1,15); // Display New postion.
                    bar (batA.xcoordinate,batA.new_ycoordinate,batA.xcoordinate+batA.width,batA.new_ycoordinate+batA.length);
                    break;
                   }
    
     case 'J' :
               if (batB.new_ycoordinate > 0) // Move only when bat is not touching the top so it doesnt jump out of screen.
                 {
                   batB.old_ycoordinate = batB.new_ycoordinate;
                   batB.new_ycoordinate --;
                   setfillstyle (1,0); // Remove last postion.
                   bar (batB.xcoordinate,batB.old_ycoordinate,batB.xcoordinate+batB.width,batB.old_ycoordinate+batB.length);
                   setfillstyle (1,15); // Display New postion.
                   bar (batB.xcoordinate,batB.new_ycoordinate,batB.xcoordinate+batB.width,batB.new_ycoordinate+batB.length);
                   break;
                 }
   
    case 'M' :
              if (batB.new_ycoordinate+batB.length < 480) // Make sure bat doesnot go below the screen.
                {
                  batB.old_ycoordinate = batB.new_ycoordinate;
                  batB.new_ycoordinate ++;
                  setfillstyle (1,0); // Remove last postion.
                  bar (batB.xcoordinate,batB.old_ycoordinate,batB.xcoordinate+batB.width,batB.old_ycoordinate+batB.length);
                  setfillstyle (1,15); // Display New postion.
                  bar (batB.xcoordinate,batB.new_ycoordinate,batB.xcoordinate+batB.width,batB.new_ycoordinate+batB.length);
                  break;
                }
    
   }

}

void play ()\\ Our Budding Game Engine

{
   while (inportb (0X60) != 16) // Check wether key press is Q if so exit loop
    {
      delay (10); // Reduce game speed to human playable level
      if (inportb (0X60) == 30)  movebat ('A');
      if (inportb (0X60) == 44)  movebat ('Z');
      if (inportb (0X60) == 36)  movebat ('J');
      if (inportb (0X60) == 50)  movebat ('M');
    }
}
           

int main ()

{       
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); // Intializes Graphics
initialize ();
play ();                                   // Game Engine
closegraph ();                            // Close Graphics
return 0;
}


Move The Ball


Now since we are done with bat lets move the ball.

If speed of the ball speedx is positive, ball moves in right direction and if speedx of the ball is negative ball moves in left direction.

If speed of the ball speedy is positive, ball moves in up direction and if speedy of the ball is negative ball moves in down direction.

If ball hits the top or bottom its speedy changes sign to bring reflection.

We do this in function called moveball() and call this function in our game engine.

Also we need to make ball behaviour unpredictable so we use srand and rand to intialize ball speed y from 0 - 3 pixel per second and choose two of four adjacent direction to produce any possible direction.. We also

need header time.h for this purpose.

Now code becomes even bigger, dont get scared its easy to understand.

 

#include <graphics.h>
#include <dos.h>
#include <time.h>

struct bat
{

 int length;
 int width;
 int xcoordinate;
 int new_ycoordinate;
 int old_ycoordinate;
}batA, batB; // Create Two Bats For Each User.

struct ball
{
  int radius;
  int speedx;
  int speedy;
  int old_xcenter;
  int old_ycenter;
  int new_xcenter;
  int new_ycenter;
}ball1;

struct score
{
  int score_A;
  int score_B;
}score_game;

void initialize () // Initialize the game.

{
 char tempstring [10]; //This String holds score in char format temporarily
 time_t t;             // Used to generate random number from system time.
       
 //Initialise Bat A
 batA.length = 20;
 batA.width = 2;
 batA.xcoordinate = 20;
 batA.new_ycoordinate = 237;
 batA.old_ycoordinate = 237;
 
 // Intialise Bat B
 batB.length = 20;
 batB.width = 2;
 batB.xcoordinate = 620;
 batB.new_ycoordinate = 237;
 batB.old_ycoordinate = 237;
 
 // Intialise Ball
 ball1.radius = 3;
 ball1.speedx = 2;
 srand((int) time(&t)); // Seed rand a random number
 ball1.speedy = rand ()%3;// Sets speed from 0 to 3 depending upon remainder.
 if (rand() % 2 == 0)
  {
    ball1.speedx = - ball1.speedx; // Generate Random X direction.
    ball1.speedy = - ball1.speedy; // Generate Random Y direction.
  }
 ball1.old_xcenter = 320;
 ball1.old_ycenter = 250;
 ball1.new_xcenter = 320;
 ball1.new_ycenter = 250;

 // Intialise Score
 score_game.score_A = 0;
 score_game.score_B = 0;
 
 // Set Background to white
 setbkcolor (BLACK);
 
 // Draw Ball at Initial Position
 setfillstyle (1,15);
 fillellipse (ball1.new_xcenter,ball1.new_ycenter,ball1.radius,ball1.radius);
 
 // Draw Bats at Intial Position
 bar (batA.xcoordinate,batA.new_ycoordinate,batA.xcoordinate+batA.width,batA.new_ycoordinate+batA.length);
 bar (batB.xcoordinate,batB.new_ycoordinate,batB.xcoordinate+batB.width,batB.new_ycoordinate+batB.length);
 
 // Display Score
 textcolor ();
 sprintf (tempstring,"A - %d",score_game.score_A);
 outtextxy (10,450,tempstring);
 sprintf (tempstring,"B - %d",score_game.score_A);
 outtextxy (590,450,tempstring);
 outtextxy (40,470,"Remove This Line by Learn Coding It yourself at www.cencyclopedia.com");
        
}

void movebat (char input)

{
  switch (input)
      {
         case 'A' :
                   if (batA.new_ycoordinate > 0) // Move only when bat is not touching the top so it doesnt jump out of screen.
                    {
                      batA.old_ycoordinate = batA.new_ycoordinate;
                      batA.new_ycoordinate --;
                      setfillstyle (1,0); // Remove last postion.
                      bar (batA.xcoordinate,batA.old_ycoordinate,batA.xcoordinate+batA.width,batA.old_ycoordinate+batA.length);
                      setfillstyle (1,15); // Display New postion.
                      bar (batA.xcoordinate,batA.new_ycoordinate,batA.xcoordinate+batA.width,batA.new_ycoordinate+batA.length);
                    }
                    break;
         case 'Z' :
                   if (batA.new_ycoordinate+batA.length < 430) // Make sure bat doesnot go below the screen.
                    {
                      batA.old_ycoordinate = batA.new_ycoordinate;
                      batA.new_ycoordinate ++;
                      setfillstyle (1,0); // Remove last postion.
                      bar (batA.xcoordinate,batA.old_ycoordinate,batA.xcoordinate+batA.width,batA.old_ycoordinate+batA.length);
                      setfillstyle (1,15); // Display New postion.
                      bar (batA.xcoordinate,batA.new_ycoordinate,batA.xcoordinate+batA.width,batA.new_ycoordinate+batA.length);
                    }
                    break;
         case 'J' :
                   if (batB.new_ycoordinate > 0) // Move only when bat is not touching the top so it doesnt jump out of screen.
                    {
                      batB.old_ycoordinate = batB.new_ycoordinate;
                      batB.new_ycoordinate --;
                      setfillstyle (1,0); // Remove last postion.
                      bar (batB.xcoordinate,batB.old_ycoordinate,batB.xcoordinate+batB.width,batB.old_ycoordinate+batB.length);
                      setfillstyle (1,15); // Display New postion.
                      bar (batB.xcoordinate,batB.new_ycoordinate,batB.xcoordinate+batB.width,batB.new_ycoordinate+batB.length);
                    }
                    break;

          case 'M' :
                   if (batB.new_ycoordinate+batB.length < 430) // Make sure bat doesnot go below the screen.
                    {
                      batB.old_ycoordinate = batB.new_ycoordinate;
                      batB.new_ycoordinate ++;
                      setfillstyle (1,0); // Remove last postion.
                      bar (batB.xcoordinate,batB.old_ycoordinate,batB.xcoordinate+batB.width,batB.old_ycoordinate+batB.length);
                      setfillstyle (1,15); // Display New postion.
                      bar (batB.xcoordinate,batB.new_ycoordinate,batB.xcoordinate+batB.width,batB.new_ycoordinate+batB.length);
                    }
                    break;

         }


}

void moveball ()

{
  ball1.old_xcenter = ball1.new_xcenter;
  ball1.old_ycenter = ball1.new_ycenter;
  ball1.new_xcenter = ball1.new_xcenter + ball1.speedx;
  ball1.new_ycenter = ball1.new_ycenter + ball1.speedy;
  setcolor (0);
  setfillstyle (1,0);// Remove last postion.
  fillellipse (ball1.old_xcenter,ball1.old_ycenter,ball1.radius,ball1.radius);
  setfillstyle (1,15); // Display New postion.
  fillellipse (ball1.new_xcenter,ball1.new_ycenter,ball1.radius,ball1.radius);
  if ( ball1.new_ycenter - ball1.radius < 0 ) ball1.speedy = -ball1.speedy; // Reflect From Top
  if ( ball1.new_ycenter + ball1.radius > 430 ) ball1.speedy = -ball1.speedy; // Reflect From Bottom

}

void play ()

{
  while (inportb (0X60) != 16) // Check wether key press is Q if so exit loop
    {
      delay (10); // Reduce game speed to human playable level
      if (inportb (0X60) == 30)  movebat ('A');
      if (inportb (0X60) == 44)  movebat ('Z');
      if (inportb (0X60) == 36)  movebat ('J');
      if (inportb (0X60) == 50)  movebat ('M');
      moveball ();
     }
}
           

int main ()

{
  int gdriver = DETECT, gmode;
  initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); // Intializes Graphics
  initialize ();
  play (); // Game Engine
  closegraph (); // Close Graphics
  return 0;
}

 
Physics Processor

Another most important part of any game is physics processor . It calculates the physics in our game bring real world similarities.

In this Simple Game Physics is not this complicated but when we build 3D Game togther it is far more complicated than graphics.

Now we Analyse what physics do we need here -

1. When ball hit left or right of the screen it reflects itself if their is bat there or bring itself to the centre and increment the score.

Now we can modify our intialize function a little and little bit of main this can be achieved as a piece of cake without tough programming. What we do is we dont initialize score in intialize function but in main so that

intialize function can be called again without reseting score. Also we need to a closegraph and renitialise graphic in initialize function.

Because of above we can now remove the burden of intializing graphics from main making program more efficient. Hence we hit two targets in one arrow.

We create a function called physics() to do this for us.

Now we present the final code of this version -

#include <graphics.h>
#include <dos.h>
#include <time.h>

struct bat
{
 int length;
 int width;
 int xcoordinate;
 int new_ycoordinate;
 int old_ycoordinate;
}batA, batB; // Create Two Bats For Each User.

struct ball
{
 int radius;
 int speedx;
 int speedy;
 int old_xcenter;
 int old_ycenter;
 int new_xcenter;
 int new_ycenter;
}ball1;

struct score
{
  int score_A;
  int score_B;
}score_game;
           

void initialize () // Initialize the game.

{
  int gdriver = DETECT, gmode;
  char tempstring [10]; //This String holds score in char format temporarily
  time_t t;// Used to generate random number from system time.
  closegraph ();
  initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); // Intializes Graphics
 
  //Initialise Bat A
  batA.length = 40;
  batA.width = 2;
  batA.xcoordinate = 20;
  batA.new_ycoordinate = 237;
  batA.old_ycoordinate = 237;
 
  // Intialise Bat B
  batB.length = 40;
  batB.width = 2;
  batB.xcoordinate = 620;
  batB.new_ycoordinate = 237;
  batB.old_ycoordinate = 237;
 
   // Intialise Ball
   ball1.radius = 3;
   ball1.speedx = 2;
   srand((int) time(&t)); // Seed rand a random number
   ball1.speedy = rand ()%1;// Sets speed from 0 to 2 depending upon remainder.
   if (rand() % 2 == 0)
     {
       ball1.speedx = - ball1.speedx; // Generate Random X direction.
       ball1.speedy = - ball1.speedy; // Generate Random Y direction.
      }
    ball1.old_xcenter = 320;
    ball1.old_ycenter = 250;
    ball1.new_xcenter = 320;
    ball1.new_ycenter = 250;
  
   // Set Background to white
   setbkcolor (BLACK);
  
   // Draw Ball at Initial Position
   setfillstyle (1,15);
   fillellipse (ball1.new_xcenter,ball1.new_ycenter,ball1.radius,ball1.radius);
 
   // Draw Bats at Intial Position
   bar (batA.xcoordinate,batA.new_ycoordinate,batA.xcoordinate+batA.width,batA.new_ycoordinate+batA.length);
   bar (batB.xcoordinate,batB.new_ycoordinate,batB.xcoordinate+batB.width,batB.new_ycoordinate+batB.length);
 
   // Display Score
   textcolor ();
   sprintf (tempstring,"A - %d",score_game.score_A);
   outtextxy (10,450,tempstring);
   sprintf (tempstring,"B - %d",score_game.score_B);
   outtextxy (590,450,tempstring);
   outtextxy (40,472,"Remove This Line by Learn Coding It yourself at www.cencyclopedia.com");
}

void movebat (char input)

{
  switch (input)
     {
       case 'A' :
                  if (batA.new_ycoordinate > 0) // Move only when bat is not touching the top so it doesnt jump out of screen.
                    {
                      batA.old_ycoordinate = batA.new_ycoordinate;
                      batA.new_ycoordinate --;
                      setfillstyle (1,0); // Remove last postion.
                      bar (batA.xcoordinate,batA.old_ycoordinate,batA.xcoordinate+batA.width,batA.old_ycoordinate+batA.length);
                      setfillstyle (1,15); // Display New postion.
                      bar (batA.xcoordinate,batA.new_ycoordinate,batA.xcoordinate+batA.width,batA.new_ycoordinate+batA.length);
                     }
                   break;

       case 'Z' :
                  if (batA.new_ycoordinate+batA.length < 430) // Make sure bat doesnot go below the screen.
                    {
                      batA.old_ycoordinate = batA.new_ycoordinate;
                      batA.new_ycoordinate ++;
                      setfillstyle (1,0); // Remove last postion.
                      bar (batA.xcoordinate,batA.old_ycoordinate,batA.xcoordinate+batA.width,batA.old_ycoordinate+batA.length);
                      setfillstyle (1,15); // Display New postion.
                      bar (batA.xcoordinate,batA.new_ycoordinate,batA.xcoordinate+batA.width,batA.new_ycoordinate+batA.length);
                    }
                  break;
       case 'J' :
                  if (batB.new_ycoordinate > 0) // Move only when bat is not touching the top so it doesnt jump out of screen.
                     {
                      batB.old_ycoordinate = batB.new_ycoordinate;
                      batB.new_ycoordinate --;
                      setfillstyle (1,0); // Remove last postion.
                      bar (batB.xcoordinate,batB.old_ycoordinate,batB.xcoordinate+batB.width,batB.old_ycoordinate+batB.length);
                      setfillstyle (1,15); // Display New postion.
                      bar (batB.xcoordinate,batB.new_ycoordinate,batB.xcoordinate+batB.width,batB.new_ycoordinate+batB.length);
                     }
                  break;

        case 'M' :
                  if (batB.new_ycoordinate+batB.length < 430) // Make sure bat doesnot go below the screen.
                    {
                      batB.old_ycoordinate = batB.new_ycoordinate;
                      batB.new_ycoordinate ++;
                      setfillstyle (1,0); // Remove last postion.
                      bar (batB.xcoordinate,batB.old_ycoordinate,batB.xcoordinate+batB.width,batB.old_ycoordinate+batB.length);
                      setfillstyle (1,15); // Display New postion.
                      bar (batB.xcoordinate,batB.new_ycoordinate,batB.xcoordinate+batB.width,batB.new_ycoordinate+batB.length);
                    }
                   break;
         }
        
}

void moveball ()

{
   ball1.old_xcenter = ball1.new_xcenter;
   ball1.old_ycenter = ball1.new_ycenter;
   ball1.new_xcenter = ball1.new_xcenter + ball1.speedx;
   ball1.new_ycenter = ball1.new_ycenter + ball1.speedy;
   setcolor (0);
   setfillstyle (1,0); // Remove last postion.
   fillellipse (ball1.old_xcenter,ball1.old_ycenter,ball1.radius,ball1.radius);
   setfillstyle (1,15); // Display New postion.
   fillellipse (ball1.new_xcenter,ball1.new_ycenter,ball1.radius,ball1.radius);
   if ( ball1.new_ycenter - ball1.radius < 0 ) ball1.speedy = -ball1.speedy; // Reflect From Top
   if ( ball1.new_ycenter + ball1.radius > 430 ) ball1.speedy = -ball1.speedy; // Reflect From Bottom
}

void physics ()

{
  char tempstring [10];
  if ( ball1.new_xcenter - ball1.radius <= 20)
     {
         if (ball1.new_ycenter > batA.new_ycoordinate && ball1.new_ycenter < batA.new_ycoordinate+batA.length)
            {
               ball1.speedx = - ball1.speedx;
               ball1.speedy = rand () % 2;// Sets speed from 0 to 2 depending upon remainder.
               if (rand() % 2 == 0) ball1.speedy = - ball1.speedy; // Generate Random Y direction.
             }
          else // Reintialize entire game with new score
             {
               score_game.score_B ++;
               initialize ();
             }
          return;
      }

  if ( ball1.new_xcenter +  ball1.radius > 620)
    
      {
         if (ball1.new_ycenter > batB.new_ycoordinate && ball1.new_ycenter < batB.new_ycoordinate+batB.length)
          {
            ball1.speedx = - ball1.speedx;
            ball1.speedy = rand ()%2;// Sets speed from 0 to 2 depending upon remainder.
            if (rand() % 2 == 0) ball1.speedy = - ball1.speedy; // Generate Random Y direction.
           }
         else // Reintialize game with new score
          {
           score_game.score_A ++;
           initialize ();
          }
           return;
       }
}

void play ()

{
  while (inportb (0X60) != 16) // Check wether key press is Q if so exit loop
     {
       delay (10); // Reduce game speed to human playable level
       if (inportb (0X60) == 30)  movebat ('A');
       if (inportb (0X60) == 44)  movebat ('Z');
       if (inportb (0X60) == 36)  movebat ('J');
       if (inportb (0X60) == 50)  movebat ('M');
       moveball ();
       physics ();
      }
}
           

int main ()

{
  score_game.score_A = 0;// Intialise score in Main This Time
  score_game.score_B = 0;// It improves Efficiency
  initialize ();
  play (); // Game Engine
  closegraph (); // Close Graphics
  return 0;
}

 

So thats all for now.
Report any bug if you find and do post your Queries and Suggestion.
Enjoy...Happy Gaming:)

Comments

Member since:
13 October 2010
Last activity:
6 years 27 weeks

hey r u using at89s52??

Member since:
20 September 2010
Last activity:
6 years 31 weeks

what do you mean by "Now we intialise graphics in main(), stop for a key press and then close the graphics."????
i did'nt really understand this statement stop for a key press.

Member since:
5 September 2010
Last activity:
6 years 31 weeks

first bug on line 35

Member since:
10 August 2010
Last activity:
6 years 37 weeks

Hi there,

I am using a 8051 microcontroller to create this game. i am using LEDs as the ball and pushbuttons as the bat. Say for example i have 5 pushbuttons and pushbutton 1 and 5 are the bets while the rest are for other commands. How do i modify this program to make this possible. Please give me a head start as i am confused at the moment.