Advertisement
Guest User

c pong game

a guest
Nov 8th, 2018
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.82 KB | None | 0 0
  1.  
  2. #include <graphics.h>
  3. #include <dos.h>
  4. #include <time.h>
  5.  
  6. struct bat
  7. {
  8.  int length;
  9.  int width;
  10.  int xcoordinate;
  11.  int new_ycoordinate;
  12.  int old_ycoordinate;
  13. }batA, batB; // Create Two Bats For Each User.
  14.  
  15. struct ball
  16. {
  17.  int radius;
  18.  int speedx;
  19.  int speedy;
  20.  int old_xcenter;
  21.  int old_ycenter;
  22.  int new_xcenter;
  23.  int new_ycenter;
  24. }ball1;
  25.  
  26. struct score
  27. {
  28.   int score_A;
  29.   int score_B;
  30. }score_game;
  31.            
  32.  
  33. void initialize () // Initialize the game.
  34.  
  35. {
  36.   int gdriver = DETECT, gmode;
  37.   char tempstring [10]; //This String holds score in char format temporarily
  38.   time_t t;// Used to generate random number from system time.
  39.   closegraph ();
  40.   initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); // Intializes Graphics
  41.  
  42.   //Initialise Bat A
  43.   batA.length = 40;
  44.   batA.width = 2;
  45.   batA.xcoordinate = 20;
  46.   batA.new_ycoordinate = 237;
  47.   batA.old_ycoordinate = 237;
  48.  
  49.   // Intialise Bat B
  50.   batB.length = 40;
  51.   batB.width = 2;
  52.   batB.xcoordinate = 620;
  53.   batB.new_ycoordinate = 237;
  54.   batB.old_ycoordinate = 237;
  55.  
  56.    // Intialise Ball
  57.    ball1.radius = 3;
  58.    ball1.speedx = 2;
  59.    srand((int) time(&t)); // Seed rand a random number
  60.    ball1.speedy = rand ()%1;// Sets speed from 0 to 2 depending upon remainder.
  61.    if (rand() % 2 == 0)
  62.      {
  63.        ball1.speedx = - ball1.speedx; // Generate Random X direction.
  64.        ball1.speedy = - ball1.speedy; // Generate Random Y direction.
  65.       }
  66.     ball1.old_xcenter = 320;
  67.     ball1.old_ycenter = 250;
  68.     ball1.new_xcenter = 320;
  69.     ball1.new_ycenter = 250;
  70.    
  71.    // Set Background to white
  72.    setbkcolor (BLACK);
  73.    
  74.    // Draw Ball at Initial Position
  75.    setfillstyle (1,15);
  76.    fillellipse (ball1.new_xcenter,ball1.new_ycenter,ball1.radius,ball1.radius);
  77.  
  78.    // Draw Bats at Intial Position
  79.    bar (batA.xcoordinate,batA.new_ycoordinate,batA.xcoordinate+batA.width,batA.new_ycoordinate+batA.length);
  80.    bar (batB.xcoordinate,batB.new_ycoordinate,batB.xcoordinate+batB.width,batB.new_ycoordinate+batB.length);
  81.  
  82.    // Display Score
  83.    textcolor ();
  84.    sprintf (tempstring,"A - %d",score_game.score_A);
  85.    outtextxy (10,450,tempstring);
  86.    sprintf (tempstring,"B - %d",score_game.score_B);
  87.    outtextxy (590,450,tempstring);
  88.    outtextxy (40,472,"Remove This Line by Learn Coding It yourself at www.cencyclopedia.com");
  89. }
  90.  
  91. void movebat (char input)
  92.  
  93. {
  94.   switch (input)
  95.      {
  96.        case 'A' :
  97.                   if (batA.new_ycoordinate > 0) // Move only when bat is not touching the top so it doesnt jump out of screen.
  98.                     {
  99.                       batA.old_ycoordinate = batA.new_ycoordinate;
  100.                       batA.new_ycoordinate --;
  101.                       setfillstyle (1,0); // Remove last postion.
  102.                       bar (batA.xcoordinate,batA.old_ycoordinate,batA.xcoordinate+batA.width,batA.old_ycoordinate+batA.length);
  103.                       setfillstyle (1,15); // Display New postion.
  104.                       bar (batA.xcoordinate,batA.new_ycoordinate,batA.xcoordinate+batA.width,batA.new_ycoordinate+batA.length);
  105.                      }
  106.                    break;
  107.  
  108.        case 'Z' :
  109.                   if (batA.new_ycoordinate+batA.length < 430) // Make sure bat doesnot go below the screen.
  110.                     {
  111.                       batA.old_ycoordinate = batA.new_ycoordinate;
  112.                       batA.new_ycoordinate ++;
  113.                       setfillstyle (1,0); // Remove last postion.
  114.                       bar (batA.xcoordinate,batA.old_ycoordinate,batA.xcoordinate+batA.width,batA.old_ycoordinate+batA.length);
  115.                       setfillstyle (1,15); // Display New postion.
  116.                       bar (batA.xcoordinate,batA.new_ycoordinate,batA.xcoordinate+batA.width,batA.new_ycoordinate+batA.length);
  117.                     }
  118.                   break;
  119.        case 'J' :
  120.                   if (batB.new_ycoordinate > 0) // Move only when bat is not touching the top so it doesnt jump out of screen.
  121.                      {
  122.                       batB.old_ycoordinate = batB.new_ycoordinate;
  123.                       batB.new_ycoordinate --;
  124.                       setfillstyle (1,0); // Remove last postion.
  125.                       bar (batB.xcoordinate,batB.old_ycoordinate,batB.xcoordinate+batB.width,batB.old_ycoordinate+batB.length);
  126.                       setfillstyle (1,15); // Display New postion.
  127.                       bar (batB.xcoordinate,batB.new_ycoordinate,batB.xcoordinate+batB.width,batB.new_ycoordinate+batB.length);
  128.                      }
  129.                   break;
  130.  
  131.         case 'M' :
  132.                   if (batB.new_ycoordinate+batB.length < 430) // Make sure bat doesnot go below the screen.
  133.                     {
  134.                       batB.old_ycoordinate = batB.new_ycoordinate;
  135.                       batB.new_ycoordinate ++;
  136.                       setfillstyle (1,0); // Remove last postion.
  137.                       bar (batB.xcoordinate,batB.old_ycoordinate,batB.xcoordinate+batB.width,batB.old_ycoordinate+batB.length);
  138.                       setfillstyle (1,15); // Display New postion.
  139.                       bar (batB.xcoordinate,batB.new_ycoordinate,batB.xcoordinate+batB.width,batB.new_ycoordinate+batB.length);
  140.                     }
  141.                    break;
  142.          }
  143.          
  144. }
  145.  
  146. void moveball ()
  147.  
  148. {
  149.    ball1.old_xcenter = ball1.new_xcenter;
  150.    ball1.old_ycenter = ball1.new_ycenter;
  151.    ball1.new_xcenter = ball1.new_xcenter + ball1.speedx;
  152.    ball1.new_ycenter = ball1.new_ycenter + ball1.speedy;
  153.    setcolor (0);
  154.    setfillstyle (1,0); // Remove last postion.
  155.    fillellipse (ball1.old_xcenter,ball1.old_ycenter,ball1.radius,ball1.radius);
  156.    setfillstyle (1,15); // Display New postion.
  157.    fillellipse (ball1.new_xcenter,ball1.new_ycenter,ball1.radius,ball1.radius);
  158.    if ( ball1.new_ycenter - ball1.radius < 0 ) ball1.speedy = -ball1.speedy; // Reflect From Top
  159.    if ( ball1.new_ycenter + ball1.radius > 430 ) ball1.speedy = -ball1.speedy; // Reflect From Bottom
  160. }
  161.  
  162. void physics ()
  163.  
  164. {
  165.   char tempstring [10];
  166.   if ( ball1.new_xcenter - ball1.radius <= 20)
  167.      {
  168.          if (ball1.new_ycenter > batA.new_ycoordinate && ball1.new_ycenter < batA.new_ycoordinate+batA.length)
  169.             {
  170.                ball1.speedx = - ball1.speedx;
  171.                ball1.speedy = rand () % 2;// Sets speed from 0 to 2 depending upon remainder.
  172.                if (rand() % 2 == 0) ball1.speedy = - ball1.speedy; // Generate Random Y direction.
  173.              }
  174.           else // Reintialize entire game with new score
  175.              {
  176.                score_game.score_B ++;
  177.                initialize ();
  178.              }
  179.           return;
  180.       }
  181.  
  182.   if ( ball1.new_xcenter +  ball1.radius > 620)
  183.      
  184.       {
  185.          if (ball1.new_ycenter > batB.new_ycoordinate && ball1.new_ycenter < batB.new_ycoordinate+batB.length)
  186.           {
  187.             ball1.speedx = - ball1.speedx;
  188.             ball1.speedy = rand ()%2;// Sets speed from 0 to 2 depending upon remainder.
  189.             if (rand() % 2 == 0) ball1.speedy = - ball1.speedy; // Generate Random Y direction.
  190.            }
  191.          else // Reintialize game with new score
  192.           {
  193.            score_game.score_A ++;
  194.            initialize ();
  195.           }
  196.            return;
  197.        }
  198. }
  199.  
  200. void play ()
  201.  
  202. {
  203.   while (inportb (0X60) != 16) // Check wether key press is Q if so exit loop
  204.      {
  205.        delay (10); // Reduce game speed to human playable level
  206.        if (inportb (0X60) == 30)  movebat ('A');
  207.        if (inportb (0X60) == 44)  movebat ('Z');
  208.        if (inportb (0X60) == 36)  movebat ('J');
  209.        if (inportb (0X60) == 50)  movebat ('M');
  210.        moveball ();
  211.        physics ();
  212.       }
  213. }
  214.            
  215.  
  216. int main ()
  217.  
  218. {
  219.   score_game.score_A = 0;// Intialise score in Main This Time
  220.   score_game.score_B = 0;// It improves Efficiency
  221.   initialize ();
  222.   play (); // Game Engine
  223.   closegraph (); // Close Graphics
  224.   return 0;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement