我创建的程序(使用python-版本2.7.15)使用Finch机器人播放Simon Says。该程序中的播放器必须执行计算机告诉它的操作(无论是将机器人上下翻转,倾斜,摇动还是敲击它)。如果播放器在一定的秒数内没有执行此操作,或者执行了错误的操作,或者在计算机未显示“ Simon Says”时执行了该操作,则程序结束。我使用了一个函数来检查程序的错误。最后,雀科会发出嗡嗡声,以表示游戏已结束,并且会告知用户正确答案的数量。
我遇到的问题是在执行错误检查时。当计算机未说出“ Simon Says”字样时,该程序可以通过告诉用户是否进行了正确的操作来正常运行。但是,当它说Simon说时,计算机在检查CheckErrors()函数中的错误时什么也没说,这就是我遇到的问题。我该怎么做才能使其在说西蒙说时起作用?
import finch
import time
import win32com.client as wincl
speak = wincl.Dispatch("SAPI.SpVoice")
import random
robot = finch.Finch()
left_light, right_light = robot.light() #light sensors
left_tilt, no_tilt, right_tilt, tap_finch, shake_finch = robot.acceleration() #orientation sensors
#Commands that will be said
Tiltleft = "Tilt to the left"
Shake = "Shake"
Tiltright = "Tilt to the right"
Tiltdown = "Tilt downwards"
Tiltup = "Tilt up"
Covereyes = "Cover your eyes"
Flipover = "Flip over"
Tap = "Tap"
SimonSays = "Simon Says "
GameOver = False
CorrectAnswers = 0
AddOne = 1
SimonSaysCommands = [SimonSays + Tiltleft, SimonSays + Tiltright, SimonSays + Tiltdown, SimonSays + Tiltup, SimonSays + Covereyes, SimonSays + Tap, SimonSays + Shake, SimonSays + Flipover]
RegularCommands = [Tiltleft, Tiltright, Tiltdown, Tiltup, Covereyes, Tap, Shake, Flipover]
while not GameOver == True:
robot.wheels(1.0, -1.0)
time.sleep(1.0) #spins robot for two seconds
robot.wheels(0.0, 0.0)
Choice = random.choice(RegularCommands + SimonSaysCommands)
speak.Speak(Choice) #say command
time.sleep(2.0) #wait two seconds
accelx, accely, accelz, tap, shake = robot.acceleration() #orientation sensors
left_light, right_light = robot.light() #light sensors
#function will check if player did correct action
def CheckErrors(choices):
CorrectAnswers = 0
AddOne = 1
left_light, right_light = robot.light() #light sensors
if(choices == Tiltleft):
if(accely > 0 and accelz < 1 and accelx < 1):
speak.speak("Correct - tilted to left")
CorrectAnswers += AddOne
else:
speak.speak("Incorrect")
GameOver == True
elif(choices == Tap):
if(tap == True):
speak.speak("What's that tapping?")
CorrectAnswers += AddOne
else:
speak.speak("Incorrect")
GameOver == True
elif(choices == Shake):
if(shake == True):
speak.speak("I just felt an earthquake!")
CorrectAnswers += AddOne
else:
speak.speak("Inorrect")
GameOver == True
elif(choices == Tiltright):
if(accely < 1 and accelz < 1 and accelx < 1):
speak.speak("Tilting to the right")
CorrectAnswers += AddOne
else:
speak.speak("Incorrect")
GameOver == True
elif(choices == Tiltdown):
if(accelx < 1 and accelx > 0):
speak.speak("I'm pointed down")
CorrectAnswers += AddOne
else:
speak.speak("Incorrect")
GameOver == True
elif(choices == Flipover):
if(accelz < 0 and accelx > 0 and accely > 0):
speak.speak("I'm flipped over")
CorrectAnswers += AddOne
else:
speak.speak("Incorrect")
GameOver == True
elif(choices == Tiltup):
if(accelx < 1 and accely < 1 and accelz > 0):
speak.speak("Taking off")
CorrectAnswers += AddOne
else:
speak.speak("Incorrect")
GameOver == True
elif(choices == Covereyes):
if (left_light < 0.25 and right_light < 0.25):
speak.speak("I can't see!")
CorrectAnswers += AddOne
else:
speak.speak("Incorrect")
GameOver == True
time.sleep(1.5)
#call function to check for errors
CheckErrors(Choice)
def FinalResults(score):
#program is over
robot.buzzer(0.5, 880)
speak.speak("Game Over")
speak.Speak("Your Score is: ", score)
robot.close()