KonsolScript:Tutorials:Pong:4 HittingBall

From GPWiki
Jump to: navigation, search

Making your own Pong

Part 4 of a Making your own Pong series.

Hitting the Ball

We have made the ball bounce from around the set boundaries, but we can not even hit the ball.

What is wrong with the code?”, you ask. I say nothing – we have not coded any that will do the hitting of the ball.

Once again, before coding anything, let us discuss the situation to get information. (Nice rhyme we have there...)

We want to hit a ball. But how can we hit a ball if the ball is just a drawing – and so with a drawn paddle?

This is where a fake collision comes in with every game programming.

We base a collision according to the X-Y coordinate of the drawn objects. Remember we have Bx and By to represent the vertical and horizontal location of the ball, as well as Py for the vertical location of the player-paddle and the Cy for the opponent-paddle.

We will also utilize those in making a fake collision of the drawn objects.

Player hitting the Ball

Let us start with making a fake collision of the player-paddle and the ball.

Since the player-paddle is located at position 10 horizontally, plus the width of a paddle which is 5, a probable collision should take place somewhere 15 to 20 horizontally. The code will look like the one below.

//proposed collision
    if (Bx LE 20) {
      if (Bx GE 15) {
        //probable collision
      }
    }

But it should be where the player-paddle is located vertically. That is along Py and Py + height of player-paddle, which is 50. The code will look like the one below.

//proposed collision
    if (Bx LE 20) {
      if (Bx GE 15) {
        if (By GE Py) {
          nTmp = Py + 50;
          if (By LE nTmp) {
            //probable collision
          }
        }
      }
    }

Then finally, we have detected a collision. The code below is the proposed code for determining a fake collision.

//proposed collision
    if (Bx LE 20) {
      if (Bx GE 15) {
        if (By GE Py) {
          nTmp = Py + 50;
          if (By LE nTmp) { TowardsPlayer = false; }
        }
      }
    }

I set the value of TowardsPlayer to false since we had a collision, making the ball bounce-off the paddle.


Below, I provided the revised CheckBallMovement function.

function CheckBallMovement() {
  if (TowardsTop EQ true)    { By -= 2; } else { By += 2; }
  if (TowardsPlayer EQ true) {
    if (Bx LE 20) {
      if (Bx GE 15) {
        if (By GE Py) {
          nTmp = Py + 50;
          if (By LE nTmp) { TowardsPlayer = false; }
        }
      }
    }
    Bx -= 2;
  } else {
    Bx += 2;
  }
 
  if (By LE 5)   { By = 6;   TowardsTop = false; } else
  if (By GE 235) { By = 234; TowardsTop = true;  }
 
  if (Bx LE 5)   { Bx = 6;   TowardsPlayer = false; } else
  if (Bx GE 315) { Bx = 314; TowardsPlayer = true;  }
}

Opponent hitting the Ball

Provided that you understood how to make a fake collision of drawn objects, let us proceed making the code for the ball colliding with the opponent-paddle.

A probable collision for the opponent-paddle and the ball could also take place somewhere the horizontal value of 300 – the place where we draw the opponent-paddle and minus the radius of the ball, which would be 295.

//proposed collision
  if (Bx GE 295) {
    if (Bx LE 300) {
      //probable collision
    }
  }

Let us also consider the vertical location of the opponent-paddle, which would depend on the value of Cy.

//proposed collision
  if (Bx GE 295) {
    if (Bx LE 300) {
      if (By GE Cy) {
        nTmp = Cy + 50;
        if (By LE nTmp) {
          //probable collision
        }
      }
    }
  }


Again, a revised CheckBallMovement function is provided below.

function CheckBallMovement() {
  if (TowardsTop EQ true)    { By -= 2; } else { By += 2; }
  if (TowardsPlayer EQ true) {
    if (Bx LE 20) {
      if (Bx GE 15) {
        if (By GE Py) {
          nTmp = Py + 50;
          if (By LE nTmp) { TowardsPlayer = false; }
        }
      }
    }
    Bx -= 2;
  } else {
    if (Bx GE 295) {
      if (Bx LE 300) {
        if (By GE Cy) {
          nTmp = Cy + 50;
          if (By LE nTmp) { TowardsPlayer = true; }
        }
      }
    }
    Bx += 2;
  }
 
  if (By LE 5)   { By = 6;   TowardsTop = false; } else
  if (By GE 235) { By = 234; TowardsTop = true;  }
 
  if (Bx LE 5)   { Bx = 6;   TowardsPlayer = false; } else
  if (Bx GE 315) { Bx = 314; TowardsPlayer = true;  }
}


Last Words

My last words for this tutorial would be, maybe it is time to limit the player-paddle from going off the upper and lower bounds.

I revised the CheckPlayerMovement function to do just that.

function CheckPlayerMovement() {
  if (BU EQ true) {
    if (Py GT 0) { Py -= 7; }
  } else if (BD EQ true) {
    if (Py LT 190) { Py += 7; }
  }
}

When BU is being pressed, I check if the current vertical location of the player-paddle is lower than the top-most which is 0. If so, I move the paddle higher some more using the code “Py -= 7;”.

If BD is being pressed, let us check if the current vertical location of the player-paddle is higher than the bottom-most which is 240minus” height of a paddle (which is 50) which would give the value of 190. If so, I move the paddle lower some more using the code “Py += 7;”.

A Pong game is coming into shape. Are you excited to finish her up? Well, read on.


~creek23~