我是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()
答案 0 :(得分:0)
不使用事件队列的Pygame程序应该每次迭代都调用pygame.event.pump。这将防止冻结发生。将intro
函数更改为这样可以起作用:
def intro():
while True:
pygame.event.pump()
render_text("It was a dark and stormy night.")
time.sleep(2)