Pygame:用颜色填充文本的透明区域

Jul*_*rdt 7 pygame python-3.x pygame-surface

我有几种字体,我想使用它们基本上是字母的轮廓,但内部是透明的。我将如何仅用颜色填充这些字体的内部区域?我怀疑它会使用特殊的 blitting RGBA_BLEND 模式,但我不熟悉它们的功能。

这是我正在使用的字体示例:https : //www.dafont.com/fipps.font?back=bitmap

现在,我只是将字体渲染到表面上,为此我编写了一个辅助函数。理想情况下,我能够将其集成到我的功能中。

def renderText(surface, text, font, color, position):
    x, y = position[0], position[1]
    width, height = font.size(text)
    position = x-width//2, y-height//2
    render = font.render(text, 1, color)
    surface.blit(render, position)
Run Code Online (Sandbox Code Playgroud)

非常感谢您能给我的任何帮助!

Ale*_*XYX 3

一种选择是定义文本大小的表面,用所需的颜色填充该表面,然后在其上位块传输文本。例如你可以这样做:

text = font.render('Hello World!', True, (255, 255, 255)
temp_surface = pygame.Surface(text.get_size())
temp_surface.fill((192, 192, 192))
temp_surface.blit(text, (0, 0))
screen.blit(temp_surface, (0, 0))
Run Code Online (Sandbox Code Playgroud)

这将创建一个临时表面,该表面应填充文本表面的透明像素。还有另一种使用选择set_at(),但对于您正在做的事情来说,它的处理能力太昂贵,并且最好用于预处理表面。

我确信使用 BLEND_RGBA_MULT 的更好选择将来自更有经验的用户。我也不太擅长混合模式。