KonsolScript:Tutorials:Pong:2 MovingPaddles

From GPWiki
Jump to: navigation, search

Making your own Pong

Part 2 of a Making your own Pong series.

Moving the Paddles

//drawing the Player's paddle

 Draw:Rect(10, 95, 5, 50, 654897)
 

//drawing the Opponent's paddle

 Draw:Rect(300, 95, 5, 50, 12345678)

Given the code above, there will be no way to move the locations of the paddles. It will always be located at the (10, 95) and (300, 95) coordinate of the viewing screen.

In order to change the location, we will utulize a Number variable. But before doing some changes, keep in mind that the paddles in the original Pong can only move vertically. How about the code below? //variables used

 Var:Number Py = 95, Cy = 95;

//drawing the Player's paddle

 Draw:Rect(10, pY, 5, 50, 654897)
 

//drawing the Opponent's paddle

 Draw:Rect(95, Cy, 5, 50, 12345678)

From there, we can make a way to dynamically change the vertical location of the paddles.


The Player Paddle

Let us start with the player paddle. Since it is the player's paddle, let us utilize the built-in keys.

I propose that when you press arrow down, the player-paddle goes down. And when you press arrow up, the player-paddle moves up.

Look at the code below. //proposed code that will be moving the player's paddle

 if (BU EQ true) {
   Py -= 7;
 } else if (BD EQ true) {
   Py += 7;
 }

Remember, that a vertical value of 0 is located at the top-most of the viewing screen. Thus, subtracting a certain value to the Py will result of relocating the player-paddle higher.

If we repeatedly check if BU or BD is pressed, we can repeatedly change the vertical location of the player-paddle.

In the code below, we placed the drawing of the paddles and the ball and the checking of user-input inside a scope of a while loop. function main() {

 Var:Number Py = 95, Cy = 95;
 Screen:Show()
 while (B1 EQ false) {
   Screen:CLS()
   Draw:Circ(80, 20, 5, 321654)
   Draw:RectFill(10, Py, 5, 50, 654897)
   Draw:RectFill(300, Cy, 5, 50, 12345678)
   if (BU EQ true) {
     Py -= 7;
   } else if (BD EQ true) {
     Py += 7;
   }
   Screen:Render()
 }

}

Please bear in mind that the code for moving the player-paddle is not yet final.


The Opponent Paddle

To be honest, coding the opponent-paddle's movement is quite early.

If we try to analyze, the artificial intelligence should determine the current location of the 'Ball. Because the goal in this game is to hit the ball and let the opponent miss for a player to score. Without a moving ball around, yet, there is nothing to anticipate.

Let us pretend that a Number variable, By, is already declared and is being used for vertical location of the ball. Var:Number nTmp; nTmp = Cy + 25; if (nTmp LT By) {

 if (Cy LT 190) {
   Cy += 3;
 }

} else {

 if (Cy GT 0) {
   Cy -= 3;
 }

} Based from the code above, we are determining the vertical location of the opponent-paddle based on the ball's vertical location.

A Number variable, nTmp, is declared to get the vertical value from the center of the opponent-paddle. The value 25 is half of the opponent-paddle's length.

We coded that, if the center of opponent-paddle is higher than the location of the ball, then we move downward (Cy += 3;). Otherwise, we move upward (Cy -= 3;). But before we move to whatever direction, we check if we are going out-of-bounds.

Before we try to move upwards, we checked if the opponent-paddle's location is already in the top-most (if (Cy GT 0)). Same as before we move downward.

The code above is translated into: compute for the opponent-paddles center is it higher than the ball {

 are we NOT on the bottom-most part of the viewing screen {
   not yet, so move lower
 }

} is it lower than the ball {

 are we NOT on the top-most part of the viewing screen {
   not yet so, move higher
 }

}

Like the player-paddle movement code, this is also just a portion of what will be a complete AI for the opponent-paddle.

So, a partially (and not yet working) movement code for the opponent-paddle will be coded like the one below. function main() {

 Var:Number Py = 95, Cy = 95, By = 95, nTmp;
 Screen:Show()
 while (B1 EQ false) {
   Screen:CLS()
   Draw:Circ(80, 20, 5, 321654)
   Draw:RectFill(10, Py, 5, 50, 654897)
   Draw:RectFill(300, Cy, 5, 50, 12345678)
   if (BU EQ true) {
     Py -= 7;
   } else if (BD EQ true) {
     Py += 7;
   }
   
   nTmp = Cy + 25;
   
   if (nTmp LT By) {

if (Cy LT 190) { Cy += 3; } } else { if (Cy GT 0) { Cy -= 3; }

   }
   Screen:Render()
 }

} If you execute the code above, you could see the the opponent paddle moves (or share) on its own.

Last Words

Like I said earlier, the code above is just a portion of whatever the whole source code for Pong will be. Before we proceed to adding more codes, it is very well suggested that the function main should be broken down into more different functions. See what I did to the code below. Var:Number Py = 95, Cy = 95, By = 95, nTmp;

function main() {

 Screen:Show()
 while (B1 EQ false) {
   Screen:CLS()
   DrawPong()
   CheckPlayerMovement()
   CheckAIMovement()
   Screen:Render()
 }

}

function DrawPong() {

 Draw:Circ(80, 20, 5, 321654)
 Draw:RectFill(10, Py, 5, 50, 654897)
 Draw:RectFill(300, Cy, 5, 50, 12345678)

}

function CheckPlayerMovement() {

 if (BU EQ true) {
   Py -= 7;
 } else if (BD EQ true) {
   Py += 7;
 }

}

function CheckAIMovement() {

 nTmp = Cy + 25;
 
 if (nTmp LT By) {
   if (Cy LT 190) {
     Cy += 3;
   }
 } else {
   if (Cy GT 0) {
     Cy -= 3;
   }
 }

}

Breaking down a code into separate functions can make your code manageable. But breaking down a code requires that you deeply understand the code you wrote. Otherwise, you will simply breaking down a working code into several not-working pieces.


~creek23~