首页 > 解决方案 > 类中调用的 pygame.draw.rect() 函数不显示矩形

问题描述

即使循环“draw-refresh”的顺序是正确的,下面的代码也不会显示存储在列表中的类中调用的矩形。

while True:

     root.fill((0,200,255))

     for walls in range(len(WallList)):
          WallList[walls]
          print(walls, WallList[walls])

     for event in pygame.event.get():
          if event.type == pygame.QUIT:
                pygame.quit()
                quit()

     pygame.display.update()

我希望在填充根之前绘制矩形,但根总是蓝色(我给的颜色)。

编辑:有课:

class Wall():
     def __init__(self, x, y, thotType):
          global TypeList,camX,camY
          self.x=x
          self.y=y
          self.type=thotType
          if self.type== "Wall": pygame.draw.rect(root,(0,255,255),(x+camX,y+camY,mapmultiplier,mapmultiplier),1)
          if self.type== "Blank": pygame.draw.rect(root,(32,32,32),(x+camX,y+camY,mapmultiplier,mapmultiplier))
          TypeList.append(self.type)

标签: pythonpygame

解决方案


您必须向该类添加一个方法,该方法绘制矩形。

例如

class Wall():
    def __init__(self, x, y, thotType):
        self.x=x
        self.y=y
        self.type=thotType
        TypeList.append(self.type)

    def draw(self):
        if self.type== "Wall":
            pygame.draw.rect(root,(0,255,255),(self.x+camX,self.y+camY,mapmultiplier,mapmultiplier),1)
        if self.type== "Blank":
            pygame.draw.rect(root,(32,32,32),(self.x+camX,self.y+camY,mapmultiplier,mapmultiplier))

然后您可以调用该draw方法:

for walls in range(len(WallList)):
    WallList[walls].draw()

推荐阅读