# Breakout: Bouncing the Ball Up and Down

I'm not sure how to make the ball bounce up and down.
Here's my code:

``````while (lives > 0 && bricks > 0)
{
double velocity = 2.0;

move(ball, 0, velocity);

if (getY(ball) <= 0)
{
velocity = -velocity;
}
else if (getY(ball) + 2 * RADIUS >= getHeight(window))
{
velocity = -velocity;
}

pause(10);
}
``````
• what is happening with the current code you posted? Is the ball moving? Do you have another variable for horizontal velocity? Aug 25 '14 at 16:05
• the ball move down and diseapear out of the window,it's like the else if condition doesn't work!!! Aug 25 '14 at 16:09

## 1 Answer

If the ball is moving out of the window at the bottom edge, it means that your boundaries are likely not set appropriately.

The ball Y coordinate is calculated from the top, left corner of the ball (imagine an imaginary square around the ball, it would be the top left corner of that square).

So, to accurately calculate the boundary of the ball where it contacts the window's bottom edge, you need the y-coordinate of the ball + the height of the ball, since you want to know the BOTTOM edge of the ball. You can do 2*radius or you could use a more dynamic getHeight(ball) placeholder.

You can hard code the else if condition to meet the criteria and make sure it is getting evaluated properly by substituting the following to create a "false bottom" on the screen. Where HEIGHT is the static variable that defines the height of the window. This should make the ball bounce off of the "bottom" before it hits the bottom.

else if (getY(ball) + 2 * RADIUS >= HEIGHT-50)

However, the fact that your ball isn't bouncing off of the bottom IS the intended end-state of the game. This isn't a bad problem to have. When the game is working properly, the ball should NOT bounce off of the bottom edge. In this case, you want the user to lose a life and start over if the balls misses the paddle and goes beyond the bottom boundary of the window.