Pygame Rect Collision

后端 未结 2 501
太阳男子
太阳男子 2021-01-22 15:35

I am creating a game of Pong in Pygame with Python (obviously) and am new to Pygame so would like some help working the physics of when the ball touches the paddle, it will reve

相关标签:
2条回答
  • 2021-01-22 16:02

    For the collision use the code below but change the variables

    If paddle1.colliderect(paddle2) or paddle2.colliderect(paddle1): That is the ball direction in the x-axis ballDirectionX*=-1

    0 讨论(0)
  • 2021-01-22 16:15

    The code that you have is probably a little excessive. Let's go with something a bit more simple. Within your draw functions (on both Ball & Paddle), go ahead and make the start of your lines look like this:

    self.rect = pygame.draw.rect...
    

    Then you can use the colliderect function:

    if ball.rect.colliderect(paddle1):
        # Reverse, reverse!
    
    0 讨论(0)
提交回复
热议问题