Pygame 没有将 Rect 重新绘制为不同的颜色
Pygame isn't redrawing Rect as a different color
我正在尝试制作一个正方形网格,然后根据它们的标签更改正方形的颜色,但是当我更改颜色时它不会在 pygame window 上更新显示。
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()
您在 DrawGrid
中的 for 循环更新了方块的颜色,但您
不要在你的表面上画这些所以它自然不会更新
展示。你必须在你的
for 循环,因为做 self.i.color = (0,0,255)
是不够的。
除此之外,DrawGrid
是 class 的方法似乎很奇怪
Sq
。由于 DrawGrid
绘制了一组 Sq
它应该是 class
Sq
的方法。实际上,当您查看它的 body 时,它并没有使用
self
除了 for self.i in SqL
循环应该是
在 for sq in SqL
中更改,因为在这里使用 self
是无关紧要的。
这是一个好兆头,告诉您 DrawGrid
是一个 class 方法。
我正在尝试制作一个正方形网格,然后根据它们的标签更改正方形的颜色,但是当我更改颜色时它不会在 pygame window 上更新显示。
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()
您在 DrawGrid
中的 for 循环更新了方块的颜色,但您
不要在你的表面上画这些所以它自然不会更新
展示。你必须在你的
for 循环,因为做 self.i.color = (0,0,255)
是不够的。
除此之外,DrawGrid
是 class 的方法似乎很奇怪
Sq
。由于 DrawGrid
绘制了一组 Sq
它应该是 class
Sq
的方法。实际上,当您查看它的 body 时,它并没有使用
self
除了 for self.i in SqL
循环应该是
在 for sq in SqL
中更改,因为在这里使用 self
是无关紧要的。
这是一个好兆头,告诉您 DrawGrid
是一个 class 方法。