2016-09-28 119 views
0

我知道這已經在技術上「問」在這個論壇上,但這個問題是基於一個不同的概念,我找不到。Python:睡眠方法

雖然使用time.sleep(無論)它明顯睡覺,我明白了,但使用Pygame它會鎖定程序。除了不鎖定pygame的輸入之外,是否有任何真正的方法在代碼中使用睡眠或暫停?我試過了;

time.sleep 
pygame.wait and 
pygame.delay 

這些都做完全相同的事情。我正在研究一個計算機科學課的遊戲,這個課包括一張13張我稍微有點不同的照片的小動畫,但是當間隔0.12秒時,它使它看起來很好,可悲的是整個窗口凍結等待聲明使其跳過並顯得非常糟糕。

感謝誰能弄清楚這個謎團。

+0

也許這將有助於:http://stackoverflow.com/questions/2660919/python-animation-timing – jdigital

+0

該線程唯一的問題是,我需要的動畫是每幀,它需要等待0.12秒在這之間和這篇文章不完全覆蓋那麼多? – Verxi

+0

你可以將動畫分成它自己的'Thread'嗎?這將避免放慢程序的其他部分。不過,我對pyGame並不熟悉,所以我只是在黑暗中拍攝而已。 – haliphax

回答

0

我想你可能想嘗試使用顯示爲here的方法。

我的意思的例子是這樣的:他使用pygame.time.get_ticks()來檢查變量是否小於,如果是,他通過if語句

class Unit(): 
    def __init__(self): 
     self.last = pygame.time.get_ticks() 
     self.cooldown = 300  

    def fire(self): 
     # fire gun, only if cooldown has been 0.3 seconds since last 
     now = pygame.time.get_ticks() 
     if now - self.last >= self.cooldown: 
      self.last = now 
      spawn_bullet() 

通知併產生子彈。

查看此概念的簡單方法如下

curtime = pygame.time.get_ticks() + 10 # the 10 is what we add to make it sleep 

while True: # Create a loop that checks its value each step 
    if curtime < pygame.time.get_ticks(): # check if curtime is less than get_ticks() 
     print("Sleep is over now!") 
     break # exit the loop 

我希望幫助你以任何方式,這似乎是因爲它保持凍結,當你使用正常功能,這可能是一個可行的選擇。

一件事

也注意到,pygame.time.get_ticks()會給你當前的時間以毫秒爲單位的完整文檔這一去here