I have been reading through this tutorial. I am in a chapter where you can build a pong game. However I am having trouble trying to make a function that can detect when the ball hits the paddle?
It says I should do this:
Make a boolean function
hit(bx, by, r, px, py, h)that returnsTruewhen the vertical coordinate of the ball (by) is between the bottom and top of the paddle, and the horizontal location of the ball (bx) is less than or equal to the radius (r) away from the front of the paddle.
I have tried this so far:
def hit(bx, by, r, px, py, h):
if by > py and by <= h:
if bx <= r:
return True
else:
return False
...
# If this returns True then change the direction of the ball (dx).
if hit(ball_x, ball_y, radius, paddle_x, paddle_y, height):
dx *= -1
I'm having trouble translating the quoted paragraph into code. What am I doing wrong?
Note: The expected output of this function would be:
hit(760, 100, 10, 780, 100, 100)
False
hit(770, 100, 10, 780, 100, 100)
True
hit(770, 200, 10, 780, 100, 100)
True
hit(770, 210, 10, 780, 100, 100)
False