房间开关之间文本冒险结束的麻烦

时间:2015-03-01 15:24:03

标签: python loops text input break

我有一个文本冒险,在房间的切换之间结束。我认为这是while room = '#'我正在使用,以便玩家可以随时输入。我可以从0号房间转换到1号房间,但是一旦我尝试反之亦然,程序就会结束。请帮忙解决房间之间的过渡问题!

import os

os.system("cls")

openleft = '0'
openright = '0'
openleft = openleft
room = '0'
lights = '0'
drawerclosed = '1'
key = '0'
unlocked = '0'
tableclose = '0'
open = ("It is already open.")
closed = ("It is already closed.")
shut = ("It is closed shut.")

print(" ")
print("*Stuck? Need Some Help? Try Using Some Commands Like:") 
print("Open / Close / Unlock / Enter / Look / Check / ")
print(" ")
print("---------------------------------------------------------------------")
print("Welcome To A Text Adventure")
print("---------------------------------------------------------------------")
print(" ")
print("---------------------------------------------------------------------")
print("The Building")
print("---------------------------------------------------------------------")
print(" ")
print(" ")
print("You awake on the hard floor of a white and empty room.")
print("There are two wooden doors.")

while room == '0':
    i = input(">>>")
    #Looking Around
    if 'look' in i:
        print ("You see nothing of importance aside from two doors.")
    if 'check' in i:
        print ("You see nothing of importance aside from two doors.")
    if 'examine' in i:
        print("You see nothing of importance aside from two doors.")
    #Opening Doors
    if 'open' in i:
        if 'left' not in i:
            if 'right' not in i:
                print("Open what?")
    if 'open' in i:
        if 'left' in i:
            if 'right' in i:
                print("Pick a single door.")
    if 'open' in i:
        if 'left' in i:
            if 'right' not in i:
                if openleft == '0':
                    print("You open the left door.")
                    openleft = '1'
                else:
                    print(open)
    if 'open' in i:
        if 'right' in i:
            if 'left' not in i:
                if unlocked == '0':
                    print("You attempt to open the door, but it's locked.")
                if unlocked == '1':
                    if openright == '0':
                        print("You open the right door.")
                        openright = '1'
                    else:
                        print(open)
    #Unlocking Doors
    if 'unlock' in i:
        if 'right' in i:
            if 'left' in i:
                print("Pick a single door.")
    if 'unlock' in i:
        if 'right' not in i:
            if 'left' not in i:
                print("Unlock what?")
    if 'unlock' in i:
        if 'right' not in i:
            if 'left' in i:
                print("It does not have a lock.")
    if 'unlock' in i:
        if 'right' in i:
            if 'left' not in i:
                if key == '0':
                    print("You cannot without a key.")
                if key == '1':
                    if unlocked == '0':
                        print("You unlock the door.")
                        unlocked = '1'
                    if unlocked == '1':
                        print("It is already unlocked.")
    #Locking Doors
    if 'lock' in i:
        if 'right' in i:
            if 'left' in i:
                print("Pick a single door.")
    if 'lock' in i:
        if 'right' not in i:
            if 'left' not in i:
                print("Lock what?")
    if 'lock' in i:
        if 'right' not in i:
            if 'left' in i:
                print("It does not have a lock.")
    if 'lock' in i:
        if 'right' in i:
            if 'left' not in i:
                if 'unlock' not in i:
                    if unlocked == '0':
                        print("It is already locked.")
                    if unlocked == '1':
                        if key == '1':
                            print("You lock the door.")
                            unlocked = '0'
                        else:
                            print("You cannot without a key.")
    #Closing Doors
    if 'close' in i:
        if 'left' not in i:
            if 'right' not in i:
                print("Open what?")
    if 'close' in i:
        if 'left' in i:
            if 'right' in i:
                print("Pick a single door.")
    if 'close' in i:
        if 'left' in i:
            if 'right' not in i:
                if openleft == '0':
                    print(closed)
                else:
                    print("You close the left door.")
                    openleft = '0'
    if 'close' in i:
        if 'right' in i:
            if 'left' not in i:
                if openright == '0':
                    print(closed)
                else:
                    print("You close the right door.")
                    openright = '0'
    #Entering Doors
    if 'enter' in i:
        if 'right' in i:
            if 'left' in i:
                print("You cannot enter both.")
    if 'enter' in i:
        if 'right' not in i:
            if 'left' not in i:
                print("Enter what?")
    if 'enter' in i:
        if 'right' in i:
            if 'left' not in i:
                if openright == '0':
                    print(shut)
                if openright == '1':
                    print("You enter.")
                    print("There is a large and immediate drop several feet after the door frame.")
                    room = '2'
    if 'enter' in i:
        if 'right' not in i:
            if 'left' in i:
                if openleft == '0':
                    print(shut)
                if openleft == '1':
                    print("You enter.")
                    print ("It is a dark room.")
                    print ("You barely see the light switch next to you.")
                    room = '1'
    if 'go' in i:
        if 'right' in i:
            if 'left' in i:
                print("You cannot enter both.")
    if 'go' in i:
        if 'right' not in i:
            if 'left' not in i:
                print("Enter what?")
    if 'go' in i:
        if 'right' in i:
            if 'left' not in i:
                if openright == '0':
                    print(shut)
                if openright == '1':
                    print("You enter.")
                    room = '2'
    if 'go' in i:
        if 'right' not in i:
            if 'left' in i:
                if openleft == '0':
                    print(shut)
                if openleft == '1':
                    print("You enter.")
                    print ("It is a dark room.")
                    print ("You barely see the light switch next to you.")
                    room = '1'
    #If No Actions Are Said
    if 'enter' not in i:
        if 'open' not in i:
            if 'close' not in i:
                if 'unlock' not in i:
                    if 'lock' not in i:
                        if 'look' not in i:
                            if 'check' not in i:
                                if 'go' not in i:
                                    if 'examine' not in i:
                                        print("What?")
while room == '1':
    i = input(">>>")
    #Lights
    if 'switch' in i:
        if 'on' in i:
            if 'off' not in i:
                if 'turn' not in i:
                    if lights == '1':
                        print("The lights are already on.")
                    else:
                        print("You turn on the lights.")
                        lights = '1'
                        tableclose = '0'
    if 'switch' in i:
        if 'on' not in i:
            if 'off' in i:
                if 'turn' not in i:
                    if lights == '0':
                        print("The lights are already off.")
                    if lights == '1':
                        print("You turn off the lights.")
                        lights = '0'
                        tableclose = '0'

    if 'turn' in i:
        if 'on' in i:
            if 'off' not in i:
                if lights == '1':
                    print("The lights are already on.")
                if lights == '0':
                    print("You turn on the lights.")
                    lights = '1'
                    tableclose = '0'

    if 'turn' in i: 
        if 'off' in i:
            if 'on' not in i:
                if lights == '0':
                    print("The light are already off")
                if lights == '1':
                    print("You turn off the lights.")   
                    lights = '0'
                    tableclose = '0'
    #Looking Around
    if 'look' in i:
        if 'drawer' not in i:
            if 'table' not in i:
                if lights == '0':
                    print("It is too dark to see.")
                if lights == '1':
                    print("You see a table in the corner of the room.")
    if 'check' in i:
        if 'drawer' not in i:
            if 'table' not in i:
                if lights == '0':
                    print("It is too dark to see.")
                if lights == '1':
                    print("You see a table in the corner of the room.")
    if 'examine' in i:
        if 'drawer' not in i:
            if 'table' not in i:
                if lights == '0':
                    print("It is too dark to see.")
                if lights == '1':
                    print("You see a table in the corner of the room.")
    #Walking Over To table
    if 'walk' in i:
        if 'table' in i:
            if lights == '0':
                print("It is too dark to see.")
            else:
                if tableclose == '1':
                    print("You are already next to the table")
                if tableclose == '0':
                    print("You walk over to the table.")
                    tableclose = '1'
    if 'go' in i:
        if 'table' in i:
            if lights == '0':
                print("It is too dark to see.")
            else:
                if tableclose == '1':
                    print("You are already next to the table")
                if tableclose == '0':
                    print("You walk over to the table.")
                    tableclose = '1'
    #Look At Table
    if 'look' in i:
        if 'table' in i:
            if lights == '0':
                print("It is too dark to see.")
            else:
                if tableclose == '0':
                    print("From way over here?")
                if tableclose == '1':
                    print("You notice a small drawer on it's underbelly.")
    if 'check' in i:
        if 'table' in i:
            if lights == '0':
                print("It is too dark to see.")
            else:
                if tableclose == '0':
                    print("From way over here?")
                if tableclose == '1':
                    print("You notice a small drawer on it's underbelly.")
    if 'examine' in i:
        if 'table' in i:
            if lights == '0':
                print("It is too dark to see.")
            else:
                if tableclose == '0':
                    print("From way over here?")
                if tableclose == '1':
                    print("You notice a small drawer on it's underbelly.")
    #Looking At Drawer
    if 'look' in i:
        if 'drawer' in i:
            if lights == '0':
                print("It is too dark to see.")
            else:
                if drawerclosed == '0':
                    print("You notice a key hidden in the back.")
                if drawerclosed == '1':
                    print("The outside of it looks very nice.")
    if 'check' in i:
        if 'drawer' in i:
            if lights == '0':
                print("It is too dark to see.")
            else:
                if drawerclosed == '0':
                    print("You notice a key hidden in the back.")
                if drawerclosed == '1':
                    print("The outside of it looks very nice.")
    if 'examine' in i:
        if 'drawer' in i:
            if lights == '0':
                print("It is too dark to see.")
            else:
                if drawerclosed == '0':
                    print("You notice a key hidden in the back.")
                if drawerclosed == '1':
                    print("The outside of it looks very nice.")
    #Opening Drawer
    if 'open' in i:
        if 'drawer' not in i:
            if 'door' not in i:
                print("Open what?")
    if 'open' in i:
        if 'drawer' in i:
            if tableclose == '0':
                print("From way over here?")
            else:
                if drawerclosed == '0':
                    print("It is already open.")
                if drawerclosed == '1':
                    print("You open the drawer.")
                    drawerclosed = '0'
    #Closing Drawer
    if 'close' in i:
        if 'drawer' in i:
            if tableclose == '0':
                print("From way over here?")
            else:
                if drawerclosed == '1':
                    print("It is already closed.")
                if drawerclosed == '0':
                    print("You close the drawer.")
    #Taking Key
    if 'take' in i:
        if 'key' in i:
            if tableclose == '0':
                print("Which key?")
            else:
                if drawerclosed == '1':
                    print("Which key?")
                else:
                    print("You take the key.")
                    key = '1'
    if 'pick' in i:
        if 'up' in i:
            if 'key' in i:
                if tableclose == '0':
                    print("Which key?")
                else:
                    if drawerclosed == '1':
                        print("Which key?")
                    else:
                        print("You pick up the key.")
                        key = '1'
    #Opening Door
    if 'open' in i:
        if 'door' in i:
            if 'drawer' not in i:
                if openleft == '1':
                    print("It is already open.")
                if openleft == '0':
                    print("You open the door.")
                    openleft = '1'
    #Closing Door
    if 'close' in i:
        if 'door' in i:
            if 'drawer' not in i:
                if openleft == '0':
                    print("It is already closed.")
                if openleft == '1':
                    print("You close the door.")
                    openleft = '0'
    #Exiting Room
    if 'enter' in i:
        if 'door' in i:
            if openleft == '1':
                print("You enter.")
                print("There are two wooden doors.")
                room = '0'
            else:
                print(shut)
        else:
            print("Enter what?")
    if 'leave' in i:
        if 'room' in i:
            if openleft == '1':
                print("You enter.")
                print("There are two wooden doors.")
                room = '0'
            else:
                print(shut)
        else:
            print("Leave what?")
    if 'exit' in i:
        if 'room' in i:
            if openleft == '1':
                print("You exit.")
                print("There are two wooden doors.")
                room = '0'
            else:
                print(shut)
        else:
            print("Exit what?")
    #Remind The Right Door Isn't In The Room
    if 'right' in i:
        print("The right door is in the other room.")       
    #If No Actions Are Said
    if 'enter' not in i:
        if 'open' not in i:
            if 'close' not in i:
                if 'look' not in i:
                    if 'check' not in i:
                        if 'go' not in i:
                            if 'examine' not in i:
                                if 'exit' not in i:
                                    if 'take' not in i:
                                        if 'pick' not in i:
                                            if 'up' not in i:
                                                if 'walk' not in i:
                                                    if 'turn' not in i:
                                                        if 'switch' not in i:
                                                            print("What?")
while room == '1':
    i = input(">>>")
    if 'examine' in i:
        if 'ladder' not in i:
            print("You notice a rope ladder to your right.")
    if 'look' in i:
        if 'ladder' not in i:
            print("You notice a rope ladder to your right.")
    if 'check' in i:
        if 'ladder' not in i:
            print("You notice a rope ladder to your right.")

1 个答案:

答案 0 :(得分:2)

您的代码由三个"主循环"组成,如

room = "0"

while room == "0":
    # do stuff

while room == "1":
    # do stuff

while room == "1":
    # do stuff

...我想最后一个应该是while room == "2"

除了从0号室到1号房间到2号房间的线性进展(可能跳过1号房间)之外,这不允许任何事情。任何其他路径都只是结束程序。

您需要一个有向循环图结构,它允许您在图形连接后从一个房间到另一个房间。

"快速修复"将切换到像

这样的结构
room = "0"
while True:
    if room == "0":
        # do stuff
    elif room == "1":
        # do stuff
    elif room == "2":
        # do stuff
    else:
        break

但您的代码很快就会失控。你需要学会将它分解成碎片(函数和类/方法),这样你就可以分别处理每个部分了。