My code is below. For now I'm focusing on the lines where health (and
armor) are increased in each character class. Let's say I decided to
Allow me to ask something silly -- but since your "adjust" methods
are written en-bloc, would it be safe to presume your RPG is
"level-based" (ie, all characteristics go up when the character
increases a level)? (My favored, though unplayed in yes, RPG was NOT
level-based; any characteristic/skill could change state independent of
any other).
If so, you might want to consider making the characteristics
"read-only" properties, with the getter method computing the
characteristic as: ch = base_ch + ( (level-1) * level_increment)
change the amount of increase in the future. As it is now, I'd have to
go to each character class and change the number so that each is still
in a good relation to the other (right now: 3, 2, 1; later: perhaps 4,
3, 2, 1, if I added a new class -- i.e., change Fighter from 3 to 4,
Thief from 2 to 3, in other words increase them all by 1). So instead of
changing each one, is there a simple, clean way of just changing a
single number so that this change is reflected in all classes? Hope that
makes sense.
Sounds more like you are trying to undo the whole reason for
creating a class hierarchy in the first place -- that is to get
/independent/ pluggable components; you are now trying to cross-couple
the classes: having each class know something about the others'
internals. <snip>
Peruse this variation (and watch out for line wrapping):
-=-=-=-=-=-=-=-
class Character(object):
_level_inc = {"health" : 0,
"armor" : 0,
"attack" : 0,
"defense" : 0,
"magic_attack" : 0,
"magic_defense" : 0,
"dexterity" : 0,
"intelligence" : 0,
"strength" : 0 }
_base_adjustments = {"health" : 0,
"armor" : 0,
"attack" : 0,
"defense" : 0,
"magic_attack" : 0,
"magic_defense" : 0,
"dexterity" : 0,
"intelligence" : 0,
"strength" : 0 }
def __init__(self, name, strength, dexterity, intelligence):
self.name = name
self._health = 10
self._strength = strength
self._dexterity = dexterity
self._intelligence = intelligence
self._level = 0
self._armor = 0
self._attack = 0
self._defense = 0
self._magic_attack = 0
self._magic_defense = 0
self._adjust_attributes()
def _adjust_attributes(self):
for k, v in self._base_adjustments.items():
attr = "_%s" % k
setattr(self, attr, getattr(self, attr) +
self._base_adjustments[k])
def _gethealth(self):
return self._health + self._level * self._level_inc["health"]
health = property(_gethealth, None, None)
def _getarmor(self):
return self._armor + self._level * self._level_inc["armor"]
armor = property(_getarmor, None, None)
def _getattack(self):
return self._attack + self._level * self._level_inc["attack"]
attack = property(_getattack, None, None)
def _getdefense(self):
return self._defense + self._level * self._level_inc["defense"]
defense = property(_getdefense, None, None)
def _getmagic_attack(self):
return self._magic_attack + self._level *
self._level_inc["magic_attack"]
magic_attack = property(_getmagic_attack, None, None)
def _getmagic_defense(self):
return self._magic_defense + self._level *
self._level_inc["magic_defense"]
magic_defense = property(_getmagic_defense, None, None)
def _getstrength(self):
return self._strength + self._level *
self._level_inc["strength"]
strength = property(_getstrength, None, None)
def _getdexterity(self):
return self._dexterity + self._level *
self._level_inc["dexterity"]
dexterity = property(_getdexterity, None, None)
def _getintelligence(self):
return self._intelligence + self._level *
self._level_inc["intelligence"]
intelligence = property(_getintelligence, None, None)
def _getlevel(self):
return self._level + 1
level = property(_getlevel, None, None)
def levelUp(self):
self._level += 1
def __str__(self):
return """%s\t\tLevel:\t%s
Health:\t%s
Armor:\t%s
Attack:\t%s
Defense:\t%s
Magic Attack:\t%s
Magic Defense:\t%s
Dexterity:\t%s
Intelligence:\t%s
Strength:\t%s\n""" % (self.name, self.level,
self.health,
self.armor,
self.attack,
self.defense,
self.magic_attack,
self.magic_defense,
self.dexterity,
self.intelligence,
self.strength)
class Fighter(Character):
_level_inc = {"health" : 2,
"armor" : 2,
"attack" :2,
"defense" : 2,
"magic_attack" : 0,
"magic_defense" : 0,
"dexterity" : 0,
"intelligence" : 0,
"strength" : 1 }
_base_adjustments = {"health" : 3,
"armor" : 3,
"attack" : 3,
"defense" : 2,
"magic_attack" : 0,
"magic_defense" : 0,
"dexterity" : 0,
"intelligence" : 0,
"strength" : 1 }
class Thief(Character):
_level_inc = {"health" : 1,
"armor" : 1,
"attack" : 1,
"defense" : 1,
"magic_attack" : 0,
"magic_defense" : 1,
"dexterity" : 1,
"intelligence" : 0,
"strength" : 0 }
_base_adjustments = {"health" : 2,
"armor" : 2,
"attack" : 1,
"defense" : 1,
"magic_attack" : 0,
"magic_defense" : 1,
"dexterity" : 1,
"intelligence" : 0,
"strength" : 0 }
class Mage(Character):
_level_inc = {"health" : 1,
"armor" : 1,
"attack" : 0,
"defense" : 0,
"magic_attack" : 1,
"magic_defense" : 1,
"dexterity" : 0,
"intelligence" : 1,
"strength" : 0 }
_base_adjustments = {"health" : 1,
"armor" : 1,
"attack" : 0,
"defense" : 0,
"magic_attack" : 2,
"magic_defense" : 2,
"dexterity" : 0,
"intelligence" : 1,
"strength" : 0 }
if __name__ == "__main__":
fighter = Fighter("Wulfraed", 10, 10, 8)
thief = Thief("R'k'n", 8, 13, 11)
mage = Mage("Draigarian", 5, 11, 15)
print fighter
print thief
print mage
fighter.levelUp()
fighter.levelUp()
thief.levelUp()
mage.levelUp()
print fighter
print thief
print mage
-=-=-=-=-=-=-=-
Notice how each subclass, currently, is JUST a pair of dictionaries,
listing the base (creation) adjustment values, and the value increases
per level (my assumption, but it shows how things can be done). All the
code that affects those characteristics is still in the base class.
You could probably store those dictionaries in some file and have an
initialization function the uses the class name to populate the class
with the proper dictionary. That would put those, at least, into one
easily edited location... Maybe an INI file using config_parser?
[_base_adjustment]
attr = value
.....
[_fighter_adjustment]
attr = value
.....
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/