使用 pygame.transform.rotate() 时的奇怪转变 [英] Weird shifting when using pygame.transform.rotate()

查看:59
本文介绍了使用 pygame.transform.rotate() 时的奇怪转变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def rotate(self):
    #Save the original rect center
    self.saved_center=self.rect.center

    #Rotates a saved image every time to maintain quality
    self.image=pygame.transform.rotate(self.saved_image, self.angle)

    #Make new rect center the old one
    self.rect.center=self.saved_center

    self.angle+=10

当我旋转图像时,尽管我保存了旧的矩形中心并使旋转的矩形中心成为旧的,但它还是会发生奇怪的变化.我希望它在正方形的中心向右旋转.

When I rotate the image, there is a weird shifting of it despite the fact that I'm saving the old rect center and making the rotated rect center the old one. I want it to rotate right at the center of the square.

这是它的样子:http://i.imgur.com/g6Os9.gif

推荐答案

您只是在计算新的 rect 错误.试试这个:

You are just calculating the new rect wrong. Try this:

def rotate(self):
    self.image=pygame.transform.rotate(self.saved_image, self.angle)
    self.rect = self.image.get_rect(center=self.rect.center)
    self.angle+=10

它告诉新矩形以原始中心为中心(中心永远不会改变.只是不断传递).

It tells the new rect to center itself around the original center (the center never changes here. Just keeps getting passed along).

问题是 self.rect 从未正确更新.您只是在更改中心值.整个 rect 会随着图像的旋转而变化,因为它的大小会变大和变小.所以你需要做的就是每次都完全设置新的矩形.

The issue was that the self.rect was never being properly updated. You were only changing the center value. The entire rect changes as the image rotates because it grows and shrinks in size. So what you needed to do was completely set the new rect each time.

self.image.get_rect(center=self.rect.center)

这会计算一个全新的矩形,同时将其围绕给定的中心.在计算位置之前,中心设置在矩形上.因此,您会得到一个以您的点为中心的矩形.

This calculates a brand new rect, while basing it around the given center. The center is set on the rect before it calculate the positions. Thus, you get a rect that is properly centered around your point.

这篇关于使用 pygame.transform.rotate() 时的奇怪转变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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