We will check which side of the paddle the ball has hit to choose the side where it will move next. It's important to have the motion to follow through any directional hits as it would in a realistic environment. Through every paddle collision, we want to make sure the ball goes in the up direction.
Create a new function called
bounce()
for the ball after themovePaddle()
function.function bounce()
Add in a value of -3 for velocity in the y-direction. This will make the ball move in an upward motion:
vy = -3
Check when a collision is made with the
paddle
andball
and close the function:if((ball.x + ball.width * 0.5) < paddle.x) then vx = -vx elseif((ball.x + ball.width * 0.5) >= paddle.x) then vx = vx end end