亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

更改表面的顏色而不覆蓋透明度

更改表面的顏色而不覆蓋透明度

蕪湖不蕪 2023-10-06 18:42:28
我想在運行時動態更改矩形的顏色。當前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.有沒有辦法將顏色遮罩或混合到矩形上,這樣它就不會替換任何透明度?
查看完整描述

1 回答

?
臨摹微笑

TA貢獻1982條經驗 獲得超2個贊

您可以通過 2 個步驟來實現這一目標。self.original_image包含具有所需顏色的填充矩形:

self.original_image.fill(self.colour)

生成一個全白色的矩形和透明區域。使用 ( self.image) 和混合模式混合矩形BLEND_MAX(參見 pygame.Surface.blit):

whiteTransparent = pg.Surface(self.image.get_size(), pg.SRCALPHA)
whiteTransparent.fill((255, 255, 255, 0))
self.image.blit(whiteTransparent, (0, 0), special_flags=pg.BLEND_MAX)

現在矩形是完全白色的,但透明區域被保留。使用混合模式BLEND_MULT并混合self.original_imageself.image獲得所需的結果:

self.image.blit(self.original_image, (0, 0), special_flags=pg.BLEND_MULT)

最小的例子:https://img1.sycdn.imooc.com//651fe4e600019c7900400033.jpg repl.it/@Rabbid76/PyGame-ChangeColorOfSpriteArea

https://i.stack.imgur.com/oldLt.gif

import pygame as pg


class Rectangle(pg.sprite.Sprite):

    def __init__(self):

        pg.sprite.Sprite.__init__(self)

        self.original_image = pg.Surface((150, 150))

        self.image = self.original_image

        self.rect = self.image.get_rect(center = (150, 150))


    def set_colour(self, colour_value):

        self.colour = colour_value

        self.original_image.fill(self.colour)


        whiteTransparent = pg.Surface(self.image.get_size(), pg.SRCALPHA)

        whiteTransparent.fill((255, 255, 255, 0))

        self.image.blit(whiteTransparent, (0, 0), special_flags=pg.BLEND_MAX)


        self.image.blit(self.original_image, (0, 0), special_flags=pg.BLEND_MULT)


    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))


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 = 0

colorAdd = 5

run = True

while run:

    clock.tick(60)

    for event in pg.event.get():

        if event.type == pg.QUIT:

            run = False


    sprite.set_colour((min(colorVal, 255), max(0, min(511-colorVal, 255)), 0, 255))

    colorVal += colorAdd

    if colorVal <= 0 or colorVal >= 511:

        colorAdd *= -1


    window.fill(0)

    group.draw(window)

    pg.display.flip()


查看完整回答
反對 回復 2023-10-06
  • 1 回答
  • 0 關注
  • 114 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號