I'm trying to create a very basic RPG-like game where you select a character, give that character a weapon, and then tell it to attack another character with damage based on the stats of the character and the weapon. The weapon
class belongs to a top level class called Equip
. When I try to set an attack damage variable that incorporates the weapon's stats, however, I get:
'Char' object has no attribute 'weapon.'
I have the Char
class weapon
value set to None
as default. But below, I have given the character (dean
) a weapon by using dean.weapon = sword
(which is a weapon
). I've tried changing weapon.wgt
to self.weapon.wgt
but that doesn't seem to help.
See the pertinent parts of the code, leaving out the attack
code because I don't think it's relevant to the question and will clutter up things, but I will if it's necessary.
I believe the code is a mess, so constructive critique will be appreciated.
Code:
class Char(object):
def __init__(self, name):
name = name
hp = 300
mp = 10
strn = 1
dmg = 1
dex = 1
armor = 0
weapon = None
attack_speed = dex
intact_bones = ["right arm", "left arm", "right leg", "leg leg", "skull", "sternum", "nose"] # JUST ASSUME RIGHT SIDE IS PRIMARY SIDE FOR NOW
broken_bones = [] ### define what to do per bone if bone is in this list
dmg = strn * self.weapon.wgt
class Equip(object):
wgt = 1
desc = ""
def __init__(self, name):
self.name = name
class weapon(Equip):
impact = 1
sharp = 1
def __init__(self, name):
self.name = name
sword = weapon("Sword")
sword.wgt = 10
sword.impact = 6
sword.sharp = 7
dean.weapon = sword
dean.attack(hamilton)
Remember to use
self
, as in your other__init__
methods, to make attributes of the instance.Also, I think the other attributes should also be instance attributes, i.e., set them in the
__init__
methods and useself
, e.g.self.wgt
,self.impact
,self.broken_bones
, etc.Change the body of the constructor, because now you are assigning values to local variables rather than to the object variables: