文字冒险游戏无法产生正确的输出

时间:2019-06-03 21:45:29

标签: python

我正在尝试在python上创建文本逃生室游戏。当我运行它时,它不会向我的库存阵列添加正确的东西。基本上,我可以使用一些项目来创建另一个项目,但是当我使用它并抓住一个密钥时,它将d代替密钥添加到我的库存中。但是当我抓住钥匙时

main.py:

?
key = key("key","Gee golly, I wonder what you use a key on (a safe in case you're stupid, that's what.)")

room.append(paperclip)
room.append(string)
room.append(key)
room.append(bed)
room.append(bed_sheets)
room.append(loose_pipe)
room.append(safe)
room.append(door)
room.append(window)
room.append(windowsill)

在items.py中:

def inventoryPrint (inventory):
  for i in range(len(inventory)):
    if i == len(inventory) - 1:
      print inventory [i].name
    else:
      print inventory [i].name + ", ",

def grab(item):
  inventory.append(room.pop(room.index(item)))
def use(item):
   item2 = input("What item are you using this on?")
   for i in range (len(inventory)):
     if inventory[i].name == item2:
      item3 = inventory[i]
      item.use(item3)
      return
   for i in range (len(room)):
     if room[i].name == item2:
      item3 = room[i]
      item.use(item3)
      return

def examine(item):
  print item.description


while run:
  itemfound = False
  command = input()
  if (command.startswith("look around")):
    print room_description
  elif (command.startswith("examine")or command.startswith("look")):
    words = command.count(" ")
    if words == 1:
      item = command.split(" ")
      item = item[1]
    elif words >= 2:

      item = command.split(" ",1)
      item = item[1]
    for i in range (len(room)):
      if room[i].name == item:
        print room[i].description
        itemfound = True
        break
    if itemfound == False:
      print "I don't know what your talking about dude. Try something else. I mean, it's not like I have 20/20 vision! Jeez!"
  elif (command.startswith("pickup") or command.startswith("take") or command.startswith ("grab")):
    words = command.count(" ")
    if words == 1:
      item = command.split(" ")
      item = item[1]
    elif words >= 2:
      item = command.split(" ",1)
      item = item[1]
    for i in range (len(room)):
      if room[i].name == item:
        if room[i].pickup == True:
          grab(room[i])
          itemfound = True
          print "You succesfully gained the " + item + " congratulations! Now leave me alone until I have to narrate again!!!"
        else:
          print "You can't pick that up, you're not Hercules"
        break
    if itemfound == False:
      print "You can't pickup something that I was to lazy to code for... DUHH!"
  elif (command.startswith ("use")):
    words = command.count(" ")
    if words == 1:
      item = command.split(" ")
      item = item[1]
    elif words >= 2:
      item = command.split(" ",1)
      item = item[1]
    for i in range (len(inventory)):
      if inventory[i].name == item:
        use(inventory[i])
        itemfound = True
        break
    if itemfound == False:  
      print "Chigidee check your inventory before you rigidee wreck your inventory. (If your not hip like me, that means that item isn't in your inventory." 
  elif (command.startswith ("inventory")):
    inventoryPrint(inventory) 
  else:
    print "I don't know what your talking about. Do you English??"



我没有添加所有代码,但可以在需要时添加

一旦我在钥匙上使用了钩子-它应该将库存添加到钥匙上,但是库存看起来像这样: 钩子d

1 个答案:

答案 0 :(得分:0)

class hook(item):
  pickup = True
  def use(self,item2):      
    if item2.name == 'key':
      inventory.append(key)
      print "Despite your scrawny(yes i said scrawny) arms, you manage to pull the key out using your hook."
    else:
      print "you don't know what do with those things. Try something else"

除非这是伪代码,否则您在哪里定义密钥?我认为您应该这样做

inventory.append(item2)