2016-04-22 24 views
1

我正在製作一個項目,該項目需要一組Scribbler 2s在播放wav文件的開始處跳舞,並在文件結尾停止播放。在WAV歌曲播放時不跳舞的機器人

(這是不完整的代碼,而是我測試如何做到這一點,所以我可以將其應用到更大的代碼。)

from Myro import * 
from winsound import* 
from time import * 

def playSong(): 
    s=PlaySound('C:\Python34\cantHoldUs.wav',SND_FILENAME) 
    sleep(30) 
    s.PlaySound(None,SND_FILENAME) 

while playSong()==True: 
    motors(-1,1) 

的歌曲播放和結束,但機器人不動。誰能告訴我如何?

回答

1

我建議重組你的代碼的東西while循環,因爲它是更清潔和更容易控制:

from time import * 

# Play the song 
s=PlaySound('C:\Python34\cantHoldUs.wav',SND_FILENAME) 

# Start the timer so we can identify when to stop 
starttime = time() 

# Use a while loop with a True statement until we decide to break it 
while True: 
    # Make that robot dance! 
    motors(-1,1) 

    # Check the current time 
    stop_time = ((time() - starttime)) 

    # Stop when 30 seconds is hit 
    if stop_time > 30: 
     s.PlaySound(None,SND_FILENAME) 
     break 

    sleep(1) 
+0

這不工作 - 歌曲播放,但仍然文人不動。我收到時間不存在的錯誤。我通過把'從時間進口時間'改變了它,但那也沒有用。 –

+0

@ A.C。什麼是錯誤? – Adib

+0

文件「C:\ Users \ Aesia \ Desktop \ Elements \ Calico-4.0.0-windows- all(1)\ Calico \ playmusicTest.py」,第8行,在 AttributeError:'builtin_function_or_method'對象沒有屬性'time' –