在Pygame中呈现抗恶化的透明文本



我想拥有文本,我可以在仍然具有抗偏α上更改alpha值以使其看起来不错。

label1是抗恶心,但不是透明的

label2是透明的,但不抗抗

我想要两者的文本。谢谢。

import pygame
pygame.init()
screen = pygame.display.set_mode((300, 300))
font = pygame.font.SysFont("Segoe UI", 50)
label1 = font.render("hello", 1, (255,255,255))
label1.set_alpha(100)
label2 = font.render("hello", 0, (255,255,255))
label2.set_alpha(100)
surface_box = pygame.Surface((100,150))
surface_box.fill((0,150,150))
done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    screen.fill((150,0,150))
    screen.blit(surface_box, (40, 0))
    screen.blit(label1, (0,0))
    screen.blit(label2, (0,50))
    pygame.display.update()
pygame.quit()

如果您可以修改示例具有这些功能,将不胜感激。

  1. 渲染文本表面。

  2. 通过传递pygame.SRCALPHA并用白色填充和所需的alpha值来创建一个带有像素alpha的透明表面。

  3. 将alpha表面闪烁到文本表面,并将pygame.BLEND_RGBA_MULT作为special_flags参数传递。这将使表面透明的可见部分。


import pygame as pg

pg.init()
clock = pg.time.Clock()
screen = pg.display.set_mode((640, 480))
font = pg.font.Font(None, 64)
blue = pg.Color('dodgerblue1')
sienna = pg.Color('sienna2')
# Render the text surface.
txt_surf = font.render('transparent text', True, blue)
# Create a transparent surface.
alpha_img = pg.Surface(txt_surf.get_size(), pg.SRCALPHA)
# Fill it with white and the desired alpha value.
alpha_img.fill((255, 255, 255, 140))
# Blit the alpha surface onto the text surface and pass BLEND_RGBA_MULT.
txt_surf.blit(alpha_img, (0, 0), special_flags=pg.BLEND_RGBA_MULT)
done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
    screen.fill((30, 30, 30))
    pg.draw.rect(screen, sienna, (105, 40, 130, 200))
    screen.blit(txt_surf, (30, 60))
    pg.display.flip()
    clock.tick(30)
pg.quit()

最新更新