python

您所在的位置:网站首页 pygame旋转图片方向 python

python

2023-06-25 23:57| 来源: 网络整理| 查看: 265

在 pygame 中,我对程序中的所有矩形使用 pygame.draw.rect(screen, color, rectangle)。我希望能够将这些矩形旋转到任意角度。我看过以下代码来旋转 IMAGES,但我的问题是关于 RECTANGLES。

pygame.transform.rotate(image, angle)

但是我正在处理矩形,我没有可以旋转的图像或“表面”。当我尝试用

旋转矩形时 rect = pygame.draw.rect(screen, self.color, self.get_rectang()) rotatedRect = pygame.transform.rotate(rect, self.rotation) screen.blit(rotatedRect)

这给出了 TypeError: must be pygame.Surface, not pygame.Rect on the line with .rotate()

我的问题是,如何在 pygame 中旋转 a 并显示 RECTANGLE(x,y,w,h),而不是图像。

这是“潜在重复”的链接帖子不是重复的。一个答案解释了旋转矩形的后果,另一个答案使用旋转图像的代码。

最佳答案

在这里查看第二个答案:Rotating a point about another point (2D)

我认为矩形的定位只能是水平或垂直的。您需要定义角并旋转它们,然后在它们之间绘制和填充。

另一种方式是制作类

class myRect(pygame.Surface): def __init__(self, parent, xpos, ypos, width, height): super(myRect, self).__init__(width, height) self.xpos = xpos self.ypos = ypos self.parent = parent def update(self, parent): parent.blit(self, (self.xpos, self.ypos)) def rotate(self, angle): #(your rotation code goes here)

并改用它,因为这样您就可以使用变换函数旋转它。

关于python - 在pygame中旋转一个矩形(不是图像),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36510795/



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3