2015-04-06 41 views
0

我是Stack Overflow的新手,一般来说都是pygame和python,所以请原谅我,如果我犯了一个非常简单的错误。我试图在pygame屏幕中一次显示文本一个字符。我的功能正常工作,呈现我想要的东西,除了它随机冻结,在标题区域显示“Not Responding”,在不一致的时间(例如,有时在渲染10个字母后有时会冻结,有时在28之后等)。即使在我重新启动计算机后,也会发生这种情况我的问题是:这是发生在我身上,还是我的代码有问题,如果它的代码有问题,请帮我修复它。这里是我的代码,并预先感谢您:Pygame屏幕冻结在一次呈现文本一个字符的函数中

不使用该事件队列应该叫 pygame.event.pump每一次迭代,而不是
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

Pygame的程式。这将防止冻结发生。改变intro函数看起来应该是这样的:

def intro(): 
    while True: 
     pygame.event.pump() 
     render_text("It was a dark and stormy night.") 
     time.sleep(2) 
+0

工作,谢谢。 – user4594444 2015-04-09 22:13:06