我想在运行时动态更改矩形的颜色。当前set_colour正在用单一颜色值填充表面的所有像素。这是可行的,但是当调用类似的方法时会出现问题set_outline,该方法会修改表面的透明度。
class Rectangle(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.original_image = pg.Surface((10, 10))
self.image = self.original_image
self.rect = self.image.get_rect()
def set_colour(self, colour_value):
self.colour = colour_value
self.image.fill(self.colour)
self.original_image.fill(self.colour)
def set_outline(self, thickness):
self.thickness = thickness
size = self.image.get_size()
calc = thickness/100
p_width, p_height = size[0], size[1]
width, height = size[0]*calc, size[1]*calc
self.image = self.image.convert_alpha()
center_x, center_y = (p_width//2)-(width//2), (p_height//2)-(height//2)
pg.draw.rect(self.image, (0, 0, 0, 0), (center_x, center_y, width, height))
现在,如果我尝试在运行时更改该矩形的颜色,它将覆盖在set_outline.
有没有办法将颜色遮罩或混合到矩形上,这样它就不会替换任何透明度?
临摹微笑
相关分类