在 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
与 .rotate()
我的问题是,如何在 pygame 中旋转 a 并显示 RECTANGLE (x,y,w,h)
而不是图像。
这是“潜在重复”的链接帖子不是重复的。一个答案解释了旋转矩形的后果,另一个答案使用旋转图像的代码。
原文由 Keatinge 发布,翻译遵循 CC BY-SA 4.0 许可协议
在这里查看第二个答案: 围绕另一个点旋转一个点(2D)
我认为矩形的定位只能是水平或垂直的。您需要定义角并旋转它们,然后在它们之间绘制和填充。
另一种方法是上课
并改用它,因为这样您就可以使用变换功能旋转它。