gpt4 book ai didi

python - 在pygame中旋转一个矩形(不是图像)

转载 作者:太空狗 更新时间:2023-10-30 02:42:25 26 4
gpt4 key购买 nike

在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com