【问题标题】:Trouble With Text Adventure Ending Between The Switch of Rooms在房间切换之间结束文字冒险的麻烦
【发布时间】:2015-03-01 15:24:03
【问题描述】:

我有一个文字冒险,在房间切换之间结束。我认为这是我正在使用的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.")

【问题讨论】:

  • 你的问题真的需要发布 477 行代码吗?
  • 没有。但是谁知道...也许有人会玩它:)
  • 也许有人会,一旦你让它正常工作。 ;-) 我不想因为一个错误而在大结局之前被卡住。
  • 明白。三天前我才开始使用 Python。道歉

标签: python loops text input break


【解决方案1】:

您的代码由三个“主循环”组成,例如

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

但您的代码将很快失控。您需要学习将其分解为多个部分(函数和类/方法),以便您可以单独处理每个部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 2015-04-23
    相关资源
    最近更新 更多