不使用事件队列的Pygame程序应该在每次迭代时调用Pygame.event.pump。这样可以防止结冰。将
我是Stack Overflow以及pygame和python的新手,所以如果我犯了一个非常简单的错误,请原谅我。我试图在pygame屏幕上一次显示一个字符的文本。我的功能运行良好,呈现我想要的内容,只是它随机冻结,在不一致的时间在标题区域显示"未响应"(例如,有时在呈现10个字母后会冻结,有时在28个字母后冻结,等等)。即使在我重新启动计算机之后,这种情况也会发生。我的问题是:这只是发生在我身上,还是我的代码有问题,如果是我的代码出了问题,请帮我修复。这是我的密码,提前感谢:
import pygame, time
from pygame.locals import *
width = 800
height = 800
pygame.init()
scrn = pygame.display.set_mode((width, height))
font = pygame.font.SysFont(None, 22)
def render_text(string, bg = None, text_color = (0, 0, 0), surf = scrn, width = width, height = height):
text = ''
for i in range(len(string)):
if bg == None:
surf.fill((255, 255, 255))
else:
surf.blit(bg, (0, 0))
text += string[i]
text_surface = font.render(text, True, text_color)
text_rect = text_surface.get_rect()
text_rect.center = (width/2, height/2)
surf.blit(text_surface, text_rect)
pygame.display.update()
time.sleep(0.05)
def intro():
while True: #just used as an example, it will freeze up usually sometime during the first or second iteration
render_text("It was a dark and stormy night.")
time.sleep(2)
intro()
intro
函数更改为这样应该可以工作:
def intro():
while True:
pygame.event.pump()
render_text("It was a dark and stormy night.")
time.sleep(2)