python - 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)
我检查了几乎所有关于表面和层的问题,但没有任何帮助
解决方案
在全局命名空间中创建 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
# [...]
推荐阅读
- ios - 如何在 AR Kit 中将 UIImage 添加到场景中
- r - Base R 中的旋转箭头
- javascript - 分派动作后不显示道具
- android - 所有 Http 请求的时间间隔(使用改造和 Rx Java 2)
- python - 在 Python 3.6 中计算多边形和 shapefile 之间的重叠
- ios - UITableViewCell 委托不起作用
- reactjs - React Native Crash Undefined 不是一个对象(评估 'Sn[e]')
- vhdl - 使用 vhdl 的 FIFO
- javascript - 比较 div id 和字符串
- javascript - 是否可以在 SELECTED 文本的一侧创建一条垂直线?