在pygame中使用类来创建矩形?

2024-09-30 23:33:04 发布

您现在位置:Python中文网/ 问答频道 /正文

问题实际上是在pygame中创建矩形。显然这只是pygame.draw.rect(),但我的问题是当我试图在类中绘制一个矩形,并使用def __init__()

我尝试使用__init__语句中的属性来绘制矩形,但发现会出现错误。这似乎是最成功的方法,但仍然没有显示框。你知道吗

class rectangles:

    def __init__(self, command, display, colour, x, y, width, height):
        self.command = command
        self.display = display
        self.colour = colour
        self.x = int(x)
        self.y = int(y)
        self.width = int(width)
        self.height = int(height)

    def rectdraw(self):
        return (self.command, self.display, self.colour, self.x, self.y, self.width, self.height)

block1 = rectangles(pygame.draw.rect, window, black, 100, 100, 200, 200)
block2 = rectangles(pygame.draw.rect, window, black, 20, 20, 400, 600)
block3 = rectangles(pygame.draw.rect, window, black, 800, 500, 40, 40)    


(block1.rectdraw())
(block2.rectdraw())
(block3.rectdraw())

(我也为pygame创建了一个窗口,只是没有包括在这里)

虽然程序没有出现任何错误,但它只会出现一个空白屏幕,而不会显示我想要的框。这尤其令人费解,这让我觉得我的课有问题。你知道吗


Tags: rectselfinitdefdisplaywidthpygamecommand
1条回答
网友
1楼 · 发布于 2024-09-30 23:33:04

你的代码中的问题是你在哪里写的

return (self.command, self.display, self.colour, self.x, self.y, self.width, self.height)

你应该写信的

return self.command(self.display, self.colour, self.x, self.y, self.width, self.height)

相反

相关问题 更多 >