如何单独更改按钮 pygame 上文本的不透明度?

2023-12-03

我下面有以下代码,它是从另一篇文章中获取的按钮的类。我想知道是否可以更改按钮背景的不透明度而不更改其上文本的不透明度。我怎样才能实现这个目标?

Code:

import pygame

pygame.init()
font = pygame.font.SysFont('Microsoft New Tai Lue', 23)


class Button(pygame.sprite.Sprite):
    # 1) no need to have 4 parameters for position and size, use pygame.Rect instead
    # 2) let the Button itself handle which color it is
    # 3) give a callback function to the button so it can handle the click itself 
    def __init__(self, color, color_hover, rect, callback, text='', outline=None):
        super().__init__()
        self.text = text
        # a temporary Rect to store the size of the button
        tmp_rect = pygame.Rect(0, 0, *rect.size)


        self.org = self._create_image(color, outline, text, tmp_rect)
        self.hov = self._create_image(color_hover, outline, text, tmp_rect)


        self.image = self.org

        self.rect = rect
        self.callback = callback



    def _create_image(self, color, outline, text, rect):
        img = pygame.Surface(rect.size)
        #img.set_alpha(110)
        if outline:
            img.fill(outline)
            img.fill(color, rect.inflate(-4, -4))
        else:
            img.fill(color)

        # render the text once here instead of every frame
        if text != '':
            text_surf = font.render(text, 1, pygame.Color('white'))
            text_rect = text_surf.get_rect(center=rect.center)
            img.blit(text_surf, text_rect)
        return img

    def update(self, events):
        # here we handle all the logic of the Button
        pos = pygame.mouse.get_pos()
        hit = self.rect.collidepoint(pos)
        self.image = self.hov if hit else self.org
        for event in events:
            if event.type == pygame.MOUSEBUTTONDOWN and hit:
                self.callback(self)

任何帮助表示赞赏。


你需要创建一个pygame.Surface每个像素有一个 alpha 通道。这可以通过设置来完成pygame.SRCALPHA flag:

class Button(pygame.sprite.Sprite):
    # [...]

    def _create_image(self, color, outline, text, rect):
        
        img = pygame.Surface(rect.size, pygame.SRCALPHA) # <---
        
        if outline:
            img.fill(outline)
            img.fill(color, rect.inflate(-4, -4))
        else:
            img.fill(color)

使用半透明颜色作为按钮的背景:

color = (255, 0, 0, 127) # just for example red color with ~50% transparency

也可以看看透明文字


最小的例子:

repl.it/@Rabbid76/PyGame-TextTransparentBackground

import pygame

pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()

background = pygame.Surface(window.get_size())
ts, w, h, c1, c2 = 50, *window.get_size(), (128, 128, 128), (64, 64, 64)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
for rect, color in tiles:
    pygame.draw.rect(background, color, rect)

font = pygame.font.SysFont(None, 80)
text = font.render("Button", True, (255, 255, 255))

button = pygame.Surface((320, 120), pygame.SRCALPHA)
button.fill((255, 255, 255))
button.fill((196, 127, 127, 127), button.get_rect().inflate(-4, -4))
button.blit(text, text.get_rect(center = button.get_rect().center))

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False          

    window.blit(background, (0, 0))
    window.blit(button, button.get_rect(center = window.get_rect().center))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
exit()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何单独更改按钮 pygame 上文本的不透明度? 的相关文章

随机推荐