1

我正在做一个项目,该项目需要一组 Scribbler 2 在播放 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)   

歌曲播放结束,但机器人没有移动。谁能告诉我怎么做?

4

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)
于 2016-04-22T22:06:26.957 回答