Stopping the game when touching sides pong game

Hi,

Can you please format your code by using the </> button in the message editor on the forum? (You can press Ctrl+T in the Processing IDE to auto format your code :slight_smile: )

Because for now it’s actually impossible to read or copy into Processing to test it. (I already told you this on I need help to keep score)

And please do not post the same code on multiple threads, try to summarize your problem…
(Bouncing ball with paddle)
(I need help to keep score)
(Creating menu for game and touching sides pong game)

But testing if your ball touches left or right side is not complicated :

  • The location of your ball is described by x and y
  • It’s diameter is w and h (or just w because a ball is always circular in pong)
  • The left side of the screen is at coordinates [0, y] (y can be whatever)
  • The right side of the screen is at coordinates [width, y]

So in order to test if it touches the left wall, you test if the x coordinate of the ball is inferior (on the left side) to 0 (the left wall), you need to take into account the diameter of the ball (in fact it’s radius) :

boolean touchLeftWall(x, diameter){
  return (x - diameter / 2) <= 0;
}

This function returns a boolean (true or false) that tells if the ball hit the left wall. You can do the same for the right wall.

1 Like