Skip to main content
 首页 » 编程设计

python之在类中调用的 pygame.draw.rect() 函数不显示矩形

2024年11月24日9lyj

下面的代码没有显示在存储在列表中的类中调用的矩形,即使循环“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) 
 

请您参考如下方法:

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

例如

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()