我正在开发一个项目,需要一组Scribbler 2s在播放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)
歌曲播放和结束,但机器人不动。谁能告诉我怎么样?
答案 0 :(得分: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)