Pygame Rect Collision

后端 未结 2 535
太阳男子
太阳男子 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: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!
    

提交回复
热议问题