KonsolScript:Tutorials:Pong:5 GettingPoint
From GPWiki
The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server. However, the GPWiki forums are still active! Come say hello. [edit] Making your own PongPart 5 of a Making your own Pong series. [edit] Getting a PointNow that we have Paddles to hit a Ball moving (or bouncing ^_^) around, we now have a working Pong game. All we need to do is some scoring. We need to design a simple pointing system. A player gets a point when the opponent missed the ball – and vice versa. A winner will be announced when the score of a player amounts to, ...let us say, 10. With this information, this requires us to have two Number variables to be used to keep track of the scores. Given this, let us add the code below: //variables for scores
Var:Number Ps, Cs;
The Ps' will be used for player-score and Cs for computer-score.
[edit] Displaying the ScoresThe first thing we might want to do is to display the scores, which could also be useful for the players. I suggest that we use the Screen:PrintString function. //syntax of Screen:PrintString Screen:PrintString("messsage") The command does as it says, it prints a string value to the primary image buffer, which we call the screen.
//syntax of Screen:GoToXY Screen:GoToXY(100,10)
//proposed code for displaying the scores Screen:GoToXY(100,10) Screen:PrintString("Player Paddle : " + Ps) Screen:GoToXY(100,30) Screen:PrintString("Opponent Paddle : " + Cs)
[edit] Player Getting PointFor a player to get a point, the opponent should miss the ball. How do we determine if the opponent missed the ball? -- when the ball bounces-off the right-most boundary. Do you remember the code that makes the ball bounce-off the right-most boundary? It is the code written below. //code to bounce the ball back to player if (Bx GE 315) { Bx = 314; TowardsPlayer = true; } When the ball is horizontally located to 315 or more, we set the ball direction towards the player. Given that code, we just have to add 1 to the player-score using Ps++;, like the one I coded below. if (Bx GE 315) { Ps++; Bx = 314; TowardsPlayer = true; }
[edit] Opponent Getting PointFor an opponent to get a point, the player should miss the ball. We determine that the player missed the ball when the ball bounces-off the left-most boundary. The code that makes the ball bounce-off the left-most boundary is the code written below. if (Bx LE 5) { Bx = 6; TowardsPlayer = false; } When the ball is horizontally located to 5 or less, we set the ball direction towards the opponent. Now, we just have to add 1 to the opponent-score using Cs++;, like the one I coded below. if (Bx LE 5) { Cs++; Bx = 6; TowardsPlayer = false; }
[edit] Determining a WinnerLike I stated earlier, a player who first gets a score of 10 is the winner. This will really be simple now that we have scores to check if it is equal to 10. The code could look like the one below. //proposed code for checking who wins if (Ps EQ 10) { //player wins } else if (Cs EQ 10) { //opponent wins }
//proposed sub-looping for displaying a winner while (B3 EQ false) { Screen:GoToXY(100, 100) Screen:PrintString("Player Wins!!!") Screen:Render() } Aside from displaying the winner, we also need to reset the scores back to 0.
//revised proposed code for checking who wins function CheckWinner() { if (Ps EQ 10) { while (B3 EQ false) { Screen:GoToXY(100, 100) Screen:PrintString("Player Wins!!!") Screen:Render() } Ps = 0; Cs = 0; Bm = 2; } else if (Cs EQ 10) { while (B3 EQ false) { Screen:GoToXY(100, 100) Screen:PrintString("Opponent Wins!!!") Screen:Render() } Ps = 0; Cs = 0; Bm = 2; } } But the code above also starts to become messy and looks repetitive. Look at the common codes. We can prevent it from becoming messy by using a function. Look at the code below. //revised proposed code for checking who wins function CheckWinner() { if (Ps EQ 10) { DisplayWinner("Player") ResetGame() } else if (Cs EQ 10) { DisplayWinner("Opponent") ResetGame() } } function DisplayWinner(String winner_name) { while (B3 EQ false) { Screen:GoToXY(100, 100) Screen:PrintString(winner_name + " Wins!!!") Screen:Render() } } function ResetGame() { Ps = 0; Cs = 0; }
[edit] Playing the Game and Getting a PointIf you play the game for some time, you will notice that the opponent could not just missed the ball – this leaves you ending the game score-less. The game needs a little tweak when it comes to gameplay. How about, “when the ball is hit by any player, the ball moves faster than it was”? But we also reset it back to the ordinary speed when someone missed it. This way the ball could now be missed by either the opponent or the player leaving us a fair game to play. If we all agree to the proposed tweaking, the game will require a Number variable for the speed, but I prefer to call it Bm for ball-movement. //proposed variable for the ball movement Var:Number Bm = 2; So instead of Bx -= 2; and Bx += 2;, we change it to Bx -= Bm; and Bx += Bm;.
The code below: //portion where a player-paddle hits the ball if (By LE nTmp) { TowardsPlayer = false; } //portion where a opponent-paddle hits the ball if (By LE nTmp) { TowardsPlayer = true; } Should be changed into: //portion where a player-paddle hits the ball if (By LE nTmp) { Bm++; TowardsPlayer = false; } //portion where a opponent-paddle hits the ball if (By LE nTmp) { Bm++; TowardsPlayer = true; }
if (Bx LE 5) { Cs++; Bx = 6; TowardsPlayer = false; } else if (Bx GE 315) { Ps++; Bx = 314; TowardsPlayer = true; }
if (Bx LE 5) { Cs++; Bm = 2; Bx = 6; TowardsPlayer = false; } else if (Bx GE 315) { Ps++; Bm = 2; Bx = 314; TowardsPlayer = true; } Do not forget to add reset the ball speed from inside the ResetGame function.
[edit] Last WordsMy only last words would be, to use PrintStringNoBC instead of PrintString function. The NoBC stands for No Back Color. It may not really be that helpful – it simply prints a string value without the back color so the ball would not be covered by the printed text. //proposed change code for displaying the scores Screen:GoToXY(100,10) Screen:PrintStringNoBC("Player Paddle : " + Ps) Screen:GoToXY(100,30) Screen:PrintStringNoBC("Opponent Paddle : " + Cs)
|


