【发布时间】:2020-10-04 01:01:38
【问题描述】:
我想在运行时动态更改矩形的颜色。目前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 中创建的所有透明像素。
有没有办法将颜色遮盖或混合到矩形上,所以它不会替换任何透明度?
【问题讨论】:
标签: python pygame sprite blending