Pygame - CollideRect - But How Do They Collide?

Posted by Chakotay on Game Development See other posts from Game Development or by Chakotay
Published on 2013-09-03T20:03:58Z Indexed on 2013/10/24 4:10 UTC
Read the original article Hit count: 169

I'm having some trouble figuring out how I can handle collisions that are specifically colliding on the top or bottom a rect. How can specify those collisions?

Here's a little code snippet to give you an idea of my approach. As it is now it doesn't matter where it collides.

# the ball has hit one of the paddles. send it back in another direction.
    if paddleRect.colliderect(ballRect) or paddle2Rect.colliderect(ballRect):
        ballHitPaddle.play()
        if directionOfBall == 'upleft':
            directionOfBall = 'upright'
        elif directionOfBall == 'upright':
            directionOfBall = 'upleft'
        elif directionOfBall == 'downleft':
            directionOfBall = 'downright'
        elif directionOfBall == 'downright':
            directionOfBall = 'downleft'

Thanks in advance.

**EDIT**

Paddle rect:

 top
 ____
|    |
|    |
|    | Sides
|    |
 ---- 
bottom

I need to know if the ball has hit either the top or the bottom.

© Game Development or respective owner

Related posts about collision-detection

Related posts about python