问题描述
运行时,我将一些文本 blit'ing 到我的表面 (WIN) 上,我希望它显示在游戏中,所以我调用 pygame.display.update 来更新显示,这工作得很好,直到循环迭代大约 75 次,此后显示停止更新。代码如下
def dialogue(x,y,text):
global clock
global run
typing=True
tempstring=""
counter=0
switcher=0
for z in range(0,(len(text))):
clock.tick(15)
tempstring=text[0:z+1]
counter+=1
print(counter)
print("temp is "+tempstring)
writing=TITLE_FONT.render(tempstring,1,(255,255,255),(0,0))
if x==-1 and y>-1:
pygame.display.update(WIN.blit(writing,(640-(writing.get_width()//2),y-(writing.get_height()//2))))
elif x>-1 and y==-1:
pygame.display.update(WIN.blit(writing,(x-(writing.get_width()//2),360-(writing.get_height()//2))))
elif x==-1 and y==-1:
pygame.display.update(WIN.blit(writing,360-(writing.get_height()//2))))
else:
pygame.display.update(WIN.blit(writing,y-(writing.get_height()//2))))
print(typing)
while True:
event = pygame.event.wait()
if event.type == pygame.QUIT:
run=False
pygame.quit()
break
if event.type == pygame.KEYDOWN:
break
我也尝试过在 if 语句行的末尾使用 pygame.display.update() 但这也失败了。
解决方法
在某些系统上,如果您没有从系统获取 PyGame
,pygame.event
将无法正常工作 - 系统可能认为该程序挂起,甚至可能将其关闭。
您可能需要在循环中使用 pygame.even.get()
(或类似函数)。
在文档 pygame.event 中你可以找到(但它隐藏在长描述中)
为了防止丢失事件,尤其是发出退出命令信号的输入事件,您的程序必须每帧处理事件(使用 pygame.event.get()、pygame.event.pump()、pygame.event.wait()、 pygame.event.peek() 或 pygame.event.clear()) 并处理它们。
不处理事件可能会导致您的系统认为您的程序已锁定。
为了使 pygame 与系统保持同步,您需要在内部调用 pygame.event.pump() 处理 pygame 事件处理程序以保持所有内容都是最新的。