pygame rotation around center point

大憨熊 提交于 2019-12-11 16:38:23

问题


I've read several other posts about rotating an image around the center point, but I've yet to figure it out. I even copy pasted one of the solutions posted on another SO question and it didn't work.

This is my code

  def rotate(self, angle):
    self.old_center = self.surface.get_rect().center
    self.surface = pygame.transform.rotate(self.surface, angle)
    self.surface.get_rect(center = self.old_center)

it's inside a class which contains the surface.

When I call this method the image rotates but also translates, and gets distorted.


回答1:


You are not assigning the new rect, you should do something like:

self.rect = self.surface.get_rect(center = self.old_center)

And you should always keep the original surface and rotate from the original, that way distortion doesn't accumulate when rotating multiple times.

Update

If you don't want to keep track of the rect object, you can do it every time you blit. If you keep the center coordinates.

Example:

rect = object.surface.get_rect()
rect.center = object.center
display.blit(object.surface, rect.topleft)


来源:https://stackoverflow.com/questions/18859278/pygame-rotation-around-center-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!