Python迷宫游戏麻烦

时间:2016-05-12 18:22:07

标签: python maze

我试图制作一个游戏,让你经历一个迷宫并试图摆脱声音,但每当玩家说出其中一个问题的错误答案时,#34; Game Over&#34 ;但是接着继续下去,我已经尝试了很多东西并进行了研究,但我似乎无法弄清楚,我只是一个初学者

`

导入时间 import os

    print ("Your adventure starts as a young boy, running away from home becuase you're a rebel")
    time.sleep(2)
    print ("You find the famous labyrinth, do you go in?")
    time.sleep(2)
    answer = input("Make your choice, Yes OR No")
    time.sleep(2)
    print ("The answer",answer ,"got you stuck in a hole")
    time.sleep(2)
    print ("But you find a secret passage")
    answer = input("Do you go through the door, Yes or No?")
    if answer == "No":
            time.sleep(2)
            print ("Game Over.")
    elif answer == "Yes":
        time.sleep(2)
        print("You hear a strange voice")
        time.sleep(2)
    answer = input("What do you say to the Voice, Hello or Who are you?")
    if answer == "Hello":
        print ("Hello")
    elif answer == "Who are you?":
        print ("Im your worst nightmare")
    time.sleep(2)
    print("You try and escape the labyrinth and turn a large gate with a gnome on the over end")
    answer = input("Do you open the gate, Yes Or No?")
    if answer == "Yes":
        time.sleep(3)
        print ("Game Over, you get brutally killed by a gnome, good job")
        os._exit(0)
    elif answer == "No":
        time.sleep(3)
        print ("You go the other way and see a light at the end of the tunnel")
    answer = input("You see your family outside crying and waiting for you, do you go with them?")
    if answer == "Yes":
        print("You have a nice ending and you're sorry you ran away")
        print("You have been graded: ")
    elif answer == "No":
        print("God smites you for being stupid.")

        os._exit(0)`

1 个答案:

答案 0 :(得分:2)

取这个块,例如

print ("But you find a secret passage")
answer = input("Do you go through the door, Yes or No?")
if answer == "No":
        time.sleep(2)
        print ("Game Over.")
elif answer == "Yes":
    time.sleep(2)
    print("You hear a strange voice")
    time.sleep(2)
# continuation

如果用户输入"否"它将打印" Game Over" - 我认为这是正确的。但是,程序中的控制流程会继续超过if/else块。您需要做的是使用类似sys.exit()之类的东西退出程序,或者确保您的控制流只有前进路径,如果它应该包裹if/else块的真实部分接下来发生的事情

if answer == "No":
        time.sleep(2)
        print ("Game Over.")
elif answer == "Yes":
    time.sleep(2)
    print("You hear a strange voice")
    time.sleep(2)

    # put continuation here