首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用我的文字输入麻烦冒险游戏

用我的文字输入麻烦冒险游戏
EN

Stack Overflow用户
提问于 2015-03-01 03:58:56
回答 1查看 107关注 0票数 0

下面请参阅我的整个项目代码。它还没有完成

这就是游戏的工作原理:你输入一个动作去做,如果它存在的话,玩家就会这么做。代码检查您输入的单词是否与它在特定房间中的任何类似单词匹配。问题是,输入的机会并不总是提供给您,项目只是结束或命令提示关闭。请帮助我,以便在您输入每一个命令后,它保证不会像现在这样关闭,并允许玩家输入一些内容。

代码语言:javascript
复制
import os

os.system("cls")

openleft = '0'
openright = '0'
room = '0'
lights = '0'
draweropen = '0'
key = '0'
unlocked = '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 room.")

if room == '0':
    print("There are two wooden doors.")
    i = input(">>>")
    if 'look' in i:
        print ("You see nothing.")
        i = input(">>>")
    if 'check' in i:
        print ("You see nothing.")
        i = input(">>>")
    if 'open' in i:
        if 'left' not in i:
            if 'right' not in i:
                print("Open what?")
                i = input(">>>")
    if 'open' in i:
        if 'left' in i:
            if 'right' in i:
                print("Pick a single door.")
                i = input(">>>")
    if 'open' in i:
        if 'left' in i:
            if 'right' not in i:
                if openleft == '0':
                    print("You open the left door.")
                    openleft = '1'
                    i = input(">>>")
                else:
                    print(open)
                    i = input(">>>")
    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.")
                    i = input(">>>")
                if unlocked == '1':
                    if openright == '0':
                        print("You open the right door.")
                        openright = '1'
                        i = input(">>>")
                    else:
                        print(open)
                        i = input(">>>")
    if 'unlock' in i:
        if 'right' in i:
            if 'left' in i:
                print("Pick a single door.")
                i = input(">>>")
    if 'unlock' in i:
        if 'right' not in i:
            if 'left' not in i:
                print("Unlock what?")
                i = input(">>>")
    if 'unlock' in i:
        if 'right' not in i:
            if 'left' in i:
                print("It does not have a lock.")
                i = input(">>>")
    if 'unlock' in i:
        if 'right' in i:
            if 'left' not in i:
                if key == '0':
                    print("You cannot without a key.")
                    i = input(">>>")
                if key == '1':
                    if unlocked == '0':
                        print("You unlock the door.")
                        unlocked = '1'
                        i = input(">>>")
                    if unlocked == '1':
                        print("It is already unlocked.")
                        i = input(">>>")
    if 'close' in i:
        if 'left' not in i:
            if 'right' not in i:
                print("Open what?")
                i = input(">>>")
    if 'close' in i:
        if 'left' in i:
            if 'right' in i:
                print("Pick a single door.")
                i = input(">>>")
    if 'close' in i:
        if 'left' in i:
            if 'right' not in i:
                if openleft == '0':
                    print(closed)
                    i = input(">>>")
                else:
                    print("You close the left door.")
                    openleft = '0'
                    i = input(">>>")
    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'
                    i = input(">>>")
    if 'enter' in i:
        if 'right' in i:
            if 'left' in i:
                print("You cannot enter both.")
                i = input(">>>")
    if 'enter' in i:
        if 'right' not in i:
            if 'left' not in i:
                print("Enter what?")
                i = input(">>>")
    if 'enter' in i:
        if 'right' in i:
            if 'left' not in i:
                if openright == '0':
                    print(shut)
                    i = input(">>>")
                if openright == '1':
                    print("You enter.")
                    room = '2'
    if 'enter' in i:
        if 'right' not in i:
            if 'left' in i:
                if openleft == '0':
                    print(shut)
                    i = input(">>>")
                if openleft == '1':
                    print("You enter.")
                    print ("It is a dark room.")
                    room = '1'
    if 'enter' not in i:
        if 'open' not in i:
            if 'close' not in i:
                if 'left' not in i:
                    if 'right' not in i:
                        if 'open' not in i:
                            if 'close' not in i:
                                if 'unlock' not in i:
                                    if 'look' not in i:
                                        if 'check' not in i:
                                            print("What?")
                                            i = input(">>>")
if room == '1':
    print ("You barely see the light switch next to you.")
    i = input(">>>")
    if 'flip' in i:
        if 'switch' in i:
                if lights == '0':
                    print("You turn on the lights.")
                    lights = '1'
                else:
                    print("You turn off the lights.")
                    lights = '0'
    if 'turn' in i:
        if 'on' in i:
            if 'off' not in i:
                if lights == '0':
                    print("You turn on the lights.")
                    lights = '1'
                else:
                    print("The lights are already on.")
    if 'turn' in i:
        if 'off' in i:
            if 'on' not in i:
                if lights == '1':
                    print("You turn off the lights.")
                    lights = '0'
                else:
                    print("The light are already off")
    if 'look' in i:
        if 'drawer' not in i:
            if lights == '0':
                print("It is too dark to see.")
            if lights == '1':
                print("You see a drawer in the corner of the room.")
    if 'check' in i:
        if 'drawer' not in i:
            if lights == '0':
                print("It is too dark to see.")
            if lights == '1':
                print("You see a drawer in the corner of the room.")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-01 04:07:35

您应该使用while循环来持续获取输入,直到出现指定的中断条件。目前,程序中发生的情况是,代码从if语句的末尾掉下来。

代码语言:javascript
复制
if room == '1':
    print ("You barely see the light switch next to you.")
    i = input(">>>")
    if 'flip' in i:
        if 'switch' in i:
                if lights == '0':
                    print("You turn on the lights.")
                    lights = '1'
                else:
                    print("You turn off the lights.")
                    lights = '0'
    if 'turn' in i:
        if 'on' in i:
            if 'off' not in i:
                if lights == '0':
                    print("You turn on the lights.")
                    lights = '1'
                else:
                    print("The lights are already on.")
    if 'turn' in i:
        if 'off' in i:
            if 'on' not in i:
                if lights == '1':
                    print("You turn off the lights.")
                    lights = '0'
                else:
                    print("The light are already off")
    if 'look' in i:
        if 'drawer' not in i:
            if lights == '0':
                print("It is too dark to see.")
            if lights == '1':
                print("You see a drawer in the corner of the room.")
    if 'check' in i:
        if 'drawer' not in i:
            if lights == '0':
                print("It is too dark to see.")
            if lights == '1':
                print("You see a drawer in the corner of the room.")

在这段代码中,输入只被要求一次。如果你做了下面的事情。

代码语言:javascript
复制
if room == '1':
    print ("You barely see the light switch next to you.")
    while(cond):
         rest of the code

然后将cond标记为false,以便在完成后结束循环。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28789936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档