如何在相同位置旋转图像?

时间:2019-03-29 16:18:09

标签: python pygame pygame-surface

我目前正在尝试编写一款游戏,其中包括一个降落在垫板上的小机器人。在尝试弄清其下落的物理原理时,我遇到了旋转问题。按左右方向分别旋转。

我尝试过将blit与rect.center一起使用,但它似乎仍然无法正常工作。任何帮助将不胜感激!

 def rotate_right(self):
    self.new_angle = self.angle + 30
    self.rotate_lander()   

 def rotate_lander(self):
    self.image = pygame.transform.rotozoom(self.image, self.new_angle, 1)
    self.rect = self.image.get_rect(center=self.image.get_rect().center)
    screen.blit(self.image, self.rect.center)

我设法使其旋转,但是它每旋转一次都会移动,因此我需要将其保持在同一位置。我认为中心不在,但我不确定它可能在哪里出了错。

1 个答案:

答案 0 :(得分:0)

首先,在 rotate_lander()中,您应始终旋转原始图片,否则它将变形(并且在您的情况下,请逃开)。因此,创建另一个 self.image 副本,您将不会更改。

original = pygame.Surface(...)
...
def rotate_lander(self):
   self.image = pygame.transform.rotozoom(self.original, self.new_angle, 1)

但是现在,图像仍然不会在完全相同的位置旋转。 问题是 image 的边界框正在更改。这样,图像的每个点的位置都会改变。您不应将 self.rect 的位置设置为中心,因为它正在移动。 相反,您必须根据边界框的更改来更新位置。您应该比较旋转前后的边界框。 您已经在此处回答了有关此主题的完整教程:

Rotate an image around its center

*如果只希望将图像保留在适当的位置(而不是围绕图像的中心旋转),则可以摆脱 center

 def rotate_lander(self):
   self.image = pygame.transform.rotozoom(self.image, self.new_angle, 1)
   self.rect = self.image.get_rect()
   screen.blit(self.image, self.rect)
相关问题