views:

45

answers:

1

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 returns True when 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
+2  A: 

You're not accounting for the actual locations of the items - the coordinates you have refer to a specific point on the ball and paddle. What your code does is check that the ball is above the paddle and has a bigger radius than the distance from the y-origin to the paddle, then check that it's wider than the distance from the x-origin to the paddle.

Assuming that (px, py) is the top-left corner of the paddle, you could try something like this:

def hit(bx, by, r, px, py, h):
    if by >= py and by <= py + h:
        print "Y satisfied."
        if bx <= px + r:
            print "HIT"
            return True
        print "X not satisfied."
    print "not hit."
    return False

Bear in mind this doesn't account for the ball being round (or whatever other shape you have in mind).

EDIT: If you're having issues with making it work, you could try sticking in some print statements to let you know what the values of your parameters are and what the return value is. This should give you some insights into what is actually going on.

EDIT AGAIN: Realised that there was a possibility that the function didn't return a value. Also, this code doesn't pass all the test cases - it counts the first one as a hit.

Nathan Tomkins
It's not working. The ball is moving all crazy and it never touches my paddle. Also the ball is round.
Bob Dylan
@Bob: I got the code wrong on the first try, did you get it before or after I edited it? Having said that, it may still be wrong anyway - I'm not awesome at these things.
Nathan Tomkins
@Nathan: I tried it after your update and the ball is no longer moving crazy. However, the ball just goes through the paddle (meaning it never returns True).
Bob Dylan
@Nathan: I also tried the print statements with your code running. Here is the output: http://pastebin.com/m1zAr6GS **I made sure the ball hit the paddle on this test, but it of course just went through it.**
Bob Dylan
I tried your second edit: Now when ever the ball and the paddle cross paths it counts it as a hit, regardless of weather or not the ball and paddle are touching. Also thanks so much for your help so far.
Bob Dylan
I have now pasted my full code here in hopes that it might help. http://pastebin.com/rmT8RKk3
Bob Dylan
Okay so I can't test this right now but maybe if I changed the second if to this: `by == px - r`. This should satisy the test. If this works, I'll give you the accepted answer.
Bob Dylan
After testing `by == px - r` it worked perfectly. Thanks for pointing me in the right direction.
Bob Dylan