Jul-30-2019, 05:09 PM
Hello, I'm am trying to convert my old game engine into a newer version that uses classes instead of just dictionaries.
My goal is to add similar key values together from two different classes in order to modify stats of the player.
Example:items 'sword' 'str' + player 'str' = player 'modstr'
I want to be able to add those together and also the next values for dex, I want to do this in a way that is nonrepetitive. I'm not really sure where to begin, I just barely learned how classes to write Classes today. Is there any way to go through the dictionary and add values together that have the same key?
My goal is to add similar key values together from two different classes in order to modify stats of the player.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
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 ) |
I want to be able to add those together and also the next values for dex, I want to do this in a way that is nonrepetitive. I'm not really sure where to begin, I just barely learned how classes to write Classes today. Is there any way to go through the dictionary and add values together that have the same key?