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

标签 python rotation pygame rectangles

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

相关文章:

python - 在 Pygame 中启用全屏切换的正确语法是什么?

python - Siamese 网络的 Keras 模型不学习并且总是预测相同的输出

python - 如何对 Pandas 数据透视表进行排序,但将总数保留在表的末尾

java - 如何获得旋转图像上的点?

c++ - OpenGL : glRotatef not working?

python - 我正在尝试制作一个基于 "top down"图 block 的游戏。我不知道如何最初制作瓦片 map 来放置图像

python - 在 RHEL 7.2 上安装 Python 2.7.8 和 2.7.5 时出现问题

python - 如何在 Tkinter 中实现不确定的进度条?

c++ - 确定正确的原点以围绕 QPolygonF 旋转

python - 有没有办法更改 Pygame 中导入的 .obj 文件的位置和大小?