更改表面的颜色而不覆盖透明度

我想在运行时动态更改矩形的颜色。当前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.


有没有办法将颜色遮罩或混合到矩形上,这样它就不会替换任何透明度?


芜湖不芜
浏览 85回答 1
1回答

临摹微笑

您可以通过 2 个步骤来实现这一目标。self.original_image包含具有所需颜色的填充矩形:self.original_image.fill(self.colour)生成一个全白色的矩形和透明区域。使用 (&nbsp;self.image) 和混合模式混合矩形BLEND_MAX(参见 pygame.Surface.blit):whiteTransparent&nbsp;=&nbsp;pg.Surface(self.image.get_size(),&nbsp;pg.SRCALPHA) whiteTransparent.fill((255,&nbsp;255,&nbsp;255,&nbsp;0)) self.image.blit(whiteTransparent,&nbsp;(0,&nbsp;0),&nbsp;special_flags=pg.BLEND_MAX)现在矩形是完全白色的,但透明区域被保留。使用混合模式BLEND_MULT并混合self.original_image以self.image获得所需的结果:self.image.blit(self.original_image,&nbsp;(0,&nbsp;0),&nbsp;special_flags=pg.BLEND_MULT)最小的例子:&nbsp;repl.it/@Rabbid76/PyGame-ChangeColorOfSpriteAreahttps://i.stack.imgur.com/oldLt.gif import pygame as pgclass Rectangle(pg.sprite.Sprite):&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; pg.sprite.Sprite.__init__(self)&nbsp; &nbsp; &nbsp; &nbsp; self.original_image = pg.Surface((150, 150))&nbsp; &nbsp; &nbsp; &nbsp; self.image = self.original_image&nbsp; &nbsp; &nbsp; &nbsp; self.rect = self.image.get_rect(center = (150, 150))&nbsp; &nbsp; def set_colour(self, colour_value):&nbsp; &nbsp; &nbsp; &nbsp; self.colour = colour_value&nbsp; &nbsp; &nbsp; &nbsp; self.original_image.fill(self.colour)&nbsp; &nbsp; &nbsp; &nbsp; whiteTransparent = pg.Surface(self.image.get_size(), pg.SRCALPHA)&nbsp; &nbsp; &nbsp; &nbsp; whiteTransparent.fill((255, 255, 255, 0))&nbsp; &nbsp; &nbsp; &nbsp; self.image.blit(whiteTransparent, (0, 0), special_flags=pg.BLEND_MAX)&nbsp; &nbsp; &nbsp; &nbsp; self.image.blit(self.original_image, (0, 0), special_flags=pg.BLEND_MULT)&nbsp; &nbsp; def set_outline(self, thickness):&nbsp; &nbsp; &nbsp; &nbsp; self.thickness = thickness&nbsp; &nbsp; &nbsp; &nbsp; size = self.image.get_size()&nbsp; &nbsp; &nbsp; &nbsp; calc = thickness/100&nbsp; &nbsp; &nbsp; &nbsp; p_width, p_height = size[0], size[1]&nbsp; &nbsp; &nbsp; &nbsp; width, height = size[0]*calc, size[1]*calc&nbsp; &nbsp; &nbsp; &nbsp; self.image = self.image.convert_alpha()&nbsp; &nbsp; &nbsp; &nbsp; center_x, center_y = (p_width//2)-(width//2), (p_height//2)-(height//2)&nbsp; &nbsp; &nbsp; &nbsp; pg.draw.rect(self.image, (0, 0, 0, 0), (center_x, center_y, width, height))pg.init()window = pg.display.set_mode((300, 300))clock = pg.time.Clock()sprite = Rectangle()sprite.set_colour((255, 0, 0, 255))sprite.set_outline(50)group = pg.sprite.Group(sprite)colorVal = 0colorAdd = 5run = Truewhile run:&nbsp; &nbsp; clock.tick(60)&nbsp; &nbsp; for event in pg.event.get():&nbsp; &nbsp; &nbsp; &nbsp; if event.type == pg.QUIT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; run = False&nbsp; &nbsp; sprite.set_colour((min(colorVal, 255), max(0, min(511-colorVal, 255)), 0, 255))&nbsp; &nbsp; colorVal += colorAdd&nbsp; &nbsp; if colorVal <= 0 or colorVal >= 511:&nbsp; &nbsp; &nbsp; &nbsp; colorAdd *= -1&nbsp; &nbsp; window.fill(0)&nbsp; &nbsp; group.draw(window)&nbsp; &nbsp; pg.display.flip()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python