python pygame 设置颜色透明度

python pygame set color transparency

我想在屏幕上创建一个透明的按钮和文本,我正在寻找方法,第四个RGB参数和set_alpha可以透明颜色

所以我使用 self.button_color=(0,100,100,128) 设置按钮并使用 self.text.set_alpha(128) 更改文本颜色

但是当我 运行 脚本时没有任何变化

代码如下:

#!/usr/bin/python
import sys,os
import pygame


class Setting():
    def __init__(self,width,height):
        self.w=width
        self.h=height
        self.flag=pygame.RESIZABLE
        self.screen=pygame.display.set_mode((self.w,self.h),self.flag)
        self.screen_rect=self.screen.get_rect()
        pygame.display.set_caption("Test")

class Button():
    def __init__(self,setting,text):            
        self.width,self.height = 400,100

        self.button_color=(0,100,100,128)

        self.text_color=(255,0,0)
        self.text = pygame.font.Font(None,100).render(text,True,self.text_color)
        self.text.set_alpha(128)
        self.rect = pygame.Rect(0,0,self.width,self.height)
        self.rect.center = setting.screen_rect.center
        self.text_rect = self.text.get_rect()
        self.text_rect.center = self.rect.center
    def draw_button(self,setting):
        setting.screen.fill(self.button_color,self.rect)
        setting.screen.blit(self.text,self.text_rect)


def game():
    pygame.init()
    setting=Setting(1200,800)
    button=Button(setting,'PLAY')
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        setting.screen.fill((0,0,0))
        button.draw_button(setting)
        pygame.display.flip()
game()

阅读pygame.font.Font.render的文档:

[...] Depending on the type of background and antialiasing used, this returns different types of Surfaces. For performance reasons, it is good to know what type of image will be used. [...] If the background is transparent a colorkey will be set. Antialiased images are rendered to 24-bit RGB images. If the background is transparent a pixel alpha will be included.

也就是说,如果antialias参数是True,那么你必须设置透明background颜色来生成透明文本。例如:

self.button_color=(0,100,100,128) # transparent alpha=128
self.text_color=(255,0,0)
self.text = pygame.font.Font(None,100).render(text,True,self.text_color, self.button_color)

阅读pygame.Surface.fill的文档:

[...] The color argument can be either a RGB sequence, a RGBA sequence or a mapped color index. If using RGBA, the Alpha (A part of RGBA) is ignored unless the surface uses per pixel alpha (Surface has the SRCALPHA flag).

您必须创建一个具有 SCRALPHA 属性的 pygame.Surface 对象来绘制透明矩形:

rectsurf = pygame.Surface(self.rect.size,pygame.SRCALPHA)
rectsurf.fill(self.button_color)
setting.screen.blit(rectsurf,self.rect.topleft)

要实现您想要的效果,您必须使用特殊标志 BLEND_MAX blit 矩形上的文本。 draw_button 只需要 blit,屏幕上包含文本的按钮矩形。例如:

class Button():
    def __init__(self,setting,text):
        self.width,self.height = 400,100

        self.button_color=(0,100,100,128)
        self.text_color=(255,0,0,128)
        self.text = pygame.font.Font(None,100).render(text,True,self.text_color, self.button_color)

        self.rect = pygame.Rect(0,0,self.width,self.height)
        self.text_rect = self.text.get_rect()
        self.text_rect.center = self.rect.center

        self.btnsurf = pygame.Surface(self.rect.size,pygame.SRCALPHA)
        self.btnsurf.fill(self.button_color)
        self.btnsurf.blit(self.text, self.text_rect, special_flags=pygame.BLEND_MAX)

        self.rect.center = setting.screen_rect.center

    def draw_button(self,setting):
        setting.screen.blit(self.btnsurf,self.rect)