I am trying to make weapons in my game exclusive to certain classes. I have an item database in form:
itemsList = {
1: {"name": "Padded Armor", "armor": 1, "value": 5, "class": "light"},
2: {"name": "Leather Armor", "armor": 2, "value": 10, "class": "light"},
3: {"name": "Studded Leather Armor", "armor": 3, "value": 25, "class": "light"},
...
19: {"name": "Dagger", "damage" : int(random.randrange(1, 4)), "value": 2, "Type": "Dagger"},
20: {"name": "Dagger + 1", "damage" : int(random.randrange(1, 4) + 1), "value": 200, "Type": "Dagger"},
21: {"name": "Dagger + 2", "damage" : int(random.randrange(1, 4) + 2), "value": 750, "Type": "Dagger"},
22: {"name": "Dagger + 3", "damage" : int(random.randrange(1, 4) + 3), "value": 2000, "Type": "Dagger"}
}
I am going to import, if a class can equip a certain weapon, to a new dictionary,
character.canEquipWeapon = {}
I would like to assign the name, damage, value, and type to a new set of keys in the dictionary, only, I would like to add only certain subsets (daggers, maces, swords) to certain classes.
I have tried
character.canEquipWeapon.update(itemsList[5])
and that just overwrites the dictionary with each new value. How do I go about doing this?
How do you feel about making itemsList
an array instead of a dict? Seems like you're using it as an array anyhow. That way, you could do something like
itemsList2 = [weapon for weapon in itemsList if weapon.type == "dagger"]
or if the weapon types are stored in, say, character.weaponTypes
itemsList2 = [weapon for weapon in itemsList if weapon.type in character.weaponTypes]
You could create a dict with usable items by class, so that for a given class, you have the list of the IDs it can equip as such :
classItem = {
'rogue' : [1,2,3,12,13,14,15],
'mage' : [13,15,16,18],
'soldier' : [1,2,3,4,5,6,7],
}
Then to get the list of items usable by the rogue for instance :
rogueItems = [v for k,v in itemsList.items() if k in classItem['rogue']]
Note that with python2 you'd have to use itemsList.iteritems()
instead of itemsList.items()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.