This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!

Pong #2

By: darkestfear3592 on Oct 22nd, 2012  |  syntax: C++  |  size: 9.23 KB  |  views: 72  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <GL/glut.h>
  2. #include<iostream>
  3. #include<ctime>
  4. #include<sstream>
  5. #include<windows.h>
  6. using namespace std;
  7.  
  8. // Game state
  9. enum { MENU, GAME, END };
  10. int width=800, height=600;      // Window params
  11. int gamestate = MENU;           // Game state
  12. int score1=0, score2=0;         // Scores of 2 players
  13. int winScore=10;                // Score to win
  14. bool keyStates[256] = { 0 };    // Key presses
  15.  
  16. // Board params
  17. double board1x=20,  board2x=770;
  18. double board1y=450, board2y=450;
  19. double boardWidth=10, boardHeight=100;
  20. double boardSpeed = 12;
  21.  
  22. // Ball params
  23. bool   ballUp    = true;
  24. bool   ballRight = true;
  25. double ballSpeed = 8;
  26. double ballCurSpeed = ballSpeed; //Speed just after hitting a board (fn of distance from center) Max value = 2*ballSpeed
  27. double ballSize = 10;
  28. double ballX = board1x + boardWidth;
  29. double ballY = board1y - boardHeight/2;
  30.  
  31. // Misc params
  32. time_t start;
  33. int timePassed = 0;
  34. bool miss1,miss2;
  35. string self("by Max");
  36. string text("Press RETURN to start");
  37. string ins1("Player 1 - keys Q,A");
  38. string ins2("Player 2 - keys P,L");
  39. string rule("First to score 10 wins!");
  40. string miss("MISS!");
  41. string win1("Player 1 wins!");
  42. string win2("Player 2 wins!");
  43. string playagain("Play again (RETURN)");
  44. string reset("Reset (Spacebar)");
  45.  
  46. void ball();                // Draws ball
  47. void ballOperations();      // Moves ball
  48. void board(double,double);  // Draws boards
  49. void cube();                // Draws cube
  50. void drawTitle();           // Draws menu
  51. void drawString(string,double,double);          // Draws a string
  52. void init();
  53. void keyOperations();
  54. void display();
  55. void keyboardDown();
  56. void keyboardUp();
  57. void idle();
  58.  
  59. void resetState() {
  60.     ballUp = true;
  61.     ballRight = true;
  62.     ballSpeed = 10;
  63.     ballCurSpeed = ballSpeed;
  64.     ballSize = 10;
  65.     ballX = board1x + boardWidth;
  66.     ballY = board1y - boardHeight/2;
  67.     score1=0;
  68.     score2=0;
  69. }
  70.  
  71. void ball() {
  72.     glColor3f(1,1,1);
  73.     glBegin(GL_POLYGON);
  74.         glVertex2d(ballX,ballY);
  75.         glVertex2d(ballX+ballSize,ballY);
  76.         glVertex2d(ballX+ballSize,ballY-ballSize);
  77.         glVertex2d(ballX,ballY-ballSize);
  78.     glEnd();
  79. }
  80.  
  81. void board(double boardx, double boardy) {    
  82.     glColor3f(1,1,1);
  83.     glBegin(GL_POLYGON);
  84.         glVertex2d(boardx,boardy);
  85.         glVertex2d(boardx+boardWidth,boardy);
  86.         glVertex2d(boardx+boardWidth,boardy-boardHeight);
  87.         glVertex2d(boardx,boardy-boardHeight);
  88.     glEnd();
  89. }
  90.  
  91. void ballOperations() {
  92.     miss1 = false;
  93.     miss2 = false;
  94.     // Player 1 miss
  95.     if(ballX < 0) {
  96.         score2++;
  97.         ballCurSpeed = ballSpeed;
  98.         if(score2==winScore) {
  99.             gamestate = END;
  100.             return;
  101.         }
  102.         ballX = board1x + boardWidth;
  103.         ballY = board1y - boardHeight/2;
  104.         ballRight = true;
  105.         miss1 = true;
  106.         display();
  107.         Sleep(1000);
  108.     }
  109.    
  110.     // Player 2 miss
  111.     if(ballX > 800) {
  112.         score1++;
  113.         ballCurSpeed = ballSpeed;
  114.         if(score1==winScore) {
  115.             gamestate = END;
  116.             return;
  117.         }
  118.         ballX = board2x - ballSize;
  119.         ballY = board2y - boardHeight/2;
  120.         ballRight = false;
  121.         miss2 = true;
  122.         display();
  123.         Sleep(1000);
  124.     }
  125.    
  126.     // Board-ball collision
  127.     if(ballX < board1x+boardWidth && ballY-ballSize < board1y && ballY > board1y-100) {
  128.         ballRight = !ballRight;
  129.         ballCurSpeed = ballSpeed + ballSpeed * abs( board1y-(boardHeight/2)-ballY ) / (boardHeight/2+ballSize) ;
  130.         ballX += ballCurSpeed;      // Just to be safe after a collision
  131.     }
  132.     if(ballX+ballSize > board2x   && ballY-ballSize < board2y && ballY > board2y-100) {
  133.         ballRight = !ballRight;
  134.         ballCurSpeed = ballSpeed + ballSpeed * abs( board2y-(boardHeight/2)-ballY ) / (boardHeight/2+ballSize) ;
  135.         ballX -= ballCurSpeed;      // Just to be safe after a collision
  136.     }
  137.    
  138.     // Top-bottom collison detection
  139.     if(ballY > height)                      //collide top
  140.         ballUp = !ballUp;
  141.     if(ballY < ballSize)                    //collide bottom
  142.         ballUp = !ballUp;
  143.    
  144.     // Code to move the ball
  145.     if(ballRight)
  146.             ballX += ballCurSpeed;
  147.     else
  148.             ballX -= ballCurSpeed;
  149.            
  150.     if(ballUp)
  151.             ballY += ballCurSpeed;
  152.     else
  153.             ballY -= ballCurSpeed;
  154. }
  155.  
  156. void cube(int x, int y) {
  157.     glColor3f(1,1,1);
  158.     glBegin(GL_POLYGON);
  159.         glVertex2d(x,y);
  160.         glVertex2d(x,y+20);
  161.         glVertex2d(x+20,y+20);
  162.         glVertex2d(x+20,y);
  163.     glEnd();
  164. }
  165.  
  166. void drawTitle() {
  167.     // Dirty way to draw "PONG"
  168.     cube(200,500); cube(220,500); cube(240,500); cube(260,500); cube(260,480); cube(260,460); cube(240,460); cube(220,460);
  169.     cube(200,460); cube(200,480); cube(200,460); cube(200,440); cube(200,420);
  170.    
  171.     cube(300,500); cube(320,500); cube(340,500); cube(360,500); cube(300,480); cube(300,460); cube(300,440); cube(300,420);
  172.     cube(360,480); cube(360,460); cube(360,440); cube(360,420); cube(300,420); cube(320,420); cube(340,420); cube(360,420);
  173.    
  174.     cube(400,500); cube(400,480); cube(400,460); cube(400,440); cube(400,420); cube(420,480); cube(440,460); cube(460,440);
  175.     cube(480,500); cube(480,480); cube(480,460); cube(480,440); cube(480,420);
  176.    
  177.     cube(520,500); cube(540,500); cube(560,500); cube(580,500); cube(520,480); cube(520,460); cube(520,440); cube(520,420);
  178.     cube(540,420); cube(560,420); cube(580,420); cube(580,440); cube(580,460); cube(560,460);
  179.    
  180.     // "by Max"
  181.     drawString(self,530,400);
  182. }
  183.  
  184. void drawString(string s, double x, double y) {
  185.     glRasterPos2f(x,y);
  186.                 for(int i=0 ; i<s.length() ; i++)
  187.                     glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, s.at(i));
  188. }
  189.  
  190. void init (void) {
  191.     glClearColor (0,0,0,0);
  192.     glMatrixMode(GL_PROJECTION);
  193.         glLoadIdentity();
  194.         gluOrtho2D(0,width,0,height);
  195. }
  196.  
  197. void keyOperations() {
  198.     // 13=RETURN, 27=ESC
  199.     if(keyStates[13] && gamestate == MENU) {
  200.         resetState();
  201.         gamestate = GAME;
  202.     }
  203.     if(keyStates[27] && gamestate == MENU)
  204.         exit(0);
  205.        
  206.     if(keyStates[32] && gamestate == GAME)
  207.         gamestate = MENU;
  208.        
  209.     if(keyStates[32] && gamestate == END) {
  210.         resetState();
  211.         gamestate = MENU;
  212.     }
  213.    
  214.     if(keyStates[13] && gamestate == END) {
  215.         resetState();
  216.         gamestate = GAME;
  217.     }
  218.    
  219.     //Move boards
  220.     if(keyStates['q'])
  221.         if(board1y < 600)
  222.             board1y+=boardSpeed;
  223.    
  224.     if(keyStates['a'])
  225.         if(board1y > 100)
  226.             board1y-=boardSpeed;
  227.     if(keyStates['p'])
  228.         if(board2y < 600)
  229.             board2y+=boardSpeed;
  230.    
  231.     if(keyStates['l'])
  232.         if(board2y > 100)
  233.             board2y-=boardSpeed;
  234. }
  235.  
  236. void display (void) {
  237.     glClear(GL_COLOR_BUFFER_BIT);
  238.    
  239.     keyOperations();
  240.        
  241.     switch(gamestate) {
  242.         case MENU:
  243.             // Draw menu
  244.             drawTitle();
  245.            
  246.             drawString(ins1,300,300);
  247.             drawString(ins2,300,250);
  248.             drawString(rule,300,200);
  249.            
  250.             // Blink
  251.             if(timePassed % 2 == 0)
  252.                 drawString(text,290,100);
  253.            
  254.         break;
  255.        
  256.         case GAME:
  257.             if(miss1) drawString(miss,100,50);  //if miss
  258.             if(miss2) drawString(miss,600,50);
  259.            
  260.             ballOperations();           // Move ball
  261.             ball();                     // Draw ball
  262.             board(board1x, board1y);    // Draw boards
  263.             board(board2x, board2y);
  264.            
  265.             glBegin(GL_LINES);          // Center line
  266.                 glVertex2i(400,0);
  267.                 glVertex2i(400,600);
  268.             glEnd();
  269.            
  270.             // Draw score
  271.             {
  272.                 string number;
  273.                 stringstream conv1;
  274.                 conv1<<score1;
  275.                 number = conv1.str();
  276.                 drawString(number,330,550);
  277.                
  278.                 stringstream conv2;
  279.                 conv2<<score2;
  280.                 number = conv2.str();
  281.                 drawString(number,450,550);
  282.             }
  283.         break;
  284.        
  285.         case END:
  286.             // Draw winner
  287.             if(score1 == winScore)
  288.                 drawString(win1,50,400);
  289.             else
  290.                 drawString(win2,550,400);
  291.            
  292.             // Draw options
  293.             drawString(playagain,300,200);
  294.             drawString(reset,300,100);
  295.         break;
  296.     }
  297.    
  298.     glutSwapBuffers();
  299. }
  300.  
  301. void keyboardDown(unsigned char key, int x, int y) {
  302.     keyStates[key] = true;
  303. }
  304.  
  305. void keyboardUp(unsigned char key, int x, int y) {
  306.     keyStates[key] = false;
  307. }
  308.  
  309. void idle() {
  310.     glutPostRedisplay();
  311.     timePassed = static_cast<int>(clock()-start)/CLOCKS_PER_SEC;
  312. }  
  313.  
  314. int main (int argc, char **argv) {
  315.     glutInit(&argc, argv);
  316.     glutInitDisplayMode(GLUT_DOUBLE);
  317.     glutInitWindowSize(width, height);
  318.     glutInitWindowPosition (0, 0);
  319.     glutCreateWindow("Pong by Max");
  320.    
  321.     init();
  322.     glutDisplayFunc(display);
  323.     glutIdleFunc(idle);
  324.     glutKeyboardFunc(keyboardDown);
  325.     glutKeyboardUpFunc(keyboardUp);
  326.     start = clock();
  327.     glutMainLoop();
  328.     return 0;
  329. }
clone this paste RAW Paste Data