[英]problem with Classes and dictionaries in python
我正在嘗試將舊的游戲引擎轉換為使用類而不是僅詞典的較新版本。
我的目標是從兩個不同的類中添加相似的鍵值,以修改播放器的統計信息。
class player:
def __init__(self, name, level, xp, nextlvlxp, stats, inventory):
self.name = ''
self.level = 1
self.xp = 0
self.nextlvlxp = 25
self.stats = {
'str': [],
'dex': [],
'con': [],
'int': [],
'wis': [],
'cha': [],
'modstr': [],
'moddex': [],
'modcon': [],
'modint': [],
'modwis': [],
'modcha': []
}
self.combat_stats = {
'totalhp': [],
'currenthp':[],
'totalmp': [],
'currentmp': [],
'baseatk': [],
'attack': [],
'speed': [],
'perception':[]
}
self.noncombat_stats = {
'evasion': [],
}
self.inventory = {}
class Item():
def __init__(self, name, description, stats, value):
self.name = name
self.description = description
self.stats = {}
self.value = value
class Weapon(Item):
def __init__(self, name, description, stats, value):
self.stats = stats
super().__init__(name, description, value)
def __str__(self):
return "{}\n=====\n{}\nValue: {}\nStats: {}".format(self.name, self.description, self.value, self.stats)
class Sword(Weapon):
def __init__(self):
super().__init__(name="Sword",
description="A common shortsword",
stats = {
'str': 2,
'dex': 1
},
value = 10)
示例:項目“ sword”“ str” +播放器“ str” =播放器“ modstr”
我希望能夠將這些值以及dex的下一個值相加,我希望以非重復的方式進行。 我不太確定從哪里開始,我今天才剛剛學會如何編寫類。 有什么方法可以瀏覽字典並將具有相同鍵的值相加?
這基本上就是您要執行的操作,但是由於您的默認屬性為空列表,因此我不確定您的情況。
a = {1: 2, 3: 4, 5: 6}
b = {1: 8, 3: 2, 7: 8}
for key in a:
if key in b:
a[key] += b[key]
例如:
class Pool:
def __init__(self, value):
self.value = value
self.max_value = value
class Stats:
def __init__(self, strength=0, dexterity=0, constitution=0, intelligence=0, wisdom=0, charisma=0):
self.strength = strength
self.dexterity = dexterity
self.constitution = constitution
self.intelligence = intelligence
self.wisdom = wisdom
self.charisma = charisma
def __add__(self, stats):
return Stats(
self.strength + stats.strength,
self.dexterity + stats.dexterity,
self.constitution + stats.constitution,
self.intelligence + stats.intelligence,
self.wisdom + stats.wisdom,
self.charisma + stats.charisma
)
def copy(self):
return Stats(
self.strength,
self.dexterity,
self.constitution,
self.intelligence,
self.wisdom,
self.charisma
)
def __repr__(self):
return str(vars(self))
class Equipment:
def __init__(self):
self.weapon = Weapon('None', 'None', 0, Stats())
self.chest = Weapon('None', 'None', 0, Stats())
self.hands = Weapon('None', 'None', 0, Stats())
self.head = Weapon('None', 'None', 0, Stats())
self.legs = Weapon('None', 'None', 0, Stats())
def __repr__(self):
return str(vars(self))
class Player:
def __init__(self, name, next_xp, stats, inventory):
self.xp = 0
self.name = name
self.stats = stats
self.next_xp = next_xp
self.inventory = inventory
self.equipment = Equipment()
def get_stats(self):
stats = self.stats.copy()
elist = vars(self.equipment)
for v in elist:
stats += elist[v].stats
return stats
class Item:
def __init__(self, name, description, value):
self.name = name
self.description = description
self.value = value
class Weapon(Item):
def __init__(self, name, description, value, stats):
Item.__init__(self, name, description, value)
self.stats = stats
def __repr__(self):
return str(vars(self))
def main():
weapons = {}
weapons['sword'] = Weapon('sword', 'common sword', 10, Stats(2, 1))
weapons['intelligence sword'] = Weapon('intelligence sword', 'sword of intelligence', 10, Stats(2, 1, intelligence=1))
player = Player('Me', 25, Stats(5, 4, 6, 3, 3, 4), {})
print(player.get_stats())
player.equipment.weapon = weapons['sword']
print(player.get_stats())
player.equipment.weapon = weapons['intelligence sword']
print(player.get_stats())
main()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.