Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 改变游戏表面的颜色_Python_Pygame - Fatal编程技术网

Python 改变游戏表面的颜色

Python 改变游戏表面的颜色,python,pygame,Python,Pygame,我制作了一个矩形类: class Rectangle(pygame.sprite.Sprite): def __init__(self, len, x, y, color): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface((len, len), flags=pygame.SRCALPHA) # self.image.set_colorkey(Constants.WHITE) self

我制作了一个矩形类:

class Rectangle(pygame.sprite.Sprite):
def __init__(self, len, x, y, color):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.Surface((len, len), flags=pygame.SRCALPHA)
    # self.image.set_colorkey(Constants.WHITE)
    self.image.fill(color)
    self.rect = self.image.get_rect()
    self.rect.center = (x + len / 2, y + len / 2)
    self.is_blocked = False
    self.x_pos = x
    self.y_pos = y
    self.x = math.floor((x - Constants.PADTOPBOTTOM) / Constants.CELL_SIZE)
    self.y = math.floor((y - Constants.PADLEFTRIGHT) / Constants.CELL_SIZE)

def update(self):
    if self.is_blocked:
       print("is update working?") # it is working. 
       self.image.fill(Constants.GREEN)
调用update()时,我想将矩形的颜色更改为绿色,但它不起作用。
我是pygame新手,我不知道该怎么办。

你的问题是,你一直在每帧创建新的精灵,因此当你将一个
矩形的颜色更改为绿色时,它将被一个白色覆盖


您可以通过调用
drawGrid
函数中的
fill
函数(创建新的
Rectangle
实例)来完成此操作。

您确定在之前将
is blocked
设置为
True
吗?@sloth是的,我在console@Rabbid76颜色仍然是白色,我做不出这个长方形green@Rabbid76在循环之前,只有一次,在循环中,我调用所有的_spirtes.update()。“所有精灵”是一个包含我所有矩形的列表。你确定a)
常量。绿色实际上是绿色的,b)你没有在应该改变颜色的精灵上绘制另一个精灵?这解决了我的问题。非常感谢你!我知道我是怎么把那条线放在那里的。