Python文字冒险游戏输入无限循环

时间:2018-08-07 06:58:18

标签: python-3.x user-input adventure

我是一个初学者,通过创建文本冒险游戏学习python编码。我正在使用多种资源,而没有教程。我创建了一个开始场景,它要求您提供输入或执行某项命令。我与对象(扫帚和椅子)交互的命令运行良好。我在更改场景,检查库存和查看周围环境时遇到问题。当我输入这些命令时,它只是简单地再次请求输入,而不是显示库存清单,描述我的周围环境(使用功能CabFrontPorchAction)并将我的位置更改为悬崖并显示有关悬崖的信息。

这是我的全部代码,其中我遇到的部分用#分隔

# text game (similar to zork)
name = 1

location = "cabinFrontPorch"

loadWeight = 0

description = {
    "chair" : "It is a wooden chair there is really nothing special about it. Probably good for pondering lifes greatest questions.",
    "broom": "It is a wooden broom with old-fasioned straw bristles. It looks like a witch would love one of these bad boys!"
}

weightList ={
    "broom": 3,

}

inventory = [
  "",
]

cabinFrontPorch = [
  "chair",
  "broom",
]

heavy = [
    "chair",
    ]

goCommand = [
  "go north",
  "go south",
  "go east",
  "go west",
  "go up",
  "go down",
  "go northwest",
  "go northeast",
  "go southeast",
  "go southwest",
]

objectCommand = [
  "get",
  "drop",
  "push",
  "pull",
  "examine",
  "use",
  "attack"
]

def addWeight(x):
  global loadWeight;
  loadWeight = loadWeight + x;

def listItem(list):
  for item in list:
    print("There is also a " + item)

def objectAction(object, location, action):
    if object in inventory or object in location:
            if action == "get":
                if object not in inventory:
                    if loadWeight <= 10:
                        if object in heavy:
                            #Create heavy list
                            print("The " + object + "is too heavy to carry")
                        else:
                            inventory.append(object)
                            location.remove(object)
                            print("Taken.")
                            x = weightList[object]
                            addWeight(x)
                            print("Your are carrying " + str(loadWeight) + "/10 units of weight.")
                    else:
                        print("You have too much stuff in your hands")
                else:
                    print("You already have that in your hand")
            elif action == "push":
                if object not in inventory:
                    if object in pushable:
                        print("This object is pushable")
                    else:
                        print ("Why are you so intent on disturbing the " + object + "? It is just a " + object + "!")
                else:
                    print("You can't push this because it is in your inventory")
            elif action == "pull":
                if object not in inventory:
                    if object in pullable:
                        print("This object is pullable")
                    else:
                        print ("Why are you so intent on disturbing the " + object + "? It is just a " + object + "!")
                else:
                    print("You can't pull this because it is in your inventory")
            elif action == "examine":
                print(description[object])
                #write description dictionary
            elif action == "use":
                print("You sit down for a while taking this time to enjoy life and ponder the deeper philosophies if the world. You realize that there is an entire world out there that is yours for the taking but you also know that you aren't going to accomplish anything just sitting in a chair all day. It is with this that you realize you just wasted time by sitting in a chair.")
            elif action == "drop":
                if object in inventory:
                    print("Dropped.")
                    location.append(object)
                    inventory.remove(object)
                else:
                    print("You can't drop something that's not already in your inventory")
            elif action == "attack":
                print("I've seen some crazy stuff but I've never seen someone attack an " + object + "!")
    else:
        print("The " + object + " is not in this area.")

def intro():
  print("Welcome to Korz!")
  print("")
  print("In this world of interactive fiction you will")
  print("find lots of treasure, monsters and puzzles to")
  print("test the limits of your human mind")
  print("You will be able to LOOK at your surroundings")
  print("EXAMINE certain objects")
  print("USE objects")
  print("ATTACK monsters")
  print("OPEN and CLOSE doors")
  print("PUSH and PULL objects")
  print("GET and DROP items")
  print("You may view your inventory at anytime")
  print("And perhaps most importantly GO places")
  print("(UP DOWN NORTH SOUTH EAST WEST)")
  print("It is now up to you to use this virtual world")
  print("To explore all of the possibilities and to have")
  print("an amazing ADVENTURE")
  name = input("What is your NAME Adventurer? \n>>>")
  print ("Allright " + name +"! You are on your own from here! Have Fun!")
  cabinFrontPorchAction()

def cabinFrontPorchAction():
  location = "cabinFrontPorch";
  print("You are standing on the porch on the north side of a log cabin")
  print("To the north of the porch is a forest with lush evergreen trees")
  print("East of the cabin is a creek that runs north to south through the woods")
  print("West of the cabin are sheer cliffs that look out over a vast blue ocean")
  listItem(cabinFrontPorch)
  while location == "cabinFrontPorch":
    requestInputCFP()
###########################################
def requestInputCFP():
    action = input(">");
    action = action.lower();
    action = action.split();
    if action in goCommand:
        if action == "go south":
            print ("The door is locked shut. To go this direction unlock the door")
        elif action == "go west":
            cliff()
    elif action == "inventory":
        if not inventory:
            print("You are empty handed")
        else:
            print(inventory)
    elif action == "look":
        cabinFrontPorchAction();
    elif len(action) > 1:
        if action[1] == "broom":
            objectAction("broom" ,cabinFrontPorch, action[0])
        elif action[1] == "chair" or action[1] == "wooden":
            objectAction("chair" , cabinFrontPorch, action[0])


def cliff():
  location = "cliff";
  print("You come up to the edge of a sheer cliff just east of a vast blue ocean.")
  print("It would be impossible to get down to the beach this way.")
  print()
########################################################
intro()

1 个答案:

答案 0 :(得分:0)

我认为这与您的action.split()有关。该命令将在空格上分割并返回一个数组:请参见enter link description here。本质上就是这样:

Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> action = "go north"
>>> action = action.split()
>>> print (action)
['go', 'north']
>>> "go north" == action
False
>>>

您可以看到该数组不等于“向北”命令,因此它不在有效命令列表中。

相关问题