使用 pygame 获取精灵角度

Get sprite angle with pygame

我 pygame 有点问题。我实际上正在用 pygame 编写一个项目,我想知道我怎么知道精灵的角度,因为 pygame.transfrom.rotate(self.player.image, 50) 正在将它从实际角度旋转 50 度,但我想从零开始旋转它。所以我想我可以将精灵的角度减去自身以使其为零,但我找不到如何找到精灵的角度。 有人可以帮我吗?

您无法从图像中获取角度(pygame.Surface). A Surface is always rectangular. pygame.transform.rotate 创建一个新的但稍大的表面。新的 Surface 的大小是旋转矩形的大小轴对齐的边界框。 如果你想知道图像是如何旋转的,你需要将角度存储在一个属性中。

无论如何,永远不要旋转已经旋转过的图像。这会导致奇怪的扭曲。参见 How do I rotate an image around its center using PyGame?

将原始图像存储在属性中。例如:

class MySprite(pygame.sprite.Sprite):
    def __init__(self, x, y, image):
        super().__init__() 
        self.original_image = image
        self.image = self.original_image
        self.rect = self.image.get_tect(center = (x, y))
        self.angle = 0

    def roatate(self, angle):
        self.angle = angle
        self.image = pygame.transfrom.rotate(self.original_image, slef.angle)
        self.rect = self.image.get_tect(center = (x, y))

    def resetRotate(self):
        self.rotate(0)