2015-04-06 16 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