python - 类中调用的 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)
解决方案
您必须向该类添加一个方法,该方法绘制矩形。
例如
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()
推荐阅读
- javascript - 未捕获的错误:未定义容器 - Google 饼图
- python - 我正在尝试生成均值和协方差以生成 Mahalonobis 距离,但这是运行时警告,所有值都是 NaN
- reactjs - 模块未定义 Nodemailer
- xml - 使用 CDATA 元素对 xml 中的 uuid 进行排序的 xslt 模板
- c# - 使用 Visual Studio 2019 调试未来日期
- python - 拆分文件后修改 Excel 中的标题格式(Pandas 输出)
- python - 在 Pandas 中,如何使用确切的日期查找行
- c# - 简单的 ARM 模板在 Azure Cli 中不起作用
- latex - Latex - 当你有多个章节时,论文的命名法
- android - 在 Android Studio 中显示 RGB 颜色值