Python 如何从pycharm中的中心旋转对象?

Python 如何从pycharm中的中心旋转对象?,python,python-3.x,pygame,Python,Python 3.x,Pygame,我试图在pycharm中旋转一个图像,由于某种原因,当我旋转时,对象会抖动,并且不会在适当的位置旋转 class rocket(object): def __init__(self, x, y): self.original_image = pygame.image.load('sprites/spaceship-off.png') self.image = self.original_image self.x = x self.y = y self.wi

我试图在pycharm中旋转一个图像,由于某种原因,当我旋转时,对象会抖动,并且不会在适当的位置旋转

class rocket(object):
def __init__(self, x, y):
    self.original_image = pygame.image.load('sprites/spaceship-off.png')
    self.image = self.original_image
    self.x = x
    self.y = y
    self.width = 50
    self.height = 50
    self.speed = 15
    self.angle = 0
    self.direction = [0, -1]
    self.position = [400, 400]

def draw(self, win):
    win.blit(self.image, (self.position))
    self.image = pygame.transform.rotate(self.original_image, self.angle)
-

if键[pygame.K_a]和player.x>player.speed:
player.angle+=10%360
如果键[pygame.K_d]和player.x<800-player.width-player.speed:
player.angle-=10%360

当您想围绕图像中心旋转图像时,您必须:

  • 获取轴(未旋转图像的中心点)
  • 旋转图像并获取旋转图像的(轴对齐边界)矩形
  • 将矩形置于轴的中心
  • blit
    新矩形左上角的图像
def draw(自我,赢):
image_rect=self.original_image.get_rect(左上角=self.position)
pivot=图像右中心
self.image=pygame.transform.rotate(self.original\u image,self.angle)
rotated_rect=self.image.get_rect(中心=轴)
win.blit(self.image,左上角旋转)
另见

if keys[pygame.K_a] and player.x > player.speed:
    player.angle += 10 % 360
if keys[pygame.K_d] and player.x < 800 - player.width - player.speed:
    player.angle -= 10 % 360