我显示的文本仅显示约 2 秒。我希望当我点击其他区域时它会显示
elif msg[0:7] == 'YOU WIN' and Message_id == '200':
print('You Win')
textSurface = font.render('You Win', True, (0, 0, 0))
largeText = pygame.font.Font('freesansbold.ttf', 115)
TextSurf = pygame.font.render('You Win', True, (0, 0, 0))
TextRect = textSurface.get_rect()
TextRect.center = ((600 / 2), (700 // 2))
surface.blit(TextSurf, TextRect)
grid.draw(surface)
pygame.display.flip()
break
如果您希望永久绘制某些内容,则需要在应用程序循环中绘制它。典型的 PyGame 应用程序循环必须:
- 通过以下任一方式处理事件pygame.event.pump() https://www.pygame.org/docs/ref/event.html#pygame.event.pump or pygame.event.get() https://www.pygame.org/docs/ref/event.html#pygame.event.get.
- 根据输入事件和时间(分别为帧)更新游戏状态和对象位置
- 清除整个显示或绘制背景
- 绘制整个场景(
blit
所有对象)
- 通过以下任一方式更新显示pygame.display.update() https://www.pygame.org/docs/ref/display.html#pygame.display.update or pygame.display.flip() https://www.pygame.org/docs/ref/display.html#pygame.display.flip
在初始化时创建文本表面:
winTextSurf = font.render('You Win', True, (0, 0, 0))
winTextRect = winTextSurf.get_rect(topleft = (600 // 2, 700 // 2)
添加一个变量来指示需要绘制的文本:
drawWinText = Fasle
如果需要绘制文本,则设置变量:
elif msg[0:7] == 'YOU WIN' and Message_id == '200':
drawWinText = True
break
当您不再希望绘制文本时,需要重置该值。
drawWinText = False
根据状态在主应用程序循环中绘制文本drawWinText
:
while run:
# [...]
if drawWinText:
surface.blit(winTextSurf, winTextRect)
pygame.display.flip()
查看问题的答案Python - Pygame - 渲染半透明文本 https://stackoverflow.com/questions/20620109/how-to-render-transparent-text-with-alpha-channel-in-pygame/64552616#64552616了解如何绘制透明文本。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)