KonsolScript:Tutorials:Pong:5 GettingPoint

From GPWiki
Jump to: navigation, search

Making your own Pong

Part 5 of a Making your own Pong series.

Getting a Point

Now 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.


Displaying the Scores

The 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.


We also need to print the scores at a certain location. We need the GoToXY function of the Screen class. //syntax of Screen:GoToXY

 Screen:GoToXY(100,10)


Below is the proposed code, I suggest that you place it inside the DrawPong function, just after drawing the ball and paddles. //proposed code for displaying the scores

 Screen:GoToXY(100,10)
 Screen:PrintString("Player Paddle   : " + Ps)
 Screen:GoToXY(100,30)
 Screen:PrintString("Opponent Paddle : " + Cs)


Player Getting Point

For 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;  }


Opponent Getting Point

For 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; }


Determining a Winner

Like 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
 }


I say, we use a sub-looping to display the winner and do a little pause using a while loop. //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.


Though the code is a little short, I advise that a separate function is created for this. Maybe, we could call it CheckWinner. //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;

}


Be sure to call the function, CheckWinner, from within our main loop.


Playing the Game and Getting a Point

If 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;.


We also have to code the increasing of the speed when ball is hit by a paddle. We just have to add Bm++; to the code where a paddle gets to hit the ball.

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; }


Lastly, we need to reset the ball speed when a player gets a score, which should be placed in the code below.

 if (Bx LE 5)   { Cs++; Bx = 6;   TowardsPlayer = false; } else
 if (Bx GE 315) { Ps++; Bx = 314; TowardsPlayer = true;  }


Just remember that the initial speed is 2 from which we derive the code, Bm = 2;, like the one coded below.

 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.


Last Words

My 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)


~creek23~