【发布时间】:2021-12-07 00:09:15
【问题描述】:
我正在尝试制作一个正方形网格,然后根据它们的标签更改正方形的颜色,但是当我更改颜色时,它不会在 pygame 窗口显示上更新。
class Sq():
#init Func
def __init__(self, surface, color, x, y, width=50, height=50):
self.tag = 'normal'
self.value = 1
self.color = color
self.x = x
self.y = y
self.width = width
self.height = height
self.surface = surface
self.Sq = pygame.draw.rect(win.screen, self.color, (self.x, self.y,
self.width,self.height))
self.SQC = (self.Sq,self.tag )
def DrawGrid(self):
# Grid
a1 = Sq(win.screen,(255,0,0),0,3,50,50)
a2 = Sq(win.screen,(255,0,0),51,3,50,50)
a3 = Sq(win.screen,(255,0,0),102,3,50,50)
a4 = Sq(win.screen,(255,0,0),153,3,50,50)
a5 = Sq(win.screen,(255,0,0),204,3,50,50)
#Square list
SqL = [a1, a2, a3 ,a4 ,a5 ,a6 ,a7 ,a8 ,a9 ,a10,a11,a12,a13,a14,a15,a16,b1 ,b2 ,b3 ,b4 ,b5 ,b6 ,b7 ,b8 ,b9 ,b10,b11,b12,b13,b14,b15,b16,etc...]
#This loop checks all the Square.tag and changes their color if they're tag is "normal"
for self.i in SqL:
if self.i.tag == 'normal':
self.i.color = (0,0,255)
print(self.i.tag, self.i.color)
pygame.display.update()
#print check
print(a1.color)
pygame.display.update()
【问题讨论】: