python - 文字冒险游戏没有产生正确的输出
问题描述
我正在尝试在 python 上创建一个文本逃生室游戏。当我运行它时,它不会将正确的东西添加到我的库存数组中。基本上我可以使用一些物品来创建另一个物品,但是当我使用它并抓住钥匙时,它会将 d 添加到我的库存中而不是钥匙。但是当我抓住钥匙时,它
主要.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)
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??"
在 items.py 中:
class item:
pickup = False
def __init__(self,name,description):
self.name = name
self.description = description
def use(self,item2):
print "I don't know how to do that"
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"
我没有添加所有代码,但如果需要可以添加
一旦我在钥匙上使用钩子 - 它应该将库存添加到钥匙,但库存看起来像这样:钩子,d
解决方案
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)
推荐阅读
- javascript - 如何使用 @ 登录键 javascript 获取键的值
- php - 使用带有 Ajax 的选择元素在 WordPress 中按一个 meta_key 和两个 meta_value 过滤帖子
- indexing - INDEX MATCH 产品代码 两张表中的 INDEX
- c# - 空引用异常中的无效 MoveNext()
- typescript - 如何在警报框离子2中添加列表
- c# - 如果找到位图
- python - 根据 json 文件中的列表 test-data 对测试进行参数化
- javascript - 如何使用 Angular 4 获取系统的 MAC 地址
- excel - 将数据复制到新工作表会引发“下标超出范围”
- c# - 球没有在桨/墙上正确弹跳