Pygame rect.contains 未检测到碰撞

Pygame rect.contains collision not detecting

我正在制作一个简单的 Breakout/Arkanoid 游戏来学习 pygame。我 运行 遇到了球拍和球的矩形未正确碰撞的问题。我还注意到,如果我在两块砖块之间射球,球不会与砖块发生碰撞,即使球精灵在视觉上与砖块重叠也是如此。此代码段来自球的 .update 方法,该方法传入球拍和砖块列表。

    new_pos = self.__calc_pos()

    # Check for collision with walls
    if not self.area.contains(new_pos):
        self.angle = -self.angle
        new_pos = self.__calc_pos()
    else:
        # Check for collision with paddle
        if paddle.rect.contains(new_pos):
            self.angle = -self.angle
            new_pos = self.__calc_pos()

        # Check for collision with bricks
        for brick in bricks:
            if brick.rect.contains(new_pos):
                self.angle = -self.angle
                new_pos = self.__calc_pos()
                brick.kill()
                bricks.remove(brick)

    self.rect = new_pos

.__calc_pos方法:

def __calc_pos(self):
    new_x = int(math.cos(math.radians(self.angle))) * self.speed
    new_y = -int(math.sin(math.radians(self.angle))) * self.speed
    return self.rect.move(new_x, new_y)

contains() checks if one rect if fully inside another rect - and it doesn't true if one object only partially touch other object. Use colliderect()

contains()
test if one rectangle is inside another
contains(Rect) -> bool
Returns true when the argument is completely inside the Rect.

-

colliderect()
test if two rectangles overlap
colliderect(Rect) -> bool
Returns true if any portion of either rectangle overlap (except the top+bottom or left+right edges).