为什么在pygame中文本显示2秒 [英] Why text display for 2 seconds in pygame
问题描述
我显示的文本仅显示约 2 秒.我希望它会在我点击其他区域时显示
Text wchich I display is displaying for only around 2 sec. I want that it will display while I click to other area
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 应用程序循环必须:
If you want something to be drawn permanently, you need to draw it in the application loop. The typical PyGame application loop has to:
- 通过
pygame.event 处理事件.pump()
或 <代码>pygame.event.get(). - 根据输入事件和时间(分别为帧)更新游戏状态和对象位置
- 清除整个显示或绘制背景
- 绘制整个场景(
blit
所有对象) - 通过
pygame.display.update()
或 <代码>pygame.display.flip()
在初始化时创建文本表面:
Create the text surface at initialization:
winTextSurf = font.render('You Win', True, (0, 0, 0))
winTextRect = winTextSurf.get_rect(topleft = (600 // 2, 700 // 2)
添加一个变量,表示需要绘制文本:
Add a variable that indicates the text needs to be drawn:
drawWinText = Fasle
如果需要绘制文本,设置变量:
Set the variable if the text needs to be drawn:
elif msg[0:7] == 'YOU WIN' and Message_id == '200':
drawWinText = True
break
当您不再希望绘制文本时,您需要重置该值.
When you no longer want the text to be drawn, you need to reset the value.
drawWinText = False
根据drawWinText
的状态在主应用循环中绘制文本:
Draw the text in the main application loop depending on the status of drawWinText
:
while run:
# [...]
if drawWinText:
surface.blit(winTextSurf, winTextRect)
pygame.display.flip()
查看问题的答案Python - Pygame - 渲染半透明文本,了解如何绘制透明文本.
See the answers to question Python - Pygame - rendering translucent text for how to draw transparent text.
这篇关于为什么在pygame中文本显示2秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!