home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / misc / util / super_c / pong.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-01-01  |  7.6 KB  |  166 lines

  1. /*              Pong Game Main Program
  2.  */
  3.  
  4. /* Copying modes for rect: */
  5.  
  6. #define COPY 0
  7. #define XOR 1
  8. #define BITSET 2
  9. #define BITCLEAR 3
  10. #define blankPattern 0
  11.  
  12. /* Gray pattern for the wall: */
  13. wallPattern[] = {
  14.     0xAAAA, 0x5555, 0xAAAA, 0x5555, 0xAAAA, 0x5555, 0xAAAA, 0x5555,
  15.     0xAAAA, 0x5555, 0xAAAA, 0x5555, 0xAAAA, 0x5555, 0xAAAA, 0x5555
  16. };
  17.  
  18. /* Diamond pattern for the ball: */
  19. ballPattern[] = {
  20.     0x0180, 0x03C0, 0x07E0, 0x0FF0, 0x1FF8, 0x3FFC, 0x7FFE, 0xFFFF,
  21.     0xFFFF, 0x7FFE, 0x3FFC, 0x1FF8, 0x0FF0, 0x07E0, 0x03C0, 0x0180
  22. };
  23.  
  24. #define FALSE 0
  25. #define TRUE 1
  26.  
  27. /*      main()
  28.  
  29.         Function: Play pong. Simulate a ball moving around the display and
  30.         bouncing off the walls and paddle. If the users hits the up or
  31.         down arrow keys, move the paddle.
  32.  
  33.         Algorithm: Initialize the display and draw the walls, paddle, and
  34.         ball. Then iterate checking the keyboard for up/down arrow hits,
  35.         and simulating the moving ball.
  36. */
  37.  
  38. main()
  39.  
  40. {
  41.         int ballX, ballY;       /* Current location of ball. */
  42.         int newX, newY;         /* Next ball location. */
  43.         int ballXV, ballYV;     /* Current velocity of ball. */
  44.         int padHeight;          /* Paddle height. */
  45.         int padY;               /* Current paddle location. */
  46.         int ch;                 /* Input character from user. */
  47.         int goodHits;           /* Number of successfull rebounds. */
  48.  
  49.         /* Set the display to graphics mode. */
  50.         setMod(6);
  51.  
  52.         /* Set the initial positions and velocities of everything. */
  53.         ballX = 431;
  54.         ballY = 97;
  55.         ballXV = ballYV = 1;
  56.         padHeight = 60;
  57.         padY = 10;
  58.         goodHits = 0;
  59.  
  60.         /* Draw the wall, paddle, and ball. */
  61.         rect(0,0,640,200,blankPattern,1,COPY);          /* Clear screen. */
  62.         rect(0,0,640,10,wallPattern,1,COPY);            /* Top wall. */
  63.         rect(0,190,640,200,wallPattern,1,COPY);         /* Bottom wall. */
  64.         rect(630,10,640,190,wallPattern,1,COPY);        /* Right wall. */
  65.         rect(10,padY,20,padY+padHeight,0,1,XOR);        /* Paddle. */
  66.         rect(ballX,ballY,ballX+16,ballY+16,ballPattern,0,XOR); /* Ball. */
  67.  
  68.         /* Loop, simulating movement and waiting for commands from the user. */
  69.         while (TRUE) {
  70.                 /* Check if there's a keypress waiting. */
  71.                 if (keyChk(&ch)) {
  72.                         /* If so, read it from the keystroke queue. */
  73.                         keyRd();
  74.                         /* If it was an ESC, exit the main loop. */
  75.                         if ((ch & 0xFF) == 0x1B) break;
  76.                         /* If it was an extended key, switch on that key. */
  77.                         if ((ch & 0xFF) == 0)
  78.                                 switch ((ch>>8) & 0xFF) {
  79.                                         /* Up arrow: move paddle up. */
  80.                                         case 72:
  81.                                                 /* Check for at top. */
  82.                                                 if (padY <= 20) break;
  83.                                                 /* Draw new paddle. */
  84.                                                 rect(10,padY-20,20,
  85.                                                      padY+padHeight-20,0,1,1);
  86.                                                 /* Erase old paddle. */
  87.                                                 rect(10,padY,20,
  88.                                                      padY+padHeight,0,1,1);
  89.                                                 /* Update position. */
  90.                                                 padY -= 20;
  91.                                                 break;
  92.                                         /* Down arrow: move paddle down. */
  93.                                         case 80:
  94.                                                 /* Check for at bottom. */
  95.                                                 if ((padY+padHeight) >= 180) 
  96.                                                         break;
  97.                                                 /* Draw new paddle. */
  98.                                                 rect(10,padY+20,20,
  99.                                                      padY+padHeight+20,0,1,1);
  100.                                                 /* Erase old paddle. */
  101.                                                 rect(10,padY,20,
  102.                                                      padY+padHeight,0,1,1);
  103.                                                 /* Update position. */
  104.                                                 padY += 20;
  105.                                                 break;
  106.                                 };
  107.                 };
  108.                 /* If the ball is still on the display... */
  109.                 if (ballX > -16) {
  110.                         /* Compute where the ball will go next. */
  111.                         newX = ballX+ballXV;
  112.                         newY = ballY+ballYV;
  113.                         /* Check and adjust for running into top or bottom. */
  114.                         if ((newY < 10) || (newY > 174)) {
  115.                                 ballYV = -ballYV;
  116.                                 newY = ballY+ballYV;
  117.                         };
  118.                         /* Check and adjust for running into the right wall. */
  119.                         if (newX > 614) {
  120.                                 ballXV = -ballXV;
  121.                                 newX = ballX+ballXV;
  122.                         /* Check and adjust for running into the paddle. */
  123.                         } else if ((padY <= (ballY+15)) &&
  124.                                    ((padY+padHeight) >= ballY) &&
  125.                                    (ballX >= 20) &&
  126.                                    (newX < 20)) {
  127.                                 /* Reverse direction. */
  128.                                 ballXV = -ballXV;
  129.                                 newX = ballX+ballXV;
  130.                                 /* Check for edge of paddle hit. */
  131.                                 if (ballY < padY) {
  132.                                         if (--ballYV == 0) ballYV--;
  133.                                 } else if ((ballY+15) > (padY+padHeight)) {
  134.                                         if (++ballYV == 0) ballYV++;
  135.                                 };
  136.                                 /* If he's gotten five, speed it up. */
  137.                                 if (((++goodHits) % 5) == 0) {
  138.                                         if (ballXV > 0) ballXV++;
  139.                                         else ballXV--;
  140.                                         if (ballYV > 0) ballYV++;
  141.                                         else ballYV--;
  142.                                 };
  143.                         };
  144.                         /* Draw the new ball. */
  145.                         rect(newX,newY,newX+16,newY+16,ballPattern,0,XOR);
  146.                         /* Erase the old ball. */
  147.                         rect(ballX,ballY,ballX+16,ballY+16,ballPattern,0,XOR);
  148.                         /* Update the ball position. */
  149.                         ballX = newX; ballY = newY;
  150.                 /* Otherwise, the ball is off the screen -- he missed. */
  151.                 } else {
  152.                         /* Put the ball ack on the screen, and reset hits. */
  153.                         ballX = 431;
  154.                         ballY = 97;
  155.                         ballXV = ballYV = 1;
  156.                         goodHits = 0;
  157.                         /* Draw the ball. */
  158.                         rect(ballX,ballY,ballX+16,ballY+16,ballPattern,0,1);
  159.                 };
  160.         };
  161.  
  162.         /* Reset the display mode to text. */
  163.         setMod(2);
  164. }
  165.  
  166.