Posts: 11
Threads: 5
Joined: Jul 2019
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.
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) 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?
Posts: 4,220
Threads: 97
Joined: Sep 2016
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] That's basically what you would do, however I'm not sure in your case, since your default attributes are empty lists.
Posts: 544
Threads: 15
Joined: Oct 2016
Example
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()
99 percent of computer problems exists between chair and keyboard.
Posts: 11
Threads: 5
Joined: Jul 2019
(Jul-30-2019, 06:37 PM)Windspar Wrote: Example
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()
Wow, this is exactly what I needed, and it also shows me how much more I have to learn just to become a beginner at this, lol. Time to study this and break it down so I can maybe figure out how classes work and how i can learn to manipulate them, too.
|