【问题标题】:How to separately change the opacity of a text on a button pygame?如何单独更改按钮pygame上文本的不透明度?
【发布时间】:2021-06-25 19:18:12
【问题描述】:

下面的代码是从另一个帖子中获取的按钮的类。我想知道是否可以更改按钮背景的不透明度而不更改其上文本的不透明度。我怎样才能做到这一点?

代码:

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)

感谢任何帮助。

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    您需要创建一个pygame.Surface,每个像素都有一个 Alpha 通道。这可以通过设置pygame.SRCALPHA 标志来完成:

    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
    

    另见Transparent text


    小例子:

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

    【讨论】:

      猜你喜欢
      • 2013-08-29
      • 1970-01-01
      • 2016-10-19
      • 2021-08-01
      • 2013-03-04
      • 2022-06-26
      • 2019-09-18
      • 2014-01-05
      • 1970-01-01
      相关资源
      最近更新 更多