Pygame文本:让文本显示图像或动画,而不是颜色

Che*_*rot 5 python pygame python-3.x

看起来像这样但我希望图像和文本可编辑.

看起来像这样但我希望图像和文本可编辑

而不是像:

title = menuFont.render("COMPUTER INFORMATION!", 1, BLACK) 
screen.blit(title, Rect(50, 100, 400, 400))
Run Code Online (Sandbox Code Playgroud)

文本中的颜色可能是图像,还是动画?

编辑:对于那些好奇的...当我导入图像时,我不得不改变代码的结尾

screen.blit(texture, (50, 50))
screen.fill(BG_COLOR)
screen.blit(text_surface, (50, 170))
pg.display.update()
clock.tick(30)
Run Code Online (Sandbox Code Playgroud)

screen.fill出现在纹理之后...只是一个抬头:)

skr*_*krx 5

要对文本进行纹理处理,可以先将文本渲染为白色,然后将纹理blit到其上,然后pygame.BLEND_RGB_MULT作为special_flags参数传递以使用乘法混合模式.纹理仅出现在文本表面的不透明部分.

此外,请确保您的纹理大于文本表面,否则文本的某些部分将保持不受影响.

import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray32')
FONT = pg.font.Font(None, 50)

# I create a grid texture for demonstration purposes here.
# Just load your image with pygame.image.load instead.
texture = pg.Surface((200, 100))
texture.fill((200, 100, 0))
for x in range(0, 201, 5):
    pg.draw.line(texture, (0, 0, 0), (x, 0), (x, 200))
for y in range(0, 101, 5):
    pg.draw.line(texture, (0, 0, 0), (0, y), (200, y))

# Render the text and use pure white as the color.
text_surface = FONT.render('Hello world!', True, (255, 255, 255))
# Now blit the texture onto the text surface and pass BLEND_RGB_MULT as
# the special_flags argument, so that only the opaque parts are affected.
text_surface.blit(texture, (0, 0), special_flags=pg.BLEND_RGB_MULT)

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True

    screen.fill(BG_COLOR)
    screen.blit(texture, (50, 50))
    screen.blit(text_surface, (50, 170))
    pg.display.flip()
    clock.tick(30)

pg.quit()
Run Code Online (Sandbox Code Playgroud)

这是动画版.您必须加载动画的单独帧,并对每个帧执行与上面相同的操作.将生成的曲面放入列表中,然后在主循环中播放它们.

import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray32')
FONT = pg.font.Font(None, 50)

# I create a grid texture for demonstration purposes here.
# Just load your image with pygame.image.load instead.
texture = pg.Surface((200, 100))
texture.fill((200, 100, 0))
for x in range(0, 201, 5):
    pg.draw.line(texture, (0, 0, 0), (x, 0), (x, 200))
for y in range(0, 101, 5):
    pg.draw.line(texture, (0, 0, 0), (0, y), (200, y))

# Render the text and use pure white as the color.
text_surface = FONT.render('Hello world!', True, (255, 255, 255))

frames = []
for i in range(5):
    surf = text_surface.copy()  # We need a fresh copy of the text.
    # Now blit the texture onto the text surface and pass BLEND_RGB_MULT as
    # the special_flags argument, so that only the opaque parts are affected.
    # The y-position is shifted by -1 each iteration.
    surf.blit(texture, (0, -1*i), special_flags=pg.BLEND_RGB_MULT)
    frames.append(surf)

frame_counter = 0
frame_timer = 0
dt = 0

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True

    frame_timer += dt  # Add the passed time.
    if frame_timer >= 150:  # If 150 milliseconds have passed...
        frame_timer = 0  # Reset the timer.
        frame_counter += 1  # Increment the counter.
        frame_counter %= len(frames)  # Keep it in the correct range.

    screen.fill(BG_COLOR)
    # Now use `frame_counter` as the list index and blit the surface.
    screen.blit(frames[frame_counter], (50, 170))
    pg.display.flip()
    dt = clock.tick(60)  # `dt` is the passed time in milliseconds.

pg.quit()
Run Code Online (Sandbox Code Playgroud)