Advertisement
ToastyStoemp

Pong CPlusPlus File by Wolf Van Herreweghe

Sep 12th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.89 KB | None | 0 0
  1. //-----------------------------------------------------------------
  2. // Game File
  3. // C++ Source - Pong.cpp - version v2_12 jan 2013
  4. // Copyright Kevin Hoefman - kevin.hoefman@howest.be
  5. // http://www.digitalartsandentertainment.be/
  6. //-----------------------------------------------------------------
  7.  
  8. //-----------------------------------------------------------------
  9. // Include Files
  10. //-----------------------------------------------------------------
  11. #include "Pong.h"                                                                              
  12.  
  13. //-----------------------------------------------------------------
  14. // Defines
  15. //-----------------------------------------------------------------
  16. #define GAME_ENGINE (GameEngine::GetSingleton())
  17.  
  18. //-----------------------------------------------------------------
  19. // Pong methods                                                                            
  20. //-----------------------------------------------------------------
  21.  
  22. Pong::Pong():m_xpos(20), m_ypos(20), d_xspeed(3.0), d_yspeed(2.0), m_p1movXpos(20), m_p1movYpos(0), m_p2movXpos(0),
  23.     m_p2movYpos(0), m_p1points(0), m_p2points(0), m_counter(0), d_pspeed(2), m_red(0), m_green(0), m_blue(0),
  24.     m_yguess(0), m_tempX(0), m_tempY(0), m_ballRadius(10), m_height(0), m_width(0)
  25. {
  26.     // nothing to create
  27. }
  28.  
  29. Pong::~Pong()                                                                                      
  30. {
  31.     // nothing to destroy
  32. }
  33.  
  34. void Pong::GameInitialize(HINSTANCE hInstance)         
  35. {
  36.     // Set the required values
  37.     AbstractGame::GameInitialize(hInstance); //Something something
  38.     GAME_ENGINE->SetTitle("Wolf maakt Pong"); // sets the game titel   
  39.     GAME_ENGINE->RunGameLoop(true); //Makes the game use tick
  40.    
  41.     // Set the optional values
  42.     m_width = ::GetSystemMetrics(SM_CXSCREEN); //Get the x-resolution of the current active screen
  43.     m_height= ::GetSystemMetrics(SM_CYSCREEN); //Get the y-resolution of the curretn actice screen
  44.     GAME_ENGINE->SetWidth(m_width); //Fill in resolution width
  45.     GAME_ENGINE->SetHeight(m_height); //Fill in resoultion height
  46.     //GAME_ENGINE->SetKeyList(String("QSDZ") + (TCHAR) VK_SPACE);
  47. }
  48.  
  49. void Pong::GameStart()
  50. {
  51.     AI(); //Start the AI method
  52.     m_p1movXpos = 20; //Sets the left pannel 20 pixels from the left side of the screen
  53.     m_p1movYpos = (GAME_ENGINE->GetHeight()/2); //Sets the left pannel in the middle of the screen
  54.     m_p2movXpos = (GAME_ENGINE->GetWidth()-40); //Sets the right pannel 40 pixels from the right side of the screen
  55.     m_p2movYpos = (GAME_ENGINE->GetHeight()/2); //Sets the right pannel in the middle of the screen
  56.     GAME_ENGINE->GoFullscreen(); //Sets Game in full screen
  57.     // Insert the code that needs to be executed at the start of the game
  58. }
  59.  
  60. void Pong::GameEnd()
  61. {
  62.     // Insert the code that needs to be executed at the closing of the game
  63. }
  64.  
  65.  
  66. //void Pong::MouseButtonAction(bool isLeft, bool isDown, int x, int y, WPARAM wParam)
  67. //{
  68. //  // Insert the code that needs to be executed when the game registers a mouse button action
  69. //
  70. //  /* Example:
  71. //  if (isLeft == true && isDown == true) // is it a left mouse click?
  72. //  {  
  73. //      if ( x > 261 && x < 261 + 117 ) // check if click lies within x coordinates of choice
  74. //      {
  75. //          if ( y > 182 && y < 182 + 33 ) // check if click also lies within y coordinates of choice
  76. //          {
  77. //              GAME_ENGINE->MessageBox("Clicked.");
  78. //          }
  79. //      }
  80. //  }
  81. //  */
  82. //}
  83. //
  84. //void Pong::MouseMove(int x, int y, WPARAM wParam)
  85. //{
  86. //  // Insert the code that needs to be executed when the mouse pointer moves across the game window
  87. //
  88. //  /* Example:
  89. //  if ( x > 261 && x < 261 + 117 ) // check if mouse position is within x coordinates of choice
  90. //  {
  91. //      if ( y > 182 && y < 182 + 33 ) // check if mouse position also is within y coordinates of choice
  92. //      {
  93. //          GAME_ENGINE->MessageBox("Da mouse wuz here.");
  94. //      }
  95. //  }
  96. //  */
  97. //
  98. //}
  99.  
  100. void Pong::CheckKeyboard()
  101. {  
  102.     // Here you can check if a key of choice is held down
  103.     // Is executed once per frame
  104.     if (m_p1movYpos > 10) //Makes sure the pannels isn't going lower then the screen
  105.     {
  106.         if (GAME_ENGINE->IsKeyDown('W')) m_p1movYpos -= d_pspeed; //Make the left pannel go up
  107.     }
  108.     if (m_p2movYpos < GAME_ENGINE->GetHeight() - 80) //Makes sure the pannel isn't going lower then the screen
  109.     {
  110.         if (GAME_ENGINE->IsKeyDown('S')) m_p1movYpos += d_pspeed; //Make the left pannel go down
  111.     }
  112.    
  113. }
  114. //
  115. //void Pong::KeyPressed(TCHAR cKey)
  116. //{
  117. //  // DO NOT FORGET to use SetKeyList() !!
  118. //
  119. //  // Insert the code that needs to be executed when a key of choice is pressed
  120. //  // Is executed as soon as the key is released
  121. //  // You first need to specify the keys that the game engine needs to watch by using the SetKeyList() method
  122. //
  123. //  /* Example:
  124. //  switch (cKey)
  125. //  {
  126. //  case 'K': case VK_LEFT:
  127. //      MoveBlock(DIR_LEFT);
  128. //      break;
  129. //  case 'L': case VK_DOWN:
  130. //      MoveBlock(DIR_DOWN);
  131. //      break;
  132. //  case 'M': case VK_RIGHT:
  133. //      MoveBlock(DIR_RIGHT);
  134. //      break;
  135. //  case 'A': case VK_UP:
  136. //      RotateBlock();
  137. //      break;
  138. //  case VK_ESCAPE:
  139. //  }
  140. //  */
  141. //}
  142. //
  143. void Pong::GameTick(double deltaTime)
  144. {
  145.     if (m_xpos - m_ballRadius < m_p1movXpos + 20 && m_ypos > m_p1movYpos && m_ypos < m_p1movYpos + 70) //Checks if the ball hits the lef side pannel
  146.     {
  147.         d_xspeed *= -1; //Reverses the speed in the x-dir
  148.         m_xpos = m_p1movXpos + 20 + m_ballRadius + 1; //Places the ball infront of the left pannel to prevent skipping at highspeeds
  149.         m_red = rand() % 256; // Randomizes a number for the red vallue
  150.         m_green = rand() % 256; // Randomizes a number for the green vallue
  151.         m_blue = rand() % 256; // Randomizes a number for the blue vallue
  152.         AI(); //Run the AI method
  153.     }
  154.         if (m_xpos + m_ballRadius > m_p2movXpos && m_ypos > m_p2movYpos && m_ypos < m_p2movYpos + 70) //Checks if the ball hits the right side pannel
  155.     {
  156.         d_xspeed *= -1; // Reverses the speed in the x-dir
  157.         m_xpos = m_p2movXpos - m_ballRadius - 1; //Places the ball infront of the rightpannel to prevent skipping at highspeeds
  158.         m_red = rand() % 256; // Randomizes a number for the red vallue
  159.         m_green = rand() % 256; // Randomizes a number for the geen vallue
  160.         m_blue = rand() % 256; // Randmizes a number for the blue vallue
  161.         m_yguess = GAME_ENGINE->GetHeight() / 2; //To make the AI pannel move to the center of the screen after hitting the ball, for realism
  162.     }
  163.  
  164.     if (m_xpos > GAME_ENGINE->GetWidth() - m_ballRadius ) //Checks if the ball hits the right side of the screen
  165.     {
  166.         d_xspeed *= -1; // Reverses the speed in the x-dir
  167.         m_xpos = GAME_ENGINE->GetWidth() - m_ballRadius -25; // places the ball infront of the right hand side of the screen to prevent skipping at highspeeds
  168.         m_p1points++;   //Add points to player 1
  169.     }
  170.     if (m_xpos < m_ballRadius) // Checks if the ball hits the left side of the screen
  171.     {
  172.         d_xspeed *= -1; // Reverses the speed in the x-dir
  173.         m_xpos = m_ballRadius + 25; //places the ball infront of the left hand side of the screen to prevent skipping at highspeeds
  174.         m_p2points++;   //Add points to player 2
  175.         AI(); //Run the AI method
  176.     }
  177.  
  178.  
  179.     if (m_ypos < m_ballRadius) //Checks if the ball hits the top of the screen
  180.     {
  181.         d_yspeed *= -1; //Reverses the speed in the y-dir
  182.         m_ypos = m_ballRadius; //Place the ball just infront of the top of the screen to prevent skipping at highspeeds
  183.     }
  184.  
  185.     if (m_ypos > (GAME_ENGINE->GetHeight() - m_ballRadius)) //Checks if the ball hits the bottom of the screen
  186.     {
  187.         d_yspeed *=-1;  //Reverses the speed in the y-dir
  188.         m_ypos = GAME_ENGINE->GetHeight() - m_ballRadius;   //Place the ball just infront to bottom, to prevent skipping at highspeeds
  189.     }
  190.  
  191.  
  192.     m_xpos += d_xspeed; //Move the ball in the X direction with the current speed;
  193.     m_ypos += d_yspeed; //Move the ball in the Y direction with the current speed;
  194.  
  195.  
  196.     if (m_counter == 2000)  // Activates the Frist speed setting after 2000 ticks
  197.     {
  198.         d_xspeed *= 1.5; //multiply the speed of the ball in the x-dir by 1.5
  199.         d_yspeed *= 1.5; //multiply the speed of the ball in the y-dir by 1.5
  200.         d_pspeed *= 1.5; //multiply the speed of the pannels by 1.5
  201.     }
  202.     if (m_counter == 4000)  // Activates the Second speed setting after 4000 ticks
  203.     {
  204.         d_xspeed *= 2;   //multiply the speed of the ball in the x-dir by 1.5
  205.         d_yspeed *= 1.5; //multiply the speed of the ball in the y-dir by 1.5
  206.         d_pspeed *= 1.5; //multiply the speed of the pannels by 1.5
  207.     }
  208.     if (m_counter == 8000)  // Activates the Third speed setting after 8000 ticks
  209.     {
  210.         d_xspeed *= 2; //multiply the speed of the ball in the x-dir by 2
  211.         d_yspeed *= 2; //multiply the speed of the ball in the y-dir by 2
  212.         d_pspeed *= 2; //multiply the speed of the pannels by 2
  213.     }
  214.  
  215.     m_counter++; //Adds to the Global counter, to keep track of the length of the game, and for the increase in speed.
  216.  
  217.     AIMove(); //Call AI mover, System that makes the AI panel move to the calculated end position of the ball.
  218.     //Insert the code that needs to be executed, EXCEPT for paint commands (see next method)
  219. }
  220.  
  221. void Pong::AI()
  222. {
  223.  
  224.     //THis method is a mathematical calculation to predict the y-position of the ball before the actueal ball reaches that point
  225.     //Used variables are m_tempX (used to calculate the predicted ball path) m_tempY (used to calculate the predicted ball path)
  226.     //m_guess (The output predicted y-pos for the ball, then used in method AIMove to physically move the pannel)
  227.  
  228.     //The Ai calculates the end point of the ball, keeping inmind the ball could 'possible' bounce a wall
  229.     //To explain this process as best as possible im gonne describe the precalculated ball as virtual ball, and normal ball as physical ball
  230.  
  231.     m_tempX = m_xpos;   //Set the virtual balls x-pos equal to physical ball x-pos
  232.     m_tempY = m_ypos;   //Set the virtual balls y-pos equal to physical ball y-pos
  233.     if (d_yspeed > 0) // Checks if the ball is going in a y increasing motion (going down the screen)
  234.     {
  235.         while (m_tempY < (GAME_ENGINE->GetHeight() - m_ballRadius) && m_tempX < (GAME_ENGINE->GetWidth() - m_ballRadius)) //While the position of the virtual ball
  236.         { // is not hitting the bottom of the screen and not hitting the right side of the screen
  237.             m_tempX += d_xspeed; //Add the current x-speed to the virtual x-pos
  238.             m_tempY += d_yspeed; //Add the current y-speed to the virtual y-pos
  239.             if (m_tempY >= (GAME_ENGINE->GetHeight() - m_ballRadius) && m_tempX < (GAME_ENGINE->GetWidth() - m_ballRadius))//Looks for the moment when the virtual
  240.             { //ball hits the bottom of the screen
  241.                 while (m_tempY > 0 && m_tempX < (GAME_ENGINE->GetWidth() - m_ballRadius)) // When the bottom of the screen has been reached it reverses the speed
  242.                 { // then continues callculation to find the moment when the virtual ball hits the right side of the screen
  243.                     m_tempX += d_xspeed; //Add the current x-speed to the virtual x-pos
  244.                     m_tempY -= d_yspeed; //substract the current y-speed to the virtual y-pos
  245.                     if (m_tempX >= (GAME_ENGINE->GetWidth() - m_ballRadius)) //checks if the virtual ball hits the right side of the screen
  246.                     {
  247.                         m_yguess = m_tempY; //Save the y-pos of the virtual ball (at the right side of the screen) in the m_yguess variable
  248.                     }
  249.                 }
  250.             }
  251.             else if (m_tempX >= (GAME_ENGINE->GetWidth() - m_ballRadius)) //incase the virtual ball doesn't hit a bottom wall
  252.             {
  253.                 m_tempX = m_tempY; //Save the end y-pos of the virtual ball in the variable m_guess
  254.             }
  255.         }
  256.     }
  257.     else if (d_yspeed < 0) //Checks if the ball is going in a y decreasing motion (going up the screen)
  258.     {
  259.         while (m_tempY >= m_ballRadius && m_tempX < (GAME_ENGINE->GetWidth() - m_ballRadius)) //While the position of the virtual ball is not hitting the top
  260.         { //of the screen and not hitting the right side of the screen
  261.             m_tempX += d_xspeed; //Add the current x-speed to the virtual x-pos
  262.             m_tempY += d_yspeed; //Add the current y-speed to the virtual y-pos
  263.             if (m_tempY <= m_ballRadius && m_tempX < (GAME_ENGINE->GetWidth() - m_ballRadius)) //Looks for the moment when the virtual ball hits the top of the screen
  264.             {
  265.                 while (m_tempY < (GAME_ENGINE->GetHeight() - m_ballRadius) && m_tempX < (GAME_ENGINE->GetWidth() - m_ballRadius)) //When the top o the screen has
  266.                 { //been reached it reverses the speed, then continues calculating to find the moment when the virtual hits the right side of the screen
  267.                     m_tempX += d_xspeed; //Add the current x-speed to the virtual x-pos
  268.                     m_tempY -= d_yspeed; //Substract the current y-speed to the virtual y-pos
  269.                     if (m_tempX >= (GAME_ENGINE->GetWidth() - m_ballRadius)) //checks if the virtual ball hits the right side of the screen
  270.                     {
  271.                         m_yguess = m_tempY; //Sace the y-pos of the virtual ball (at the right side of the screen) in the m_yguess variable
  272.                     }
  273.                 }
  274.             }
  275.             else if (m_tempX >= (GAME_ENGINE->GetWidth() - m_ballRadius)) //incase the virtual ball doesn't hit a top wall
  276.             {
  277.                 m_yguess = m_tempY; //Save the end y-pos of the virtual ball in the variable m_guess
  278.             }
  279.         }
  280.     }
  281. }
  282.  
  283. void Pong::AIMove()
  284. {
  285.     // This method will take care of moving the AIpallet to the precalculated m_yguess location
  286.  
  287.     if (m_p2movYpos + 35 < m_yguess) //Checks if the AI pallet is above the m_yguess location
  288.     {
  289.         m_p2movYpos += d_pspeed; //if so, move the pannel down with the current d_pspeed (pannel speed)
  290.     }
  291.     else if (m_p2movYpos + 35 > m_yguess) //Checks if the AI paller is lower then the m_yguess location
  292.     {
  293.         m_p2movYpos -= d_pspeed; //if so, move the pannel up with the current d_pspeed (pannel speed)
  294.     }
  295. }
  296.  
  297. void Pong::GamePaint(RECT rect)
  298. {
  299.     GAME_ENGINE->DrawSolidBackground(0,0,0); //Makes the background black
  300.     GAME_ENGINE->SetColor(COLOR(255,255,255)); //Set the draw collor to white
  301.     GAME_ENGINE->DrawLine((GAME_ENGINE->GetWidth()/2),0,(GAME_ENGINE->GetWidth()/2),GAME_ENGINE->GetHeight()); //Draw verticale line in the middle of the screen
  302.     GAME_ENGINE->SetColor(COLOR(m_red, m_green, m_blue)); //Change the collor to the randomized vallues (m_red, m_green, m_blue)
  303.     GAME_ENGINE->FillEllipse(m_xpos, m_ypos, m_ballRadius, m_ballRadius); //Draws the collor of the inner circle
  304.     GAME_ENGINE->SetColor(COLOR(255,255,255)); //Set the collor to white
  305.     GAME_ENGINE->DrawEllipse(m_xpos,m_ypos,m_ballRadius,m_ballRadius); //draw circle at the given x-pos and y-pos with the given m_ballRadius
  306.     GAME_ENGINE->DrawRect(m_p1movXpos,m_p1movYpos,(m_p1movXpos + 20),(m_p1movYpos + 70)); //Draw the player pannel
  307.     GAME_ENGINE->DrawRect(m_p2movXpos,m_p2movYpos,(m_p2movXpos + 20),(m_p2movYpos + 70)); //Draw the AI pannel
  308.  
  309.     GAME_ENGINE->DrawString(String("Score Player1: ") + m_p1points,20,20,-1,-1); //Draw the player 1 score text
  310.     GAME_ENGINE->DrawString(String("Score Player2: ") + m_p2points,20,40,-1,-1); //Draw the player 2 score text
  311.     GAME_ENGINE->DrawString(String("Counter: ") + m_counter,20,60,-1,-1); //Draw the counter debug text
  312.     GAME_ENGINE->DrawString(String("Xpos: ") + m_xpos,20,80); //Draw the x-pos of the ball for debug
  313.     GAME_ENGINE->DrawString(String("Ypos: ") + m_ypos,20,100); //Draw the y-pos of the ball for debug
  314.     // Insert the code that needs to be executed each time a new frame needs to be drawn to the screen
  315.     // Technical note: engine uses double buffering when the gamecycle is running
  316.  
  317. }
  318.  
  319. void Pong::CallAction(Caller* callerPtr)
  320. {
  321.     // Insert the code that needs to be executed when a Caller has to perform an action
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement