Pygame图像旋转

时间:2016-04-09 19:49:31

标签: python pygame

我用Python和Pygame制作一个简单的程序自上而下驾驶模拟器。驱动部分工作正常;向上键移动汽车,左右键操纵它。问题在于图像的旋转;它移动到处(运行代码来查看它)。我认为这是因为当图像旋转时,尺寸会发生变化(因为它必须是一个矩形)。当分辨率改变时,位置也会改变。如果有人知道解决这个问题,我会非常感激。 car.png - > enter image description here

do {
  f++;
  scanf("%f",&paid[f]);
} while(paid[f-1] != 0 && paid[f] != EOF);

1 个答案:

答案 0 :(得分:1)

来自PYgame文档:

  

除非以90度为增量旋转,否则图像将被填充得更大以保持新尺寸。

这意味着您将获得一个新的Rect对象。文档中没有任何信息,但我认为原始图像将位于新Rect的中心。
这意味着如果您将新的Rect位置放置到所需的位置,您将获得预期的结果。

center=(100,200) #Store pos by center

#In loop:
rCar=pygame.transform.rotate(car,r) #Do the rotation
size=rCar.get_size() #Store size
hSize=[n/2 for n in size] #Half the size
pos=(center[0]-hSize[0],center[1]-hSize[1])  #Substract half the size
#from the center
screen.blit(rCar,pos) #Draw

让我解释一下:

enter image description here

我希望它对你有所帮助。