【问题标题】:Pygame isn't redrawing Rect as a different colorPygame 不会将 Rect 重绘为不同的颜色
【发布时间】: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()

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    DrawGrid 中的 for 循环会更新方块的颜色,但您 不要在你的表面上画这些,所以它自然不会更新 展示。你必须打电话给pygame.draw.rect for 循环,因为做self.i.color = (0,0,255) 是不够的。

    除此之外,DrawGrid 是类的方法似乎很奇怪 Sq。由于DrawGrid 绘制了一组Sq 它应该被做成一个类 Sq 的方法。实际上,当你看它的身体时,它并没有使用 self 除了 for self.i in SqL 循环应该是 在for sq in SqL 中更改,因为在这里使用self 是无关紧要的。 这是一个好兆头,告诉您DrawGrid 是一个类方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      • 1970-01-01
      相关资源
      最近更新 更多