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