The ball needs to move in a continuous motion without gravity affecting it. We'll have to take into account the side walls and the top and bottom walls. The velocity in the x and y direction have to reflect the other way when a collision happens on any of the boundaries. We need to set coordinates so that the ball is only allowed to move through and alert when it passes through the area below the paddle region. Let's perform the following steps:
function updateBall()
below the removeBrick(event)
function:function updateBall()
ball.x = ball.x + vx ball.y = ball.y + vy
if ball.x < 0 or ball.x + ball.width > display.contentWidth then vx = -vx end
The following screenshot shows the movement of ball in the x direction:
if ball.y < 0 then vy = -vy end
The following screenshot shows the movement of the ball in the y direction:
"lose"
. Close the function with end
:if ball.y + ball.height > paddle.y + paddle.height then alertScreen("YOU LOSE!", "Play Again") gameEvent = "lose" end end
The following screenshot shows the lost alert screen when the ball collides with the bottom of the game play screen:
Everywhere the ball travels, proper direction change is needed when it hits the wall. Any time the ball hits the side walls, we used vx = -vx
. When the ball hits the top boundary, vy = -vy
is used. The only time the ball doesn't reflect the opposite direction is when it hits the bottom of the screen.
The alert screen displays the lose condition, which emphasizes to the player to play again. The gameEvent = "lose"
statement will be used in another if
statement to reset the current level.