你需要创建一个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()