I'm just trying to put together a simple RPG example in order to learn OOP in Python.
I created a base class CharRace(object). Then I created two derived classes from this, Elf(CharRace) and Dwarf(CharRace). In addition, I have a second base class CharClass(object), with two derived classes: Wizard(CharClass) and Warrior(CharClass).
What I'd like to be able to do is create a new class called NewChar, that I can pass a name, a race, and a class, and it will create the appropriate object that inherits all the properties of both the race and the class.
For example, I want to created a class instance of NewChar with something like this: Player1 = NewChar("Bob", Elf, Wizard) Player2 = NewChar("Phil", Dwarf, Warrior)
I sure there's a smart / correct / scalable way to do this, or perhaps my classes should be organized differently, but I'm having trouble finding it. Any help or advice is appreciated!
class CharRace(object):
'race for all characters'
health = 0
mana = 0
race = ""
name = ""
def __init__(self, health, mana, race, name):
self.health = health
self.mana = mana
self.race = race
self.name = name
print ("self = ",self)
def decreaseHealth (self, amount):
self.health = self.health - amount
print ("Decreased health to: ", self.health)
if (self.health) <= 0:
print (self.name, "has died!!!")
def increaseHealth (self, amount):
self.health = self.health + amount
print ("Increased health to: ", self.health)
def printStats(self):
print()
print("Stats for",self.name)
print (" Race =",self.race)
print (" Health =",self.health)
print (" Mana =",self.mana)
CharClass.printStats(self)
def __str__(self):
return (self.name)
class CharClass(object):
'class for all characters'
weapon = ""
armor = 0
def __init__(self, weapon, armor, classname):
self.weapon = weapon
self.armor = armor
self.classname = classname
print ("self = ",self)
def printStats(self):
print (" Weapon =",self.weapon)
print (" Armor =",self.armor)
# Character Classes
class Warrior(CharClass):
'warrior class'
weapon = "Sword"
armor = 200
classname = "Warrior"
def __init__(self):
CharClass.__init__(self, self.weapon, self.armor, self.classname)
class Wizard(CharClass):
'wizard class'
weapon = "Wand"
armor = 100
classname = "Wizard"
def __init__(self):
CharClass.__init__(self, self.weapon, self.armor, self.classname)
# Character Races
class Elf(CharRace):
'Elf subclass'
health = 100
mana = 200
race = "Elf"
def __init__(self, name):
CharRace.__init__(self, self.health, self.mana, self.race, name)
class Dwarf(CharRace):
'Dwarf subclass'
health = 200
mana = 100
race = "Dwarf"
def __init__(self, name):
CharRace.__init__(self, self.health, self.mana, self.race, name)
# This is not an efficient or scalable way to do this....
class ElfWarrior(Elf,Warrior):
'New character creation'
def __init__(self, name):
Elf.__init__(self, name)
Warrior.__init__(self)
class ElfWizard(Elf,Wizard):
'New character creation'
def __init__(self, name):
Elf.__init__(self, name)
Wizard.__init__(self)
class DwarfWarrior(Dwarf,Warrior):
'New character creation'
def __init__(self, name):
Dwarf.__init__(self, name)
Warrior.__init__(self)
class DwarfWizard(Dwarf,Wizard):
'New character creation'
def __init__(self, name):
Dwarf.__init__(self, name)
Wizard.__init__(self)