Advertisement
Guest User

Pong

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