pygame围绕中心点旋转 [英] pygame rotation around center point

查看:730
本文介绍了pygame围绕中心点旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还阅读了其他几篇有关围绕中心点旋转图像的文章,但我还没有弄清楚。我什至还复制粘贴在另一个SO问题上发布的解决方案之一,但没有用。

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.

这是我的代码

  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.

推荐答案

您没有分配新的rect,应该执行以下操作:

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.

更新

如果您不想为了跟踪 rect 对象,您可以在每次blit时都这样做。如果保持中心坐标。

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.

示例:

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

这篇关于pygame围绕中心点旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆