首页 > 解决方案 > Pygame,如何在透明层上显示文本?

问题描述

在获得奖金后,我试图在单独的透明层上显示文本。屏幕闪烁一毫秒,游戏继续。我在哪里做错了?

WIDTH = 500
HEIGHT = 600

screen = pygame.display.set_mode((WIDTH, HEIGHT))
surface = pygame.surface.Surface((WIDTH, HEIGHT))

def hit():
    screen.blit(surface, (0, 0))
    bonus = BONUSFONT.render("+3 points!", 1, (0, 0, 0))
    bonus_text = (bonus, (200, 150))
    bonus_end = pygame.time.get_ticks() + 3000
    if bonus_text and pygame.time.get_ticks() < bonus_end:
        surface.blit(*bonus_text)

我检查了几乎所有关于表面和层的问题,但没有任何帮助

标签: pythontextpygamesurface

解决方案


在全局命名空间中创建 2 个变量:

bonus_text = None
bonus_end = 0

hit当“命中”发生时,设置bonus_end并且必须调用一次:

def hit():
    global bonus_end, bonus_text  
    bonus = BONUSFONT.render("+3 points!", 1, (0, 0, 0))
    bonus_text = (bonus, (200, 150))
    bonus_end = pygame.time.get_ticks() + 3000

创建一个显示奖励文本的函数_

def show_bonus():
    if bonus_text and pygame.time.get_ticks() < bonus_end:
        surface.blit(*bonus_text)

在主应用程序循环中不断调用该函数:

while True:

    # [...]

    # show bonus text
    show_bonus()

    # update display
    # [...]

推荐阅读