Workshop › Pong › Step 13 of 26
When the ball leaves the court at the top or bottom, it bounces: the vertical speed flips sign, and the ball is put back inside so it cannot get stuck in the wall.
```js
if (ball.y < 0 || ball.y + BALL > canvas.height) {
ball.vy = -ball.vy
ball.y = clamp(ball.y, 0, canvas.height - BALL)
}
```ball.y < 0 means the top edge went above the court; ball.y + BALL > canvas.height the bottom edge went below it.
|| means "or".ball.vy = -ball.vy flips the direction.clamp puts the ball back inside the court.At the end of update, under the two ball lines, write the if block.
Skills: Velocity and gravity, Collision detection
← The ball moves Does the ball touch a paddle? →