2012-04-09 86 views

回答

2

還有的pygame.time.set_timer(eventid, milliseconds)功能,生成與每milliseconds毫秒,然後你就可以處理你喜歡的事件隊列ID eventid的事件。您可以通過致電pygame.time.set_timer(eventid, 0)來停止再次生成該事件。

SDL有一個SDL_AddTimer函數,它完全符合你的要求 - 你給它傳遞一個回調函數,在延遲後執行,但從文檔中我找不到pygame的等價物。

對於python解決方案,您可以使用threading.Timer類。

+0

什麼是SDL?我將如何實現它? – maxhud 2012-04-09 20:17:07

+1

@maxhud SDL是'pygame'構建的C庫。我只是說這種行爲存在於SDL中,但顯然不在'pygame'中。 – 2012-04-09 20:17:38

0

Python的解決方案的setTimeout相當於是:

import threading 

def set_timeout(func, sec):  
    t = None 
    def func_wrapper(): 
     func() 
     t.cancel() 
    t = threading.Timer(sec, func_wrapper) 
    t.start() 

    def hello(): 
     print "Hello, world!" 

    set_timeout(hello, 0.1) # This is 0.1s = 100ms