Python Forum
Help with Classes and dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Classes and dictionaries
#1
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?
Reply


Messages In This Thread
Help with Classes and dictionaries - by Tridium - Jul-30-2019, 05:09 PM
RE: Help with Classes and dictionaries - by Tridium - Aug-02-2019, 09:10 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020