使用唯一ID(python)将项目添加到库存中
我正在尝试编写一个RPG游戏,我一直在研究如何使用一个唯一的ID来获取所有数据来调用物品、怪物、任务等。这基本上是基于中的顶部答案 我不想将所有数据传递给使用唯一ID(python)将项目添加到库存中,python,Python,我正在尝试编写一个RPG游戏,我一直在研究如何使用一个唯一的ID来获取所有数据来调用物品、怪物、任务等。这基本上是基于中的顶部答案 我不想将所有数据传递给Item类,而是调用一个方法或函数,该方法或函数包含所有项的字典,然后根据一个唯一的ID将数据传递给Item 我目前拥有的相关代码如下(可以找到所有代码): 我让它工作了,但可能不是最有效的方法,如下所示: 1) 删除了ListItem类 2) 将字典添加到main方法 3) 将变量iid(项目ID)设置为房间中项目的任何值 4) 将项的每个值
Item
类,而是调用一个方法或函数,该方法或函数包含所有项的字典,然后根据一个唯一的ID将数据传递给Item
我目前拥有的相关代码如下(可以找到所有代码):
我让它工作了,但可能不是最有效的方法,如下所示: 1) 删除了ListItem类 2) 将字典添加到main方法 3) 将变量iid(项目ID)设置为房间中项目的任何值 4) 将项的每个值传递给项类 但是,这并不限制我在每个位置只能购买一件物品
location = {
2: {"name": "Cave - Upper area",
"description": "Placeholder",
"west": 1,
"south": 4,
"item": "sword",
"iid": 1},
all_items = {
1: {"name": "Sword", "dmg": 5, "arm": 1, "val": 10, "desc": "A rusty looking sword"},
100: {"name": "Beer", "desc": "A foaming mug of ale", "dmg": 1, "arm": 1, "val": 1}
}
elif move[0] == "get":
if "iid" in location[currentLocation] and move[1] in location[currentLocation]["item"]:
iid = location[currentLocation]["iid"]
inventory.add_item(Item(all_items[iid]["name"], all_items[iid]["dmg"], all_items[iid]["arm"], all_items[iid]["val"], all_items[iid]["desc"]))
print("%s added to inventory!\n" % all_items[iid]["name"])
del location[currentLocation]["item"]
else:
print("\nThere is no %s here!\n" % move[1])
您似乎将
ListItem.list
视为一个类方法,因此您不一定需要ListItem中的任何内容。但是,我并不清楚您想要实现什么。为什么ListItem()
首先是一个类?除非我读错了,否则它只是一个动作,在这种情况下,类可能不适合它。在我看来,列表
更适合作为库存
的一种方法,FWIW。感谢迄今为止的答案。我是编程新手,所以我试图理解这些概念。为了澄清我试图实现的目标:而不是当玩家拿起一件物品并调用inventory时。将我想要的物品(物品(“剑”,10,1,10,1,“生锈的剑”)添加到“inventory”。添加物品(物品(这里的唯一ID引用所有将数据传递给物品类的数据))
location = {
2: {"name": "Cave - Upper area",
"description": "Placeholder",
"west": 1,
"south": 4,
"item": "sword",
"iid": 1},
all_items = {
1: {"name": "Sword", "dmg": 5, "arm": 1, "val": 10, "desc": "A rusty looking sword"},
100: {"name": "Beer", "desc": "A foaming mug of ale", "dmg": 1, "arm": 1, "val": 1}
}
elif move[0] == "get":
if "iid" in location[currentLocation] and move[1] in location[currentLocation]["item"]:
iid = location[currentLocation]["iid"]
inventory.add_item(Item(all_items[iid]["name"], all_items[iid]["dmg"], all_items[iid]["arm"], all_items[iid]["val"], all_items[iid]["desc"]))
print("%s added to inventory!\n" % all_items[iid]["name"])
del location[currentLocation]["item"]
else:
print("\nThere is no %s here!\n" % move[1])