Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-----------------------------------------------------------------
- // Game File
- // C++ Source - Pong.cpp - version v2_12 jan 2013
- // Copyright Kevin Hoefman - kevin.hoefman@howest.be
- // http://www.digitalartsandentertainment.be/
- //-----------------------------------------------------------------
- //-----------------------------------------------------------------
- // Include Files
- //-----------------------------------------------------------------
- #include "Pong.h"
- //-----------------------------------------------------------------
- // Defines
- //-----------------------------------------------------------------
- #define GAME_ENGINE (GameEngine::GetSingleton())
- //-----------------------------------------------------------------
- // Pong methods
- //-----------------------------------------------------------------
- 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),
- m_p2movYpos(0), m_p1points(0), m_p2points(0), m_counter(0), d_pspeed(2), m_red(0), m_green(0), m_blue(0),
- m_yguess(0), m_tempX(0), m_tempY(0), m_ballRadius(10), m_height(0), m_width(0)
- {
- // nothing to create
- }
- Pong::~Pong()
- {
- // nothing to destroy
- }
- void Pong::GameInitialize(HINSTANCE hInstance)
- {
- // Set the required values
- AbstractGame::GameInitialize(hInstance); //Something something
- GAME_ENGINE->SetTitle("Wolf maakt Pong"); // sets the game titel
- GAME_ENGINE->RunGameLoop(true); //Makes the game use tick
- // Set the optional values
- m_width = ::GetSystemMetrics(SM_CXSCREEN); //Get the x-resolution of the current active screen
- m_height= ::GetSystemMetrics(SM_CYSCREEN); //Get the y-resolution of the curretn actice screen
- GAME_ENGINE->SetWidth(m_width); //Fill in resolution width
- GAME_ENGINE->SetHeight(m_height); //Fill in resoultion height
- //GAME_ENGINE->SetKeyList(String("QSDZ") + (TCHAR) VK_SPACE);
- }
- void Pong::GameStart()
- {
- AI(); //Start the AI method
- m_p1movXpos = 20; //Sets the left pannel 20 pixels from the left side of the screen
- m_p1movYpos = (GAME_ENGINE->GetHeight()/2); //Sets the left pannel in the middle of the screen
- m_p2movXpos = (GAME_ENGINE->GetWidth()-40); //Sets the right pannel 40 pixels from the right side of the screen
- m_p2movYpos = (GAME_ENGINE->GetHeight()/2); //Sets the right pannel in the middle of the screen
- GAME_ENGINE->GoFullscreen(); //Sets Game in full screen
- // Insert the code that needs to be executed at the start of the game
- }
- void Pong::GameEnd()
- {
- // Insert the code that needs to be executed at the closing of the game
- }
- //void Pong::MouseButtonAction(bool isLeft, bool isDown, int x, int y, WPARAM wParam)
- //{
- // // Insert the code that needs to be executed when the game registers a mouse button action
- //
- // /* Example:
- // if (isLeft == true && isDown == true) // is it a left mouse click?
- // {
- // if ( x > 261 && x < 261 + 117 ) // check if click lies within x coordinates of choice
- // {
- // if ( y > 182 && y < 182 + 33 ) // check if click also lies within y coordinates of choice
- // {
- // GAME_ENGINE->MessageBox("Clicked.");
- // }
- // }
- // }
- // */
- //}
- //
- //void Pong::MouseMove(int x, int y, WPARAM wParam)
- //{
- // // Insert the code that needs to be executed when the mouse pointer moves across the game window
- //
- // /* Example:
- // if ( x > 261 && x < 261 + 117 ) // check if mouse position is within x coordinates of choice
- // {
- // if ( y > 182 && y < 182 + 33 ) // check if mouse position also is within y coordinates of choice
- // {
- // GAME_ENGINE->MessageBox("Da mouse wuz here.");
- // }
- // }
- // */
- //
- //}
- void Pong::CheckKeyboard()
- {
- // Here you can check if a key of choice is held down
- // Is executed once per frame
- if (m_p1movYpos > 10) //Makes sure the pannels isn't going lower then the screen
- {
- if (GAME_ENGINE->IsKeyDown('W')) m_p1movYpos -= d_pspeed; //Make the left pannel go up
- }
- if (m_p2movYpos < GAME_ENGINE->GetHeight() - 80) //Makes sure the pannel isn't going lower then the screen
- {
- if (GAME_ENGINE->IsKeyDown('S')) m_p1movYpos += d_pspeed; //Make the left pannel go down
- }
- }
- //
- //void Pong::KeyPressed(TCHAR cKey)
- //{
- // // DO NOT FORGET to use SetKeyList() !!
- //
- // // Insert the code that needs to be executed when a key of choice is pressed
- // // Is executed as soon as the key is released
- // // You first need to specify the keys that the game engine needs to watch by using the SetKeyList() method
- //
- // /* Example:
- // switch (cKey)
- // {
- // case 'K': case VK_LEFT:
- // MoveBlock(DIR_LEFT);
- // break;
- // case 'L': case VK_DOWN:
- // MoveBlock(DIR_DOWN);
- // break;
- // case 'M': case VK_RIGHT:
- // MoveBlock(DIR_RIGHT);
- // break;
- // case 'A': case VK_UP:
- // RotateBlock();
- // break;
- // case VK_ESCAPE:
- // }
- // */
- //}
- //
- void Pong::GameTick(double deltaTime)
- {
- 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
- {
- d_xspeed *= -1; //Reverses the speed in the x-dir
- m_xpos = m_p1movXpos + 20 + m_ballRadius + 1; //Places the ball infront of the left pannel to prevent skipping at highspeeds
- m_red = rand() % 256; // Randomizes a number for the red vallue
- m_green = rand() % 256; // Randomizes a number for the green vallue
- m_blue = rand() % 256; // Randomizes a number for the blue vallue
- AI(); //Run the AI method
- }
- 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
- {
- d_xspeed *= -1; // Reverses the speed in the x-dir
- m_xpos = m_p2movXpos - m_ballRadius - 1; //Places the ball infront of the rightpannel to prevent skipping at highspeeds
- m_red = rand() % 256; // Randomizes a number for the red vallue
- m_green = rand() % 256; // Randomizes a number for the geen vallue
- m_blue = rand() % 256; // Randmizes a number for the blue vallue
- m_yguess = GAME_ENGINE->GetHeight() / 2; //To make the AI pannel move to the center of the screen after hitting the ball, for realism
- }
- if (m_xpos > GAME_ENGINE->GetWidth() - m_ballRadius ) //Checks if the ball hits the right side of the screen
- {
- d_xspeed *= -1; // Reverses the speed in the x-dir
- 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
- m_p1points++; //Add points to player 1
- }
- if (m_xpos < m_ballRadius) // Checks if the ball hits the left side of the screen
- {
- d_xspeed *= -1; // Reverses the speed in the x-dir
- m_xpos = m_ballRadius + 25; //places the ball infront of the left hand side of the screen to prevent skipping at highspeeds
- m_p2points++; //Add points to player 2
- AI(); //Run the AI method
- }
- if (m_ypos < m_ballRadius) //Checks if the ball hits the top of the screen
- {
- d_yspeed *= -1; //Reverses the speed in the y-dir
- m_ypos = m_ballRadius; //Place the ball just infront of the top of the screen to prevent skipping at highspeeds
- }
- if (m_ypos > (GAME_ENGINE->GetHeight() - m_ballRadius)) //Checks if the ball hits the bottom of the screen
- {
- d_yspeed *=-1; //Reverses the speed in the y-dir
- m_ypos = GAME_ENGINE->GetHeight() - m_ballRadius; //Place the ball just infront to bottom, to prevent skipping at highspeeds
- }
- m_xpos += d_xspeed; //Move the ball in the X direction with the current speed;
- m_ypos += d_yspeed; //Move the ball in the Y direction with the current speed;
- if (m_counter == 2000) // Activates the Frist speed setting after 2000 ticks
- {
- d_xspeed *= 1.5; //multiply the speed of the ball in the x-dir by 1.5
- d_yspeed *= 1.5; //multiply the speed of the ball in the y-dir by 1.5
- d_pspeed *= 1.5; //multiply the speed of the pannels by 1.5
- }
- if (m_counter == 4000) // Activates the Second speed setting after 4000 ticks
- {
- d_xspeed *= 2; //multiply the speed of the ball in the x-dir by 1.5
- d_yspeed *= 1.5; //multiply the speed of the ball in the y-dir by 1.5
- d_pspeed *= 1.5; //multiply the speed of the pannels by 1.5
- }
- if (m_counter == 8000) // Activates the Third speed setting after 8000 ticks
- {
- d_xspeed *= 2; //multiply the speed of the ball in the x-dir by 2
- d_yspeed *= 2; //multiply the speed of the ball in the y-dir by 2
- d_pspeed *= 2; //multiply the speed of the pannels by 2
- }
- m_counter++; //Adds to the Global counter, to keep track of the length of the game, and for the increase in speed.
- AIMove(); //Call AI mover, System that makes the AI panel move to the calculated end position of the ball.
- //Insert the code that needs to be executed, EXCEPT for paint commands (see next method)
- }
- void Pong::AI()
- {
- //THis method is a mathematical calculation to predict the y-position of the ball before the actueal ball reaches that point
- //Used variables are m_tempX (used to calculate the predicted ball path) m_tempY (used to calculate the predicted ball path)
- //m_guess (The output predicted y-pos for the ball, then used in method AIMove to physically move the pannel)
- //The Ai calculates the end point of the ball, keeping inmind the ball could 'possible' bounce a wall
- //To explain this process as best as possible im gonne describe the precalculated ball as virtual ball, and normal ball as physical ball
- m_tempX = m_xpos; //Set the virtual balls x-pos equal to physical ball x-pos
- m_tempY = m_ypos; //Set the virtual balls y-pos equal to physical ball y-pos
- if (d_yspeed > 0) // Checks if the ball is going in a y increasing motion (going down the screen)
- {
- while (m_tempY < (GAME_ENGINE->GetHeight() - m_ballRadius) && m_tempX < (GAME_ENGINE->GetWidth() - m_ballRadius)) //While the position of the virtual ball
- { // is not hitting the bottom of the screen and not hitting the right side of the screen
- m_tempX += d_xspeed; //Add the current x-speed to the virtual x-pos
- m_tempY += d_yspeed; //Add the current y-speed to the virtual y-pos
- if (m_tempY >= (GAME_ENGINE->GetHeight() - m_ballRadius) && m_tempX < (GAME_ENGINE->GetWidth() - m_ballRadius))//Looks for the moment when the virtual
- { //ball hits the bottom of the screen
- while (m_tempY > 0 && m_tempX < (GAME_ENGINE->GetWidth() - m_ballRadius)) // When the bottom of the screen has been reached it reverses the speed
- { // then continues callculation to find the moment when the virtual ball hits the right side of the screen
- m_tempX += d_xspeed; //Add the current x-speed to the virtual x-pos
- m_tempY -= d_yspeed; //substract the current y-speed to the virtual y-pos
- if (m_tempX >= (GAME_ENGINE->GetWidth() - m_ballRadius)) //checks if the virtual ball hits the right side of the screen
- {
- m_yguess = m_tempY; //Save the y-pos of the virtual ball (at the right side of the screen) in the m_yguess variable
- }
- }
- }
- else if (m_tempX >= (GAME_ENGINE->GetWidth() - m_ballRadius)) //incase the virtual ball doesn't hit a bottom wall
- {
- m_tempX = m_tempY; //Save the end y-pos of the virtual ball in the variable m_guess
- }
- }
- }
- else if (d_yspeed < 0) //Checks if the ball is going in a y decreasing motion (going up the screen)
- {
- while (m_tempY >= m_ballRadius && m_tempX < (GAME_ENGINE->GetWidth() - m_ballRadius)) //While the position of the virtual ball is not hitting the top
- { //of the screen and not hitting the right side of the screen
- m_tempX += d_xspeed; //Add the current x-speed to the virtual x-pos
- m_tempY += d_yspeed; //Add the current y-speed to the virtual y-pos
- 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
- {
- while (m_tempY < (GAME_ENGINE->GetHeight() - m_ballRadius) && m_tempX < (GAME_ENGINE->GetWidth() - m_ballRadius)) //When the top o the screen has
- { //been reached it reverses the speed, then continues calculating to find the moment when the virtual hits the right side of the screen
- m_tempX += d_xspeed; //Add the current x-speed to the virtual x-pos
- m_tempY -= d_yspeed; //Substract the current y-speed to the virtual y-pos
- if (m_tempX >= (GAME_ENGINE->GetWidth() - m_ballRadius)) //checks if the virtual ball hits the right side of the screen
- {
- m_yguess = m_tempY; //Sace the y-pos of the virtual ball (at the right side of the screen) in the m_yguess variable
- }
- }
- }
- else if (m_tempX >= (GAME_ENGINE->GetWidth() - m_ballRadius)) //incase the virtual ball doesn't hit a top wall
- {
- m_yguess = m_tempY; //Save the end y-pos of the virtual ball in the variable m_guess
- }
- }
- }
- }
- void Pong::AIMove()
- {
- // This method will take care of moving the AIpallet to the precalculated m_yguess location
- if (m_p2movYpos + 35 < m_yguess) //Checks if the AI pallet is above the m_yguess location
- {
- m_p2movYpos += d_pspeed; //if so, move the pannel down with the current d_pspeed (pannel speed)
- }
- else if (m_p2movYpos + 35 > m_yguess) //Checks if the AI paller is lower then the m_yguess location
- {
- m_p2movYpos -= d_pspeed; //if so, move the pannel up with the current d_pspeed (pannel speed)
- }
- }
- void Pong::GamePaint(RECT rect)
- {
- GAME_ENGINE->DrawSolidBackground(0,0,0); //Makes the background black
- GAME_ENGINE->SetColor(COLOR(255,255,255)); //Set the draw collor to white
- GAME_ENGINE->DrawLine((GAME_ENGINE->GetWidth()/2),0,(GAME_ENGINE->GetWidth()/2),GAME_ENGINE->GetHeight()); //Draw verticale line in the middle of the screen
- GAME_ENGINE->SetColor(COLOR(m_red, m_green, m_blue)); //Change the collor to the randomized vallues (m_red, m_green, m_blue)
- GAME_ENGINE->FillEllipse(m_xpos, m_ypos, m_ballRadius, m_ballRadius); //Draws the collor of the inner circle
- GAME_ENGINE->SetColor(COLOR(255,255,255)); //Set the collor to white
- 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
- GAME_ENGINE->DrawRect(m_p1movXpos,m_p1movYpos,(m_p1movXpos + 20),(m_p1movYpos + 70)); //Draw the player pannel
- GAME_ENGINE->DrawRect(m_p2movXpos,m_p2movYpos,(m_p2movXpos + 20),(m_p2movYpos + 70)); //Draw the AI pannel
- GAME_ENGINE->DrawString(String("Score Player1: ") + m_p1points,20,20,-1,-1); //Draw the player 1 score text
- GAME_ENGINE->DrawString(String("Score Player2: ") + m_p2points,20,40,-1,-1); //Draw the player 2 score text
- GAME_ENGINE->DrawString(String("Counter: ") + m_counter,20,60,-1,-1); //Draw the counter debug text
- GAME_ENGINE->DrawString(String("Xpos: ") + m_xpos,20,80); //Draw the x-pos of the ball for debug
- GAME_ENGINE->DrawString(String("Ypos: ") + m_ypos,20,100); //Draw the y-pos of the ball for debug
- // Insert the code that needs to be executed each time a new frame needs to be drawn to the screen
- // Technical note: engine uses double buffering when the gamecycle is running
- }
- void Pong::CallAction(Caller* callerPtr)
- {
- // Insert the code that needs to be executed when a Caller has to perform an action
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement