Advertisement
diamondandplatinum3

Pong ~ C++

May 27th, 2012
1,084
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 66.08 KB | None | 0 0
  1. //This is the first game I ever made in any programming language, so expect it not to be very good code.
  2.  
  3.  
  4. // Pong.cpp : Defines the entry point for the console application.
  5. //
  6.  
  7. #include <Windows.h>
  8. #include "GL_Functions.h"
  9. #include "pctimer.h"
  10. #include <string>
  11. #include <cmath>
  12. #include <time.h>
  13. #include <crtdbg.h>
  14.  
  15.  
  16.  
  17.  
  18. //=======================================================================================
  19. //          Insert Scores
  20. //=======================================================================================
  21.  
  22. int HighScoreTable[10]; //Creates the HighScoreTable integer
  23.  
  24. void InsertScore( int iHighScore) //Creates the 'InsertScore' function to be used later on, to add to the high score table.
  25.     //The int iHighScore function is used to store the players scores when the function is called.
  26.  
  27. { //begins the scope for the function.
  28.  
  29.     int iPlayerScore = iHighScore;  //copies iHighScore (Now the players previous score) into iPlayerScore.
  30.     int iTemp = 0; //creates the iTemp Variable and sets it to nil.
  31.    
  32.     for(int i = 0; i < 10; ++i) //creates the 'for loop', also creates the 'i' Variable and tests each loop to make sure that 'i' is less than 10, then adds +1 to 'i' each time until it reaches 10.
  33.     {
  34.         if(iPlayerScore > HighScoreTable[i]) //Tests if Players score is higher than the current high score, if true, it compiles the if statement, if false, it skips the if statement and runs through to 'for loop' again, testing the next highest highscore and continuing.
  35.         {
  36.             iTemp = HighScoreTable[i]; //If the if statement returns true, 'iTemp' copies the current highscore
  37.             HighScoreTable[i] = iPlayerScore; //The highscore that was previously copied is replaced with the players highscore
  38.             iPlayerScore = iTemp; // Then the players score becomes the previous highscore the 'iTemp' was holding.
  39.  
  40.             //And then the 'for loop' loops around again testing the temp score we just placed in 'iPlayerScore' to see if it's still higher than any other highscore in the list.
  41.         }
  42.     }
  43. }//End of the Functions Scope.
  44.  
  45. void GameState_P1_VS_AI ()
  46. {
  47.     int Ball                = LoadTexture("./images/Shine_Sprite.png"); //Creates the Ball Sprite for the game.
  48.     int Game_BackGround     = LoadTexture("./images/Nintendo_Logo.png"); //Creates the background Image for the game.
  49.     int Gamecube            = LoadTexture("./images/gamecube.png"); //Creates the Gamecube Sprite (Which will be our left paddle) for the game.
  50.     int Wii                 = LoadTexture("./images/wii.png"); //Creates the Wii Sprite (Which will be used as our right paddle) for the game.
  51.  
  52.     float Ballx = 512; //Creates the balls 'X Position', 512 is the middle of the screen, so it makes the game fair.
  53.     float Bally = 512; //Creates the balls 'Y Position', 512 starts the ball on the lower end of the screen and aims to make the start a little challenging.  
  54.  
  55.     float BallSpeedx = 3; //Creates the 'X Speed' of the ball, 'X Speed' controls the balls left and right movement.
  56.     float BallSpeedy = 3; //Creates the 'Y Speed' of the ball, 'Y Speed' controls the balls up and down movements.
  57.  
  58.     float Gamecubex = 30; //Sets the left paddles original x position to be just away from the left wall. Thus leaving enough space for the ball to hit the wall behind it.
  59.     float Gamecubey = 200; //Sets the left paddles original y position to start near the top of the screen, to be ready to hit the ball.
  60.  
  61.     float Wiix = 920; //Sets the right paddles original x postion to be just away from the right wall. Thus leaving enough space for the ball to hit the wall behind it.
  62.     float Wiiy = 400; //Sets the right paddles original y position to start near the bottom of the screen, to be ready to hit the ball.
  63.  
  64.     int iP1Score = 0; //Creates and sets Player 1's initial score as zero.
  65.     int iP2Score = 0; //Creates and sets Player 2's initial score as zero.
  66.  
  67.     int iP1RoundScore = 0; //Creates and sets Player 1's initial Round Score as zero.
  68.     int iP2RoundScore = 0; //Creates and sets Player 2's initial round score as zero.
  69.  
  70.     int iFrameCount = 0; //Creates and sets the immediate Frame waiting count to zero.
  71.     int iFrameWaitAmount = 128; //Creates and sets the FrameWaitAmount to 128 frames.
  72.     bool bWaiting = false; //Sets the frame wait condition to false for the beginning of the game.
  73.  
  74.  
  75.  
  76.     do
  77.     { //a 'do while' loop will always do whatever functions are inside of its scope at least once. However, it will continue to loop through those functions if the conditions for the 'while()' are met.
  78.        
  79.         ClearScreen(); //Refreshes the screen from any previous sprites that were on it.
  80.  
  81.         if( bWaiting == true) //if bWaiting has been set to true then
  82.         {
  83.             iFrameCount += 1; //'iFrameCount' will keep adding +1 to itself until it becomes higher than 'iFrameWaitAmount'
  84.             if( iFrameCount > iFrameWaitAmount) //Once iFrameCount' is higher than 'iFrameWaitAmount
  85.             {
  86.                 bWaiting = false; //The boolean 'bWaiting' is set back to false and
  87.                 iFrameCount = 0; //iFrameCount is set back to zero. The game will now continue.
  88.             }
  89.         }
  90.    
  91.         if( bWaiting == false) //If bWaiting is set to false (By default it is)
  92.         {
  93.             Ballx += BallSpeedx; //Ballx will add BallSpeedx to itself, thus moving it's position.
  94.             Bally += BallSpeedy; //Bally will add BallSpeedy to itself, thus moving it's position.
  95.         }
  96.    
  97.         char cP1ScoreString[6]; //Creates the char array for Player 1's score, since the array is of a six value, the highest amount of points that the player could get is 999,999.
  98.         _itoa_s(iP1Score, cP1ScoreString, 10); //converts the first integer into a string.
  99.         char cP2ScoreString[6]; //Creates the char array for Player 1's score, since the array is of a six value, the highest amount of points that the player could get is 999,999.
  100.         _itoa_s(iP2Score, cP2ScoreString, 10); //converts the first integer into a string.
  101.    
  102.         char cP1RoundScoreString[6]; //Creates the char array for Player 1's round score, the actual score can only go up to six and no futher.
  103.         _itoa_s(iP1RoundScore, cP1RoundScoreString, 10); //converts the first integer into a string.
  104.         char cP2RoundScoreString[6]; //Creates the char array for Player 2's round score, the actual score can only go up to six and no futher.
  105.         _itoa_s(iP2RoundScore, cP2RoundScoreString, 10); //converts the first integer into a string.
  106.    
  107.         //==============================================================================================
  108.         // Draw Sprites
  109.         //==============================================================================================
  110.    
  111.         DrawSprite(Game_BackGround, 0, 0, 1028, 768); //Sets the game background image for use. The '0, 0, 1028, 768' stands for X pos, Y pos, width, height. The graphic is set on the top left of the screen and its width and height are adjusted to cover the whole screen.
  112.    
  113.         DrawString(cP1ScoreString, 150, 50, 2); //Sets Player 1's Score to be displayed in the top left corner.
  114.         DrawString(cP2ScoreString, 780, 50, 2); //Sets Player 2's Score to be displayed in the top right corner.
  115.         DrawString(cP1RoundScoreString, 150, 120, 6); // Sets Player 1's Round Score to be displayed just under their total score.
  116.         DrawString(cP2RoundScoreString, 780, 120, 6); //Sets Player 2's Round Score to be displayed just under their total score.
  117.    
  118.    
  119.         DrawSprite(Ball, Ballx , Bally, 30, 30); //Sets the ball position in the middle of the screen but lower, and draws its size to be 30 pixels wide and 30 pixels height.
  120.         DrawSprite(Gamecube, Gamecubex, Gamecubey, 100, 100); //Sets the left paddles position to the top left of the screen, ready to be used.
  121.         DrawSprite(Wii, Wiix, Wiiy, 100, 100); //Sets the right paddles position to the bottom right of the screen.
  122.    
  123.    
  124.         //================================================================================================
  125.         //      Move the Ball & Add the Scores
  126.         //=================================================================================================
  127.    
  128.         if (Ballx > 1027 ) //If the ball makes it to the x value of 1027 (Which is one pixel away from the right wall)
  129.         {
  130.             Ballx = 512; //The ball is set back to the middle
  131.             Bally = 512; //The Ball is set back to it's lowered position
  132.             BallSpeedx = -3; //BallSpeedx results in a negative number to make it begin to move left instead of right.
  133.             bWaiting = true; //The boolean variable 'bWaiting' is set to true so that the ball will wait a little bit before the game starts again.
  134.             iP1RoundScore = iP1RoundScore +1; //And finally Player 1's Round Score is increased by one.
  135.         }
  136.    
  137.         if (Ballx < 1) //If the ball makes it to the x value of 1 (Which is one pixel away from the left wall)
  138.         {
  139.             Ballx = 512; //The ball is set back to the middle
  140.             Bally = 512; //The Ball is set back to it's lowered position
  141.             BallSpeedx = 3; //BallSpeedx results in a positive number to make it begin to move right instead of left.
  142.             bWaiting = true; //The boolean variable 'bWaiting' is set to true so that the ball will wait a little bit before the game starts again.
  143.             iP2RoundScore = iP2RoundScore +1; //And finally Player 2's Round Score is increased by one.
  144.         }
  145.    
  146.         //======================================================================================================
  147.         //      Controls
  148.         //======================================================================================================
  149.    
  150.    
  151.         if (IsKeyDown('w') && Gamecubey > 20 || IsKeyDown(KEY_UP) && Gamecubey > 20) //If either the 'Up' or 'W' key is pressed and the left paddle is not any higher than 20 pixels below the top of the screen
  152.         {
  153.             Gamecubey = Gamecubey - 4; //Move the Gamecube paddle up.
  154.         }
  155.    
  156.         if (IsKeyDown('s') && Gamecubey < 665 || IsKeyDown(KEY_DOWN) && Gamecubey < 665)//If either the 'Down' or 'S' key is pressed and the left paddle is not any lower than 665 pixels below the top of the screen
  157.         {
  158.             Gamecubey = Gamecubey + 4; //Move the Gamecube paddle down.
  159.         }
  160.  
  161.    
  162.         //=========================================================================================================
  163.         //                 Add Collision
  164.         //=========================================================================================================
  165.         if (Bally > 767) //If the ball hits the y position of 767 (Which is one pixel above the bottom of the screen
  166.         {
  167.             BallSpeedy = -3; //Change the BallSpeedy to a negative number, which forces the ball to head upwards.
  168.         }
  169.    
  170.         if (Bally < 1) //If the ball hits the y position of 1 (Which is one pixel below the top of the screen
  171.         {
  172.             BallSpeedy = 3; //Change the BallSpeedy to a positive number, which forces the ball to head downwards.
  173.         }
  174.    
  175.         if (Ballx > Wiix && Bally > Wiiy -20 && Bally < Wiiy + 110 && Bally > Wiiy) //If ballx is greater than Wiix's position and is NOT higher or lower than its y positions, the ball will hit the wall behind the Wii... Otherwise
  176.         {
  177.             BallSpeedx = -BallSpeedx; //BallSpeed equals it's current speed but in a negative form, forcing it to move left.
  178.             BallSpeedx -= 0.1f; //Everytime the paddle hits the ball, the balls speed slowly increases by 0.1.
  179.             Ballx = Wiix -10; //As the Wii is a box shape paddle, if the ball hits the top/bottom of the wii, it can cause problems. This code puts the ball back in front of the wii to keep the game from breaking.
  180.             iP2Score = iP2Score + 10; //Player 2's score is increased by ten.
  181.         }
  182.         if (Ballx < Gamecubex + 85 && Bally < Gamecubey + 110 && Bally > Gamecubey) //If ballx is less than Gamecube's position and isn't higher or lower than its y positions, the ball will hit the wall behind the Gamecube, otherwise
  183.         {
  184.             BallSpeedx = -BallSpeedx; //BallSpeed equals it's current speed but in a positive form, forcing it to move right.
  185.             BallSpeedx += 0.1f; //Everytime the paddle hits the ball, the balls speed slowly increases by 0.1.
  186.             Ballx = Gamecubex +100; //As the Gamecube is a box shape paddle, if the ball hits the top/bottom of it, it can cause problems. This code puts the ball back in front of the Gamecube to keep the game from breaking.
  187.             iP1Score = iP1Score + 10; //Player 1's score is increased by ten.
  188.         }
  189.    
  190.    
  191.         //==========================================================================================================
  192.         //      A.I
  193.         //==========================================================================================================
  194.    
  195.         if (Ballx > 400 ) //If the balls x position is greater than 400 pixels from the left side of the screen
  196.         {
  197.             if( Wiiy + 50 > Bally && Wiiy > 20) //And If the ballx position is higher than the Wii's position (Plus 50, so that the Wii will try to position the ball towards its center)
  198.             {
  199.                 Wiiy = Wiiy - 4; //The Wii paddle is free to move upwards on it's own.
  200.             }
  201.    
  202.             if( Wiiy + 50 < Bally && Wiiy < 665) //And If the ballx position is Lower than the Wii's position (Plus 50, so that the Wii will try to position the ball towards its center)
  203.             {
  204.                 Wiiy = Wiiy + 4; //The Wii Paddle is free to move downwards on it's own.
  205.             }
  206.         }
  207.    
  208.    
  209.    
  210.         if (BallSpeedx < 1 && Wiiy < 384)//If the ball is moving away from the wii and the wii is lower than the center position of the screen
  211.         {
  212.             Wiiy = Wiiy +1; //The Wii paddle will move slowly upwards to the center of the screen.
  213.         }
  214.    
  215.         if (BallSpeedx < 1 && Wiiy > 384) //If the ball is moving away from the wii and the wii is higher than the center position of the screen
  216.         {
  217.             Wiiy = Wiiy -1; //The Wii paddle will slowly move downwards to the center of the screen.
  218.         }
  219.    
  220.    
  221.         //==========================================================================================================
  222.         //      Restart The Round
  223.         //==========================================================================================================
  224.    
  225.         if (iP1RoundScore == 6) //If Player 1's Round Score equals six, then they have won the game and
  226.         {
  227.        
  228.             InsertScore(iP1Score); //P1's Score will be inserted into the highscore table if they have gotten a higher score than any other.
  229.             //The 'InserScore(iP1Score);' will call upon the 'void InsertScore( int iHighScore)' function and replace 'int iHighScore' with 'iP1Score'.
  230.             iP1Score = 0; //Sets the players score back to zero before the InsertScore function is called upon again and we get an infinite loop of highscores into ou table.
  231.             iP2Score = 0; //Sets player 2's score back to zero as well.
  232.        
  233.    
  234.             ClearScreen(); //Clears the screen of any previous sprites that were used on it.
  235.        
  236.             Ballx = 512; //Sets the balls x position back to the center.
  237.             Bally = 368; //Sets the balls y postion into the center of the screen as well.
  238.             Gamecubey = 368; //Sets the left paddles position to the center of the screen.
  239.             Wiiy = 368; //Sets the right paddles position to the center of the screen.
  240.             //This prepares the game for another run in case the player decides to play again.
  241.    
  242.             DrawString("Player 1 Wins", 270, 100, 5); //Prints the text 'Player 1 Wins' on the screen.
  243.             DrawString("Restart Round by pressing 'Enter'", 100, 500, 2); //Prints instructions to the player on the screen.
  244.             DrawString("Press 'BackSpace' to return to menu", 100, 530, 2); //prints instructions to the player on the screen.
  245.    
  246.    
  247.    
  248.             if (IsKeyDown(KEY_RETURN)) //If the player presses 'Enter/Return', the scores are reset, exiting the IF statement loop and restarting the game.
  249.             {
  250.                 iP1RoundScore = 0; //resets the player 1's round score to zero
  251.                 iP2RoundScore = 0; //resets the player 2's round score to zero
  252.             }
  253.         }
  254.    
  255.         if (iP2RoundScore == 6) //If Player 2's Round Score equals six, then they have won the game and
  256.         {
  257.             InsertScore(iP1Score); //Since this is an player vs AI match, P1's Score will still be inserted into the highscore table if they have gotten a higher score than any other.
  258.             iP1Score = 0; //Sets the players score back to zero before the InsertScore function is called upon again and we get an infinite loop of highscores into our table.
  259.             iP2Score = 0; //Sets the players score back to zero.
  260.        
  261.             ClearScreen(); //Clears the screen of any previous sprites that were used on it.
  262.        
  263.             Ballx = 512; //Sets the balls x position back to the center.
  264.             Bally = 368; //Sets the balls y postion into the center of the screen as well.
  265.             Gamecubey = 368; //Sets the left paddles position to the center of the screen.
  266.             Wiiy = 368; //Sets the right paddles position to the center of the screen.
  267.             //This prepares the game for another run in case the player decides to play again.
  268.    
  269.             DrawString("Player 2 Wins", 270, 100, 5); //Prints the text 'Player 2 Wins' on the screen.
  270.             DrawString("Restart Round by pressing 'Enter'", 100, 500, 2); //Prints instructions to the player on the screen.
  271.             DrawString("Press 'BackSpace' to return to menu", 100, 530, 2); //prints instructions to the player on the screen.
  272.    
  273.             if (IsKeyDown(KEY_RETURN)) //If the player presses 'Enter/Return', the scores are reset, exiting the IF statement loop and restarting the game.
  274.             {
  275.                 iP1RoundScore = 0; //resets the player 1's round score to zero
  276.                 iP2RoundScore = 0; //resets the player 2's round score to zero
  277.             }
  278.         }
  279.    
  280.         //==========================================================================================================
  281.         //      Quit
  282.         //==========================================================================================================
  283.    
  284.         //BackSpace will quit to the Main Menu.
  285.         if (IsKeyDown(KEY_BACKSPACE))
  286.             break;
  287.  
  288.         Sleep(5); //Will slow the frame-rate down to a more slower paced speed for the human eyes to be able to see what is happening.
  289.  
  290.     }while(FrameworkUpdate()); //Will repeat all those functions as long as the frames keep bing updated.
  291.  
  292. }
  293.  
  294. void GameState_P1_VS_P2 ()
  295. {
  296.     int Ball                = LoadTexture("./images/Shine_Sprite.png"); //Creates the Ball Sprite for the game.
  297.     int Game_BackGround     = LoadTexture("./images/Nintendo_Logo.png"); //Creates the background Image for the game.
  298.     int Gamecube            = LoadTexture("./images/gamecube.png"); //Creates the Gamecube Sprite (Which will be our left paddle) for the game.
  299.     int Wii                 = LoadTexture("./images/wii.png"); //Creates the Wii Sprite (Which will be used as our right paddle) for the game.
  300.  
  301.     float Ballx = 512; //Creates the balls 'X Position', 512 is the middle of the screen, so it makes the game fair.
  302.     float Bally = 512; //Creates the balls 'Y Position', 512 starts the ball on the lower end of the screen and aims to make the start a little challenging.  
  303.  
  304.     float BallSpeedx = 3; //Creates the 'X Speed' of the ball, 'X Speed' controls the balls left and right movement.
  305.     float BallSpeedy = 3; //Creates the 'Y Speed' of the ball, 'Y Speed' controls the balls up and down movements.
  306.  
  307.     float Gamecubex = 30; //Sets the left paddles original x position to be just away from the left wall. Thus leaving enough space for the ball to hit the wall behind it.
  308.     float Gamecubey = 200; //Sets the left paddles original y position to start near the top of the screen, to be ready to hit the ball.
  309.  
  310.     float Wiix = 920; //Sets the right paddles original x postion to be just away from the right wall. Thus leaving enough space for the ball to hit the wall behind it.
  311.     float Wiiy = 400; //Sets the right paddles original y position to start near the bottom of the screen, to be ready to hit the ball.
  312.  
  313.     int iP1Score = 0; //Creates and sets Player 1's initial score as zero.
  314.     int iP2Score = 0; //Creates and sets Player 2's initial score as zero.
  315.  
  316.     int iP1RoundScore = 0; //Creates and sets Player 1's initial Round Score as zero.
  317.     int iP2RoundScore = 0; //Creates and sets Player 2's initial round score as zero.
  318.  
  319.     int iFrameCount = 0; //Creates and sets the immediate Frame waiting count to zero.
  320.     int iFrameWaitAmount = 128; //Creates and sets the FrameWaitAmount to 128 frames.
  321.     bool bWaiting = false; //Sets the frame wait condition to false for the beginning of the game.
  322.  
  323.  
  324.         do
  325.         { //a 'do while' loop will always do whatever functions are inside of its scope at least once. However, it will continue to loop through those functions if the conditions for the 'while()' are met.
  326.            
  327.             ClearScreen(); //Refreshes the screen from any previous sprites that were on it.
  328.    
  329.             if( bWaiting == true) //if bWaiting has been set to true then
  330.             {
  331.                 iFrameCount += 1; //'iFrameCount' will keep adding +1 to itself until it becomes higher than 'iFrameWaitAmount'
  332.                 if( iFrameCount > iFrameWaitAmount) //Once iFrameCount' is higher than 'iFrameWaitAmount
  333.                 {
  334.                     bWaiting = false; //The boolean 'bWaiting' is set back to false and
  335.                     iFrameCount = 0; //iFrameCount is set back to zero. The game will now continue.
  336.                 }
  337.             }
  338.        
  339.             if( bWaiting == false) //If bWaiting is set to false (By default it is)
  340.             {
  341.                 Ballx += BallSpeedx; //Ballx will add BallSpeedx to itself, thus moving it's position.
  342.                 Bally += BallSpeedy; //Bally will add BallSpeedy to itself, thus moving it's position.
  343.             }
  344.        
  345.             char cP1ScoreString[6]; //Creates the char array for Player 1's score, since the array is of a six value, the highest amount of points that the player could get is 999,999.
  346.             _itoa_s(iP1Score, cP1ScoreString, 10); //converts the first integer into a string.
  347.             char cP2ScoreString[6]; //Creates the char array for Player 1's score, since the array is of a six value, the highest amount of points that the player could get is 999,999.
  348.             _itoa_s(iP2Score, cP2ScoreString, 10); //converts the first integer into a string.
  349.        
  350.             char cP1RoundScoreString[6]; //Creates the char array for Player 1's round score, the actual score can only go up to six and no futher.
  351.             _itoa_s(iP1RoundScore, cP1RoundScoreString, 10); //converts the first integer into a string.
  352.             char cP2RoundScoreString[6]; //Creates the char array for Player 2's round score, the actual score can only go up to six and no futher.
  353.             _itoa_s(iP2RoundScore, cP2RoundScoreString, 10); //converts the first integer into a string.
  354.        
  355.             //==============================================================================================
  356.             // Draw Sprites
  357.             //==============================================================================================
  358.        
  359.             DrawSprite(Game_BackGround, 0, 0, 1028, 768); //Sets the game background image for use. The '0, 0, 1028, 768' stands for X pos, Y pos, width, height. The graphic is set on the top left of the screen and its width and height are adjusted to cover the whole screen.
  360.        
  361.             DrawString(cP1ScoreString, 150, 50, 2); //Sets Player 1's Score to be displayed in the top left corner.
  362.             DrawString(cP2ScoreString, 780, 50, 2); //Sets Player 2's Score to be displayed in the top right corner.
  363.             DrawString(cP1RoundScoreString, 150, 120, 6); // Sets Player 1's Round Score to be displayed just under their total score.
  364.             DrawString(cP2RoundScoreString, 780, 120, 6); //Sets Player 2's Round Score to be displayed just under their total score.
  365.        
  366.        
  367.             DrawSprite(Ball, Ballx , Bally, 30, 30); //Sets the ball position in the middle of the screen but lower, and draws its size to be 30 pixels wide and 30 pixels height.
  368.             DrawSprite(Gamecube, Gamecubex, Gamecubey, 100, 100); //Sets the left paddles position to the top left of the screen, ready to be used.
  369.             DrawSprite(Wii, Wiix, Wiiy, 100, 100); //Sets the right paddles position to the bottom right of the screen.
  370.        
  371.        
  372.             //================================================================================================
  373.             //      Move the Ball & Add the Scores
  374.             //=================================================================================================
  375.        
  376.             if (Ballx > 1027 ) //If the ball makes it to the x value of 1027 (Which is one pixel away from the right wall)
  377.             {
  378.                 Ballx = 512; //The ball is set back to the middle
  379.                 Bally = 512; //The Ball is set back to it's lowered position
  380.                 BallSpeedx = -3; //BallSpeedx results in a negative number to make it begin to move left instead of right.
  381.                 bWaiting = true; //The boolean variable 'bWaiting' is set to true so that the ball will wait a little bit before the game starts again.
  382.                 iP1RoundScore = iP1RoundScore +1; //And finally Player 1's Round Score is increased by one.
  383.             }
  384.        
  385.             if (Ballx < 1) //If the ball makes it to the x value of 1 (Which is one pixel away from the left wall)
  386.             {
  387.                 Ballx = 512; //The ball is set back to the middle
  388.                 Bally = 512; //The Ball is set back to it's lowered position
  389.                 BallSpeedx = 3; //BallSpeedx results in a positive number to make it begin to move right instead of left.
  390.                 bWaiting = true; //The boolean variable 'bWaiting' is set to true so that the ball will wait a little bit before the game starts again.
  391.                 iP2RoundScore = iP2RoundScore +1; //And finally Player 2's Round Score is increased by one.
  392.             }
  393.  
  394.             //======================================================================================================
  395.             //      Controls
  396.             //======================================================================================================
  397.             if (IsKeyDown(KEY_UP) && Wiiy > 20) //If the 'Up' key is pressed and the Wiis position is more than 20 pixels from the top of the screen
  398.             {
  399.                 Wiiy = Wiiy - 4; //The Wii can is moved upwards.
  400.             }
  401.  
  402.             if (IsKeyDown(KEY_DOWN) && Wiiy < 665) //If the 'Down' key is pressed and the Wiis position is less than 665 pixels from the top of the screen
  403.             {
  404.                 Wiiy = Wiiy + 4; //The Wii paddle can be moved downwards.
  405.             }
  406.  
  407.             if (IsKeyDown('w') && Gamecubey > 20) //If the 'W' key is pressed and the Gamecube's position os more than 20 pixels from the top of the screen
  408.             {
  409.                 Gamecubey = Gamecubey - 4; //The Gamecube paddle Shall be moved Downwards.
  410.             }
  411.  
  412.             if (IsKeyDown('s') && Gamecubey < 665) //If the 'S' key is pressed and the Gamecube's position is less than 665 pixels from the top of the screen
  413.             {
  414.                 Gamecubey = Gamecubey + 4; //The Gamecube paddle can be moved downwards.
  415.             }
  416.  
  417.             //=========================================================================================================
  418.             //                 Add Collision
  419.             //=========================================================================================================
  420.             if (Bally > 767) //If the ball hits the y position of 767 (Which is one pixel above the bottom of the screen
  421.             {
  422.                 BallSpeedy = -3; //Change the BallSpeedy to a negative number, which forces the ball to head upwards.
  423.             }
  424.        
  425.             if (Bally < 1) //If the ball hits the y position of 1 (Which is one pixel below the top of the screen
  426.             {
  427.                 BallSpeedy = 3; //Change the BallSpeedy to a positive number, which forces the ball to head downwards.
  428.             }
  429.        
  430.             if (Ballx > Wiix && Bally > Wiiy -20 && Bally < Wiiy + 110 && Bally > Wiiy) //If ballx is greater than Wiix's position and is NOT higher or lower than its y positions, the ball will hit the wall behind the Wii... Otherwise
  431.             {
  432.                 BallSpeedx = -BallSpeedx; //BallSpeed equals it's current speed but in a negative form, forcing it to move left.
  433.                 BallSpeedx -= 0.1f; //Everytime the paddle hits the ball, the balls speed slowly increases by 0.1.
  434.                 Ballx = Wiix -10; //As the Wii is a box shape paddle, if the ball hits the top/bottom of the wii, it can cause problems. This code puts the ball back in front of the wii to keep the game from breaking.
  435.                 iP2Score = iP2Score + 10; //Player 2's score is increased by ten.
  436.             }
  437.             if (Ballx < Gamecubex + 85 && Bally < Gamecubey + 110 && Bally > Gamecubey) //If ballx is less than Gamecube's position and isn't higher or lower than its y positions, the ball will hit the wall behind the Gamecube, otherwise
  438.             {
  439.                 BallSpeedx = -BallSpeedx; //BallSpeed equals it's current speed but in a positive form, forcing it to move right.
  440.                 BallSpeedx += 0.1f; //Everytime the paddle hits the ball, the balls speed slowly increases by 0.1.
  441.                 Ballx = Gamecubex +100; //As the Gamecube is a box shape paddle, if the ball hits the top/bottom of it, it can cause problems. This code puts the ball back in front of the Gamecube to keep the game from breaking.
  442.                 iP1Score = iP1Score + 10; //Player 1's score is increased by ten.
  443.             }
  444.  
  445.  
  446.             //==========================================================================================================
  447.             //      Restart The Round
  448.             //==========================================================================================================
  449.  
  450.             if (iP1RoundScore == 6) //If Player 1's Round Score equals six, then they have won the game and
  451.             {
  452.            
  453.                 InsertScore(iP1Score); //P1's Score will be inserted into the highscore table if they have gotten a higher score than any other.
  454.                 //The 'InserScore(iP1Score);' will call upon the 'void InsertScore( int iHighScore)' function and replace 'int iHighScore' with 'iP1Score'.
  455.                 InsertScore(iP2Score); //P2's Score is also added to the highscore list, even if they didn't win.
  456.                 iP1Score = 0; //Sets the players score back to zero before the InsertScore function is called upon again and we get an infinite loop of highscores into ou table.
  457.                 iP2Score = 0; //Sets player 2's score back to zero as well.
  458.            
  459.        
  460.                 ClearScreen(); //Clears the screen of any previous sprites that were used on it.
  461.            
  462.                 Ballx = 512; //Sets the balls x position back to the center.
  463.                 Bally = 368; //Sets the balls y postion into the center of the screen as well.
  464.                 Gamecubey = 368; //Sets the left paddles position to the center of the screen.
  465.                 Wiiy = 368; //Sets the right paddles position to the center of the screen.
  466.                 //This prepares the game for another run in case the player decides to play again.
  467.        
  468.                 DrawString("Player 1 Wins", 270, 100, 5); //Prints the text 'Player 1 Wins' on the screen.
  469.                 DrawString("Restart Round by pressing 'Enter'", 100, 500, 2); //Prints instructions to the player on the screen.
  470.                 DrawString("Press 'BackSpace' to return to menu", 100, 530, 2); //prints instructions to the player on the screen.
  471.        
  472.        
  473.        
  474.                 if (IsKeyDown(KEY_RETURN)) //If the player presses 'Enter/Return', the scores are reset, exiting the IF statement loop and restarting the game.
  475.                 {
  476.                     iP1RoundScore = 0; //resets the player 1's round score to zero
  477.                     iP2RoundScore = 0; //resets the player 2's round score to zero
  478.                 }
  479.             }
  480.        
  481.             if (iP2RoundScore == 6) //If Player 2's Round Score equals six, then they have won the game and
  482.             {
  483.                 InsertScore(iP1Score); //Player 1's score will also be added to the list, even if they lost the match.
  484.                 InsertScore(iP2Score); //P2's Score will be inserted into the highscore table if they have gotten a higher score than any other.
  485.                 iP1Score = 0; //Sets the players score back to zero before the InsertScore function is called upon again and we get an infinite loop of highscores into our table.
  486.                 iP2Score = 0; //Sets the players score back to zero.
  487.            
  488.                 ClearScreen(); //Clears the screen of any previous sprites that were used on it.
  489.            
  490.                 Ballx = 512; //Sets the balls x position back to the center.
  491.                 Bally = 368; //Sets the balls y postion into the center of the screen as well.
  492.                 Gamecubey = 368; //Sets the left paddles position to the center of the screen.
  493.                 Wiiy = 368; //Sets the right paddles position to the center of the screen.
  494.                 //This prepares the game for another run in case the player decides to play again.
  495.        
  496.                 DrawString("Player 2 Wins", 270, 100, 5); //Prints the text 'Player 2 Wins' on the screen.
  497.                 DrawString("Restart Round by pressing 'Enter'", 100, 500, 2); //Prints instructions to the player on the screen.
  498.                 DrawString("Press 'BackSpace' to return to menu", 100, 530, 2); //prints instructions to the player on the screen.
  499.        
  500.                 if (IsKeyDown(KEY_RETURN)) //If the player presses 'Enter/Return', the scores are reset, exiting the IF statement loop and restarting the game.
  501.                 {
  502.                     iP1RoundScore = 0; //resets the player 1's round score to zero
  503.                     iP2RoundScore = 0; //resets the player 2's round score to zero
  504.                 }
  505.         }
  506.    
  507.         //==========================================================================================================
  508.         //      Quit
  509.         //==========================================================================================================
  510.    
  511.         //BackSpace will quit to the Main Menu.
  512.         if (IsKeyDown(KEY_BACKSPACE))
  513.             break;
  514.  
  515.         Sleep(5); //Will slow the frame-rate down to a more slower paced speed for the human eyes to be able to see what is happening.
  516.  
  517.     }while(FrameworkUpdate()); //Will repeat all those functions as long as the frames keep bing updated.
  518.  
  519. }
  520.            
  521. void GameState_P1_VS_WALL ()
  522. {
  523.     int Ball                = LoadTexture("./images/Shine_Sprite.png"); //Creates the Ball Sprite for the game.
  524.     int Game_BackGround     = LoadTexture("./images/Nintendo_Logo.png"); //Creates the background Image for the game.
  525.     int Gamecube            = LoadTexture("./images/gamecube.png"); //Creates the Gamecube Sprite (Which will be our left paddle) for the game.
  526.     int Wall                = LoadTexture("./images/Wall.png"); //Creates the Wall Sprite that the player will play 'one touch' with.
  527.  
  528.     float Ballx = 512; //Creates the balls 'X Position', 512 is the middle of the screen, so it makes the game fair.
  529.     float Bally = 512; //Creates the balls 'Y Position', 512 starts the ball on the lower end of the screen and aims to make the start a little challenging.  
  530.  
  531.     float BallSpeedx = 3; //Creates the 'X Speed' of the ball, 'X Speed' controls the balls left and right movement.
  532.     float BallSpeedy = 3; //Creates the 'Y Speed' of the ball, 'Y Speed' controls the balls up and down movements.
  533.  
  534.     float Gamecubex = 30; //Sets the left paddles original x position to be just away from the left wall. Thus leaving enough space for the ball to hit the wall behind it.
  535.     float Gamecubey = 200; //Sets the left paddles original y position to start near the top of the screen, to be ready to hit the ball.
  536.  
  537.     float Wallx = 920; //Sets the Walls position towards the right side of the screen.
  538.     float Wally = 0; //Sets the walls position to the top of the screen.
  539.  
  540.     int iP1Score = 0; //Creates and sets Player 1's initial score as zero.
  541.     int iP2Score = 0; //Creates and sets Player 2's initial score as zero.
  542.  
  543.     int iP1RoundScore = 0; //Creates and sets Player 1's initial Round Score as zero.
  544.     int iP2RoundScore = 0; //Creates and sets Player 2's initial round score as zero.
  545.  
  546.  
  547.     int iFrameCount = 0; //Creates and sets the immediate Frame waiting count to zero.
  548.     int iFrameWaitAmount = 128; //Creates and sets the FrameWaitAmount to 128 frames.
  549.     bool bWaiting = false; //Sets the frame wait condition to false for the beginning of the game.
  550.  
  551.  
  552.         do
  553.         { //a 'do while' loop will always do whatever functions are inside of its scope at least once. However, it will continue to loop through those functions if the conditions for the 'while()' are met.
  554.            
  555.             ClearScreen(); //Refreshes the screen from any previous sprites that were on it.
  556.    
  557.             if( bWaiting == true) //if bWaiting has been set to true then
  558.             {
  559.                 iFrameCount += 1; //'iFrameCount' will keep adding +1 to itself until it becomes higher than 'iFrameWaitAmount'
  560.                 if( iFrameCount > iFrameWaitAmount) //Once iFrameCount' is higher than 'iFrameWaitAmount
  561.                 {
  562.                     bWaiting = false; //The boolean 'bWaiting' is set back to false and
  563.                     iFrameCount = 0; //iFrameCount is set back to zero. The game will now continue.
  564.                 }
  565.             }
  566.        
  567.             if( bWaiting == false) //If bWaiting is set to false (By default it is)
  568.             {
  569.                 Ballx += BallSpeedx; //Ballx will add BallSpeedx to itself, thus moving it's position.
  570.                 Bally += BallSpeedy; //Bally will add BallSpeedy to itself, thus moving it's position.
  571.             }
  572.        
  573.             char cP1ScoreString[6]; //Creates the char array for Player 1's score, since the array is of a six value, the highest amount of points that the player could get is 999,999.
  574.             _itoa_s(iP1Score, cP1ScoreString, 10); //converts the first integer into a string.
  575.  
  576.             char cP1RoundScoreString[6]; //Creates the char array for Player 1's round score, the actual score can only go up to six and no futher.
  577.             _itoa_s(iP1RoundScore, cP1RoundScoreString, 10); //converts the first integer into a string.
  578.            
  579.  
  580.             //==============================================================================================
  581.             // Draw Sprites
  582.             //==============================================================================================
  583.  
  584.             DrawSprite(Game_BackGround, 0, 0, 1028, 768); //Sets the game background image for use. The '0, 0, 1028, 768' stands for X pos, Y pos, width, height. The graphic is set on the top left of the screen and its width and height are adjusted to cover the whole screen.
  585.        
  586.             DrawString(cP1ScoreString, 150, 50, 2); //Sets Player 1's Score to be displayed in the top left corner.
  587.                                        
  588.             DrawSprite(Ball, Ballx , Bally, 30, 30); //Sets the ball position in the middle of the screen but lower, and draws its size to be 30 pixels wide and 30 pixels height.
  589.             DrawSprite(Gamecube, Gamecubex, Gamecubey, 100, 100); //Sets the left paddles position to the top left of the screen, ready to be used.
  590.             DrawSprite(Wall, Wallx, Wally, 100, 768); //Draws the wall at its previously define x and y positions, then scales it to the defined size.
  591.  
  592.  
  593.             //================================================================================================
  594.             //      Move the Ball
  595.             //=================================================================================================
  596.  
  597.             if (Ballx > 1027 ) //If the ball makes it to the x value of 1027 (Which is one pixel away from the right wall)
  598.             {
  599.                 Ballx = 512; //The ball is set back to the middle
  600.                 Bally = 512; //The Ball is set back to it's lowered position
  601.                 BallSpeedx = -3; //BallSpeedx results in a negative number to make it begin to move left instead of right.
  602.                 bWaiting = true; //The boolean variable 'bWaiting' is set to true so that the ball will wait a little bit before the game starts again.
  603.             }
  604.        
  605.             if (Ballx < 1) //If the ball makes it to the x value of 1 (Which is one pixel away from the left wall)
  606.             {
  607.                 Ballx = 512; //The ball is set back to the middle
  608.                 Bally = 512; //The Ball is set back to it's lowered position
  609.                 BallSpeedx = 3; //BallSpeedx results in a positive number to make it begin to move right instead of left.
  610.                 bWaiting = true; //The boolean variable 'bWaiting' is set to true so that the ball will wait a little bit before the game starts again.
  611.             }
  612.  
  613.             //======================================================================================================
  614.             //      Controls
  615.             //======================================================================================================
  616.  
  617.  
  618.             if (IsKeyDown('w') && Gamecubey > 20 || IsKeyDown(KEY_UP) && Gamecubey > 20) //If either the 'Up' or 'W' key is pressed and the left paddle is not any higher than 20 pixels below the top of the screen
  619.             {
  620.                 Gamecubey = Gamecubey - 4; //Move the Gamecube paddle up.
  621.             }
  622.    
  623.             if (IsKeyDown('s') && Gamecubey < 665 || IsKeyDown(KEY_DOWN) && Gamecubey < 665)//If either the 'Down' or 'S' key is pressed and the left paddle is not any lower than 665 pixels below the top of the screen
  624.             {
  625.                 Gamecubey = Gamecubey + 4; //Move the Gamecube paddle down.
  626.             }
  627.  
  628.             //=========================================================================================================
  629.             //                 Add Collision
  630.             //=========================================================================================================
  631.             if (Bally > 767)
  632.             {
  633.                 BallSpeedy = -3;
  634.             }
  635.  
  636.             if (Bally < 1)
  637.             {
  638.                 BallSpeedy = 3;
  639.             }
  640.  
  641.             if (Ballx > Wallx)
  642.             {
  643.                 BallSpeedx = -BallSpeedx;
  644.                 BallSpeedx -= 0.1f;
  645.             }
  646.             if (Ballx < Gamecubex + 85 && Bally < Gamecubey + 110 && Bally > Gamecubey)
  647.             {
  648.                 BallSpeedx = -BallSpeedx;
  649.                 BallSpeedx += 0.1f;
  650.                 Ballx = Gamecubex +100;
  651.                 iP1Score = iP1Score + 10;
  652.             }
  653.  
  654.  
  655.        
  656.             //==========================================================================================================
  657.             //      Restart The Round
  658.             //==========================================================================================================
  659.  
  660.             if (iP2RoundScore == 1) //Since the wall can never lose, we test for when the wall scores its point, signaling the end of the game.
  661.             {  
  662.                 InsertScore(iP1Score); //Player 1's score will also be added to the list, even if they lost the match.
  663.                 iP1Score = 0; //Sets the players score back to zero before the InsertScore function is called upon again and we get an infinite loop of highscores into our table.
  664.                
  665.  
  666.                 ClearScreen(); //Clears the screen of any previous sprites that were used on it.
  667.            
  668.                 Ballx = 512; //Sets the balls x position back to the center.
  669.                 Bally = 368; //Sets the balls y postion into the center of the screen as well.
  670.                 Gamecubey = 368; //Sets the left paddles position to the center of the screen.
  671.                
  672.                
  673.  
  674.                 DrawString("Would you like to play again?", 270, 100, 2); //Asks if the Player would like to play again
  675.                 DrawString("Restart Round by pressing 'Enter'", 100, 500, 2); //Gives the player the instruction to press enter if they wish to do so
  676.                 DrawString("Press 'BackSpace' to return to menu", 100, 530, 2); //Gives the player the instruction to press 'Backspace' if they do not wish to play again.
  677.  
  678.  
  679.  
  680.                 if (IsKeyDown(KEY_RETURN)) //If the player presses 'Enter/Return', the scores are reset, exiting the IF statement loop and restarting the game.
  681.                 {
  682.                     iP1RoundScore = 0; //resets the player 1's round score to zero
  683.                 }
  684.         }
  685.    
  686.         //==========================================================================================================
  687.         //      Quit
  688.         //==========================================================================================================
  689.    
  690.         //BackSpace will quit to the Main Menu.
  691.         if (IsKeyDown(KEY_BACKSPACE))
  692.             break; //break out of the loop and ends the function processing.
  693.  
  694.         Sleep(5); //Will slow the frame-rate down to a more slower paced speed for the human eyes to be able to see what is happening.
  695.  
  696.     }while(FrameworkUpdate()); //Will repeat all those functions as long as the frames keep bing updated.
  697.  
  698. }
  699.  
  700. void GameState_AI_VS_AI ()
  701. {
  702.     int Ball                = LoadTexture("./images/Shine_Sprite.png"); //Creates the Ball Sprite for the game.
  703.     int Game_BackGround     = LoadTexture("./images/Nintendo_Logo.png"); //Creates the background Image for the game.
  704.     int Gamecube            = LoadTexture("./images/gamecube.png"); //Creates the Gamecube Sprite (Which will be our left paddle) for the game.
  705.     int Wii                 = LoadTexture("./images/wii.png"); //Creates the Wii Sprite (Which will be used as our right paddle) for the game.
  706.  
  707.     float Ballx = 512; //Creates the balls 'X Position', 512 is the middle of the screen, so it makes the game fair.
  708.     float Bally = 512; //Creates the balls 'Y Position', 512 starts the ball on the lower end of the screen and aims to make the start a little challenging.  
  709.  
  710.     float BallSpeedx = 3; //Creates the 'X Speed' of the ball, 'X Speed' controls the balls left and right movement.
  711.     float BallSpeedy = 3; //Creates the 'Y Speed' of the ball, 'Y Speed' controls the balls up and down movements.
  712.  
  713.     float Gamecubex = 30; //Sets the left paddles original x position to be just away from the left wall. Thus leaving enough space for the ball to hit the wall behind it.
  714.     float Gamecubey = 200; //Sets the left paddles original y position to start near the top of the screen, to be ready to hit the ball.
  715.  
  716.     float Wiix = 920; //Sets the right paddles original x postion to be just away from the right wall. Thus leaving enough space for the ball to hit the wall behind it.
  717.     float Wiiy = 400; //Sets the right paddles original y position to start near the bottom of the screen, to be ready to hit the ball.
  718.  
  719.     int iP1Score = 0; //Creates and sets Player 1's initial score as zero.
  720.     int iP2Score = 0; //Creates and sets Player 2's initial score as zero.
  721.  
  722.     int iP1RoundScore = 0; //Creates and sets Player 1's initial Round Score as zero.
  723.     int iP2RoundScore = 0; //Creates and sets Player 2's initial round score as zero.
  724.  
  725.     int iFrameCount = 0; //Creates and sets the immediate Frame waiting count to zero.
  726.     int iFrameWaitAmount = 128; //Creates and sets the FrameWaitAmount to 128 frames.
  727.     bool bWaiting = false; //Sets the frame wait condition to false for the beginning of the game.
  728.  
  729.  
  730.         do
  731.         { //a 'do while' loop will always do whatever functions are inside of its scope at least once. However, it will continue to loop through those functions if the conditions for the 'while()' are met.
  732.            
  733.             ClearScreen(); //Refreshes the screen from any previous sprites that were on it.
  734.    
  735.             if( bWaiting == true) //if bWaiting has been set to true then
  736.             {
  737.                 iFrameCount += 1; //'iFrameCount' will keep adding +1 to itself until it becomes higher than 'iFrameWaitAmount'
  738.                 if( iFrameCount > iFrameWaitAmount) //Once iFrameCount' is higher than 'iFrameWaitAmount
  739.                 {
  740.                     bWaiting = false; //The boolean 'bWaiting' is set back to false and
  741.                     iFrameCount = 0; //iFrameCount is set back to zero. The game will now continue.
  742.                 }
  743.             }
  744.        
  745.             if( bWaiting == false) //If bWaiting is set to false (By default it is)
  746.             {
  747.                 Ballx += BallSpeedx; //Ballx will add BallSpeedx to itself, thus moving it's position.
  748.                 Bally += BallSpeedy; //Bally will add BallSpeedy to itself, thus moving it's position.
  749.             }
  750.        
  751.             char cP1ScoreString[6]; //Creates the char array for Player 1's score, since the array is of a six value, the highest amount of points that the player could get is 999,999.
  752.             _itoa_s(iP1Score, cP1ScoreString, 10); //converts the first integer into a string.
  753.             char cP2ScoreString[6]; //Creates the char array for Player 1's score, since the array is of a six value, the highest amount of points that the player could get is 999,999.
  754.             _itoa_s(iP2Score, cP2ScoreString, 10); //converts the first integer into a string.
  755.        
  756.             char cP1RoundScoreString[6]; //Creates the char array for Player 1's round score, the actual score can only go up to six and no futher.
  757.             _itoa_s(iP1RoundScore, cP1RoundScoreString, 10); //converts the first integer into a string.
  758.             char cP2RoundScoreString[6]; //Creates the char array for Player 2's round score, the actual score can only go up to six and no futher.
  759.             _itoa_s(iP2RoundScore, cP2RoundScoreString, 10); //converts the first integer into a string.
  760.        
  761.             //==============================================================================================
  762.             // Draw Sprites
  763.             //==============================================================================================
  764.        
  765.             DrawSprite(Game_BackGround, 0, 0, 1028, 768); //Sets the game background image for use. The '0, 0, 1028, 768' stands for X pos, Y pos, width, height. The graphic is set on the top left of the screen and its width and height are adjusted to cover the whole screen.
  766.        
  767.             DrawString(cP1ScoreString, 150, 50, 2); //Sets Player 1's Score to be displayed in the top left corner.
  768.             DrawString(cP2ScoreString, 780, 50, 2); //Sets Player 2's Score to be displayed in the top right corner.
  769.             DrawString(cP1RoundScoreString, 150, 120, 6); // Sets Player 1's Round Score to be displayed just under their total score.
  770.             DrawString(cP2RoundScoreString, 780, 120, 6); //Sets Player 2's Round Score to be displayed just under their total score.
  771.        
  772.        
  773.             DrawSprite(Ball, Ballx , Bally, 30, 30); //Sets the ball position in the middle of the screen but lower, and draws its size to be 30 pixels wide and 30 pixels height.
  774.             DrawSprite(Gamecube, Gamecubex, Gamecubey, 100, 100); //Sets the left paddles position to the top left of the screen, ready to be used.
  775.             DrawSprite(Wii, Wiix, Wiiy, 100, 100); //Sets the right paddles position to the bottom right of the screen.
  776.        
  777.        
  778.             //================================================================================================
  779.             //      Move the Ball & Add the Scores
  780.             //=================================================================================================
  781.        
  782.             if (Ballx > 1027 ) //If the ball makes it to the x value of 1027 (Which is one pixel away from the right wall)
  783.             {
  784.                 Ballx = 512; //The ball is set back to the middle
  785.                 Bally = 512; //The Ball is set back to it's lowered position
  786.                 BallSpeedx = -3; //BallSpeedx results in a negative number to make it begin to move left instead of right.
  787.                 bWaiting = true; //The boolean variable 'bWaiting' is set to true so that the ball will wait a little bit before the game starts again.
  788.                 iP1RoundScore = iP1RoundScore +1; //And finally Player 1's Round Score is increased by one.
  789.             }
  790.        
  791.             if (Ballx < 1) //If the ball makes it to the x value of 1 (Which is one pixel away from the left wall)
  792.             {
  793.                 Ballx = 512; //The ball is set back to the middle
  794.                 Bally = 512; //The Ball is set back to it's lowered position
  795.                 BallSpeedx = 3; //BallSpeedx results in a positive number to make it begin to move right instead of left.
  796.                 bWaiting = true; //The boolean variable 'bWaiting' is set to true so that the ball will wait a little bit before the game starts again.
  797.                 iP2RoundScore = iP2RoundScore +1; //And finally Player 2's Round Score is increased by one.
  798.             }
  799.  
  800.             //=========================================================================================================
  801.             //                 Add Collision
  802.             //=========================================================================================================
  803.            
  804.             if (Bally > 767) //If the ball hits the y position of 767 (Which is one pixel above the bottom of the screen
  805.             {
  806.                 BallSpeedy = -3; //Change the BallSpeedy to a negative number, which forces the ball to head upwards.
  807.             }
  808.        
  809.             if (Bally < 1) //If the ball hits the y position of 1 (Which is one pixel below the top of the screen
  810.             {
  811.                 BallSpeedy = 3; //Change the BallSpeedy to a positive number, which forces the ball to head downwards.
  812.             }
  813.        
  814.             if (Ballx > Wiix && Bally > Wiiy -20 && Bally < Wiiy + 110 && Bally > Wiiy) //If ballx is greater than Wiix's position and is NOT higher or lower than its y positions, the ball will hit the wall behind the Wii... Otherwise
  815.             {
  816.                 BallSpeedx = -BallSpeedx; //BallSpeed equals it's current speed but in a negative form, forcing it to move left.
  817.                 BallSpeedx -= 0.1f; //Everytime the paddle hits the ball, the balls speed slowly increases by 0.1.
  818.                 Ballx = Wiix -10; //As the Wii is a box shape paddle, if the ball hits the top/bottom of the wii, it can cause problems. This code puts the ball back in front of the wii to keep the game from breaking.
  819.                 iP2Score = iP2Score + 10; //Player 2's score is increased by ten.
  820.             }
  821.             if (Ballx < Gamecubex + 85 && Bally < Gamecubey + 110 && Bally > Gamecubey) //If ballx is less than Gamecube's position and isn't higher or lower than its y positions, the ball will hit the wall behind the Gamecube, otherwise
  822.             {
  823.                 BallSpeedx = -BallSpeedx; //BallSpeed equals it's current speed but in a positive form, forcing it to move right.
  824.                 BallSpeedx += 0.1f; //Everytime the paddle hits the ball, the balls speed slowly increases by 0.1.
  825.                 Ballx = Gamecubex +100; //As the Gamecube is a box shape paddle, if the ball hits the top/bottom of it, it can cause problems. This code puts the ball back in front of the Gamecube to keep the game from breaking.
  826.                 iP1Score = iP1Score + 10; //Player 1's score is increased by ten.
  827.             }
  828.            
  829.  
  830.  
  831.             //==========================================================================================================
  832.             //      A.I
  833.             //==========================================================================================================
  834.            
  835.             if (Ballx > 400 ) //If the balls x position is greater than 400 pixels from the left side of the screen
  836.             {
  837.                 if( Wiiy + 50 > Bally && Wiiy > 20) //And If the ballx position is higher than the Wii's position (Plus 50, so that the Wii will try to position the ball towards its center)
  838.                 {
  839.                     Wiiy = Wiiy - 4; //The Wii paddle is free to move upwards on it's own.
  840.                 }
  841.    
  842.                 if( Wiiy + 50 < Bally && Wiiy < 665) //And If the ballx position is Lower than the Wii's position (Plus 50, so that the Wii will try to position the ball towards its center)
  843.                 {
  844.                     Wiiy = Wiiy + 4; //The Wii Paddle is free to move downwards on it's own.
  845.                 }
  846.             }
  847.  
  848.             if (Ballx < 612 ) //If the balls x position is less than 612 pixels from the left side of the screen
  849.             {
  850.                 if( Gamecubey + 50 > Bally && Gamecubey > 20) //And If the ballx position is higher than the Gamecube's position (Plus 50, so that the Wii will try to position the ball towards its center)
  851.                 {
  852.                     Gamecubey = Gamecubey - 4; //The Gamecube paddle is free to move upwards on it's own.
  853.                 }
  854.    
  855.                 if ( Gamecubey + 50 < Bally && Gamecubey < 665) //And If the ballx position is Lower than the Gamecube's position (Plus 50, so that the Gamecube will try to position the ball towards its center)
  856.                 {
  857.                     Gamecubey = Gamecubey + 4; //The Gamecube Paddle is free to move downwards on it's own.
  858.                    
  859.                 }
  860.             }
  861.    
  862.             if (BallSpeedx < 1 && Wiiy < 384)//If the ball is moving away from the wii and the wii is lower than the center position of the screen
  863.             {
  864.                 Wiiy = Wiiy +1; //The Wii paddle will move slowly upwards to the center of the screen.
  865.             }
  866.    
  867.             if (BallSpeedx < 1 && Wiiy > 384) //If the ball is moving away from the wii and the wii is higher than the center position of the screen
  868.             {
  869.                 Wiiy = Wiiy -1; //The Wii paddle will slowly move downwards to the center of the screen.
  870.             }
  871.    
  872.             if (BallSpeedx > 1 && Gamecubey < 384) //If the ball is moving away from the Gamecube and the Gamecube is lower than the center position of the screen
  873.             {
  874.                 Gamecubey = Gamecubey +1; //The Gamecube paddle will move slowly upwards to the center of the screen.
  875.             }
  876.  
  877.             if (BallSpeedx < 1 && Gamecubey > 384) //If the ball is moving away from the Gamecube and the Gamecube is higher than the center position of the screen
  878.             {
  879.                 Gamecubey = Gamecubey -1; //The Gamecube paddle will slowly move downwards to the center of the screen.
  880.             }
  881.    
  882.  
  883.             //==========================================================================================================
  884.             //      Restart The Round
  885.             //==========================================================================================================
  886.  
  887.             if (iP1RoundScore == 6)
  888.             {  
  889.                 iP1Score = 0; //Resets P1's score to zero
  890.                 iP2Score = 0; //Resets P2's score to zero
  891.                
  892.                 ClearScreen(); //Clears the screen of any previous sprites that were used on it.
  893.            
  894.                 Ballx = 512; //Sets the balls x position back to the center.
  895.                 Bally = 368; //Sets the balls y postion into the center of the screen as well.
  896.                 Gamecubey = 368; //Sets the left paddles position to the center of the screen.
  897.                 Wiiy = 368; //Sets the right paddles position to the center of the screen.
  898.                 //This prepares the game for another run in case the player decides to play again.
  899.        
  900.                 DrawString("Player 2 Wins", 270, 100, 5); //Prints the text 'Player 2 Wins' on the screen.
  901.                 DrawString("Restart Round by pressing 'Enter'", 100, 500, 2); //Prints instructions to the player on the screen.
  902.                 DrawString("Press 'BackSpace' to return to menu", 100, 530, 2); //prints instructions to the player on the screen.
  903.        
  904.                 if (IsKeyDown(KEY_RETURN)) //If the player presses 'Enter/Return', the scores are reset, exiting the IF statement loop and restarting the game.
  905.                 {
  906.                     iP1RoundScore = 0; //resets the player 1's round score to zero
  907.                     iP2RoundScore = 0; //resets the player 2's round score to zero
  908.                 }
  909.             }
  910.  
  911.             if (iP2RoundScore == 6)
  912.             {
  913.                 iP1Score = 0; //Resets P1's score to zero
  914.                 iP2Score = 0; //Resets P2's score to zero
  915.                
  916.                 ClearScreen(); //Clears the screen of any previous sprites that were used on it.
  917.            
  918.                 Ballx = 512; //Sets the balls x position back to the center.
  919.                 Bally = 368; //Sets the balls y postion into the center of the screen as well.
  920.                 Gamecubey = 368; //Sets the left paddles position to the center of the screen.
  921.                 Wiiy = 368; //Sets the right paddles position to the center of the screen.
  922.                 //This prepares the game for another run in case the player decides to play again.
  923.        
  924.                 DrawString("Player 2 Wins", 270, 100, 5); //Prints the text 'Player 2 Wins' on the screen.
  925.                 DrawString("Restart Round by pressing 'Enter'", 100, 500, 2); //Prints instructions to the player on the screen.
  926.                 DrawString("Press 'BackSpace' to return to menu", 100, 530, 2); //prints instructions to the player on the screen.
  927.        
  928.                 if (IsKeyDown(KEY_RETURN)) //If the player presses 'Enter/Return', the scores are reset, exiting the IF statement loop and restarting the game.
  929.                 {
  930.                     iP1RoundScore = 0; //resets the player 1's round score to zero
  931.                     iP2RoundScore = 0; //resets the player 2's round score to zero
  932.                 }
  933.             }
  934.    
  935.         //==========================================================================================================
  936.         //      Quit
  937.         //==========================================================================================================
  938.    
  939.         //BackSpace will quit to the Main Menu.
  940.         if (IsKeyDown(KEY_BACKSPACE))
  941.             break;
  942.  
  943.         Sleep(5); //Will slow the frame-rate down to a more slower paced speed for the human eyes to be able to see what is happening.
  944.  
  945.     }while(FrameworkUpdate()); //Will repeat all those functions as long as the frames keep bing updated.
  946.  
  947. }
  948.  
  949. void GameState_HIGHSCORES ()
  950. {
  951.     do
  952.     {
  953.         ClearScreen(); //Refreshes the screen from any previous sprites that were on it.
  954.  
  955.         for(int i=0; i<10; i++) //Creates another 'int i' Variable for use only within this function. Scans to make sure that the 'i' variable is below 10 and adds +1 to it everytime it loops.
  956.         {
  957.             char cScoreTextArray[32]; //Creates an Array to store and show the highscores.
  958.             sprintf_s( cScoreTextArray, "%i:    %i", i, HighScoreTable[i]); //Scans the ScoreTextArray and then prints out the defined values from 0 - 9 based on the 'i' variable, then adds the highscoretable based on the 'i' variable as well.
  959.  
  960.             float xPos = 10; //sets the initial x position of the table to 10
  961.             float yPos = 10 + i*10; // sets the initial y position of the table to ten and then adds 10 on to itself everytime the 'i' values increases.
  962.  
  963.             DrawString(cScoreTextArray, xPos, yPos, 1.0f); //Draws the text for the array that shows the player the current highscores and what positions they are.
  964.         }
  965.  
  966.         if (IsKeyDown(KEY_BACKSPACE)) //If 'backspace' is pressed,
  967.             break; //quit from the function and return to the main menu.
  968.  
  969.        
  970.         Sleep(5); //Will slow the frame-rate down to a more slower paced speed for the human eyes to be able to see what is happening.  
  971.                
  972.  
  973.     }while(FrameworkUpdate());
  974. }
  975.            
  976.  
  977.  
  978. int main(int argc, char* argv[]) //Begins the 'main' function.
  979. { //Beginning Scope for the main function of the C++ Program.
  980.  
  981.     for(int i = 0; i < 10; ++i) //Creates the 'i' Variable that will create the HighScoreTable at the beginning of the game.
  982.     {
  983.         HighScoreTable[i] = 0; //Creates all the highscore Array variables to start off as zero.
  984.     }
  985.  
  986.     InitGL(1024,768); //Initialises Open-GL, which will be used for the duration of the game.
  987.  
  988.     int Menu_BackGround     = LoadTexture("./images/Menu_Background.png"); //Default Background (In case the game somehow breaks from the gamestate loop)
  989.     int Menu_BackGround2    = LoadTexture("./images/Menu_Background2.png"); //P1 VS AI - Background
  990.     int Menu_BackGround3    = LoadTexture("./images/Menu_Background3.png"); //P1 VS P2 - Background
  991.     int Menu_BackGround4    = LoadTexture("./images/Menu_Background4.png"); //P1 VS Wall - Background
  992.     int Menu_BackGround5    = LoadTexture("./images/Menu_Background5.png"); //AI VS AI - Background
  993.     int Menu_BackGround6    = LoadTexture("./images/Menu_Background6.png"); //EXIT - Background
  994.     int Menu_BackGround7    = LoadTexture("./images/Menu_Background7.png"); //HighScores - Background
  995.     //Includes the menu backgrounds that will be used whilst the game is focused on the Main Menu.
  996.  
  997.  
  998.    
  999.     bool g_isKeyDownPressed = false; //Checks to make sure that the 'Down' key isn't currently being pressed and is changed to 'true' when the key is pressed.
  1000.     bool g_isKeyUpPressed = false; //Checks to make sure that the 'Up' key isn't currently being pressed and is changed to 'true' when the key is pressed.
  1001.     //A boolean type variable can only hold a true or false answer.
  1002.     //It can be written as either 'true' or 'false', or you can type a number to correspond to thos values.
  1003.     //'0' will always equal false and any other number (even negative numbers) will always equal true.
  1004.  
  1005.  
  1006.  
  1007.     enum eMenu_Select { eMS_P1_VS_AI, eMS_P1_VS_P2, eMS_P1_VS_WALL, eMS_AI_VS_AI, eMS_EXIT, eMS_HIGHSCORES};
  1008.     //Creates Menu States that change depending on which option is currently selected.
  1009.    
  1010.     int Menu_Selection = 0;
  1011.     //Creates the 'Menu_Selection' Variable and sets it's value to zero.
  1012.  
  1013.  
  1014.     do //a 'do while' loop will always do whatever functions are inside of its scope at least once. However, it will continue to loop through those functions if the conditions for the 'while()' are met.
  1015.     {
  1016.                    
  1017.         ClearScreen(); //Refreshes the screen from any previous sprites that were on it.
  1018.  
  1019.  
  1020.  
  1021.                 if (IsKeyDown(KEY_UP) && !g_isKeyUpPressed) //If the end user presses the 'Up' key, then do the following. '!g_IsKeyUpPressed' checks to make sure that the player is not holding 'Up' and if they are, the If statement only executes once.
  1022.                 {
  1023.                     Menu_Selection--; //If the 'Up' key is pressed, the Menu_Selection counter will be decreased by one, thus changing the option from 'case 2' to 'case 1', etc.
  1024.                     g_isKeyUpPressed = true; //Changes the boolean variable to true and will keep it as true so long as the 'Up' key is pressed. This ensures that the options can only be selected one at a time.
  1025.                 }
  1026.                 else if(!IsKeyDown(KEY_UP)) //If key 'Up' is NOT pressed, the boolean variable will be set to false so that the previous option can once again be selected.
  1027.                 {
  1028.                     g_isKeyUpPressed = false; //Sets the boolean to false.
  1029.                 }
  1030.            
  1031.  
  1032.                 if (IsKeyDown(KEY_DOWN) && !g_isKeyDownPressed) //If the end user presses the 'Down' key, then do the following. '!g_IsKeyDownPressed' checks to make sure that the player is not holding 'Down' and if they are, the If statement only executes once.
  1033.                 {
  1034.                     Menu_Selection++; //If the 'Down' key is pressed, the Menu_Selection counter will be increased by one, thus changing the option from 'case 3' to 'case 4', etc.
  1035.                     g_isKeyDownPressed = true; //Changes the boolean variable to true and will keep it as true so long as the 'Up' key is pressed. This ensures that the options can only be selected one at a time.
  1036.                 }
  1037.                 else if (!IsKeyDown(KEY_DOWN))//If key 'Down' is NOT pressed, the boolean variable will be set to false so that the next option can once again be selected.
  1038.                 {
  1039.                     g_isKeyDownPressed = false; //Sets the boolean Variable to false.
  1040.                 }
  1041.  
  1042.  
  1043.  
  1044.                 if (Menu_Selection < 0)//Should the player press the 'Up' key whilst on the 'case 0' switch
  1045.                 {
  1046.                     Menu_Selection = 5;// the case is switched from a negative number (which causes the game to crash) to option 5.
  1047.                 }
  1048.  
  1049.                 if (Menu_Selection > 5)//Should the player press the 'Down' key whilst on 'option 5'
  1050.                 {
  1051.                     Menu_Selection = 0;// the option is switched from a case that wouldn't exist to the very first option.
  1052.                 }
  1053.  
  1054.  
  1055.  
  1056.  
  1057.         switch (Menu_Selection)//Switches the Menu_Selection between the cases it holds. Currently there are 6 cases (0 - 5).
  1058.         {
  1059.             case 0: //By default, the game starts the menu on this option.
  1060.  
  1061.                 DrawSprite(Menu_BackGround2, 0, 0, 1028, 768); //Draws the sprite background for this option when it is selected.
  1062.  
  1063.                 if(IsKeyDown(KEY_RETURN))//If 'Enter/Return' is pressed
  1064.                 {
  1065.                     GameState_P1_VS_AI();//The game will enter another Function called 'GameState_P1_VS_AI', exiting the menu screen and entering one of the game screens.
  1066.                 }
  1067.                 break; //If the case is no longer set to zero, the game will break out of the loop.
  1068.                 //The break function is important because if it wasn't there, the program would treat 'case 1' as a prat of 'case 0' as well (which will cause an error).
  1069.                            
  1070.  
  1071.  
  1072.  
  1073.             case 1: //This is the second option on the list, should the player press the 'Down' key whilst on option 1, they would be taken here. The same holds true if they pressed the 'Up' key whilst on option 3.
  1074.  
  1075.                 DrawSprite(Menu_BackGround3, 0, 0, 1028, 768);//Draws the sprite background for this option when it is selected.
  1076.  
  1077.                 if (IsKeyDown(KEY_RETURN))//If 'Enter/Return' is pressed
  1078.                 {
  1079.                     GameState_P1_VS_P2();//The game will exit this function and enter another function called 'GameState_P1_VS_P2', exiting the menu screen and replacing it with the game screen for the new function.
  1080.                 }
  1081.                 break;//Breaks out of the case loop.
  1082.  
  1083.  
  1084.  
  1085.             case 2://This is the third option to be selected.
  1086.        
  1087.             DrawSprite(Menu_BackGround4, 0, 0, 1028, 768);//Draws the sprite background for the option selected.
  1088.  
  1089.             if (IsKeyDown(KEY_RETURN))//If 'Enter/Return is pressed whilst this option is highlighted
  1090.             {
  1091.                 GameState_P1_VS_WALL();//The game will enter another function called 'GameState_P1_VS_WALL'.
  1092.             }
  1093.             break;//Breaks out of the case loop.
  1094.        
  1095.            
  1096.  
  1097.         case 3://This is the fourth option to be selected.
  1098.        
  1099.             DrawSprite(Menu_BackGround5, 0, 0, 1028, 768);//Draws the sprite background for the option selected.
  1100.  
  1101.             if (IsKeyDown(KEY_RETURN))//If 'Enter/Return is pressed whilst this option is highlighted
  1102.             {
  1103.                 GameState_AI_VS_AI();//The game will enter another function called 'GameState_AI_VS_AI'.
  1104.             }
  1105.             break;//Breaks out of the case loop.
  1106.        
  1107.  
  1108.  
  1109.         case 4://This is the fifth option to be selected.
  1110.  
  1111.             DrawSprite(Menu_BackGround6, 0, 0, 1028, 768);//Draws the sprite background for the option selected.
  1112.  
  1113.             if (IsKeyDown(KEY_RETURN))//If 'Enter/Return is pressed whilst this option is highlighted
  1114.             {
  1115.                 return 0;//The game will close.
  1116.             }
  1117.             break;//Breaks out of the case loop.
  1118.  
  1119.         case 5://This is the sixth option to be selected.
  1120.  
  1121.             DrawSprite(Menu_BackGround7, 0, 0, 1028, 768);//Draws the sprite background for the option selected.
  1122.  
  1123.             if (IsKeyDown(KEY_RETURN))//If 'Enter/Return is pressed whilst this option is highlighted
  1124.             {
  1125.                 GameState_HIGHSCORES();//The game will enter another function called 'GameState_HIGHSCORES'.
  1126.             }
  1127.             break;//Breaks out of the case loop.
  1128.  
  1129.         }
  1130.             if (IsKeyDown(KEY_ESCAPE))//If the 'Escape' key is pressed whilst on the menu
  1131.         {
  1132.             return 0;//The game will close.
  1133.         }
  1134.        
  1135.        
  1136.         Sleep(5);//Will slow the frame-rate down to a more slower paced speed for the human eyes to be able to see what is happening.
  1137.  
  1138.            
  1139.        
  1140.     }while(FrameworkUpdate());//Will continue to loop through the 'do while loop' as long as the Frames keep on updating.
  1141.  
  1142.     return 0; //Closes the game if the game manages to exit the 'while' loop.
  1143.     //return 0; closes the game. But the 'return' command can be used to give a number back to a variable.
  1144.     //If I used a return value on any of my int functions, I could have returned a number to whatever function
  1145.     //called the return, it's commonly a process of killing two birds with one stone.
  1146.  
  1147.  
  1148. } //Ends the scope of the 'main' function.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement