类属性不会传递给对象 [英] Class attributes not passing to objects
问题描述
我试图创建一个非常基本的RPG类游戏,你选择一个角色,给这个角色的武器,然后告诉它来攻击与伤害另一个字符根据字符和武器的统计信息。在武器
类属于所谓的顶层类装备
。当我尝试不过来设置结合了武器的统计攻击损伤变量,我得到:
'字符'对象有没有属性'的武器。
我有字符
类武器
值设置为无
为默认值。但低于,我已经给了字符(院长
)使用武器 dean.weapon =剑
(这是一个武器
)。我试着改变 weapon.wgt
到 self.weapon.wgt
,但似乎并没有帮助。
查看code的相关部分,而忽略了攻击
code,因为我不认为这是相关的问题,并会扰乱的事情,但我会,如果是必要的。
我相信,code是一个烂摊子,如此具有建设性的批评将是AP preciated。
code:
类CHAR(对象): 高清__init __(自我,名):
名称=名称
HP = 300
MP = 10
STRN = 1
DMG = 1
DEX = 1
护甲= 0
武器=无 attack_speed = DEX intact_bones = [右臂,左臂,右腿,腿腿,骷髅,胸骨,鼻子]#只是假设右侧是原发性侧NOW broken_bones = [] ###确定每个骨骼做什么,如果骨在此列表 DMG = STRN * self.weapon.wgt
类装备(对象):
WGT = 1
说明= 高清__init __(自我,名):
self.name =名称级武器(装备):
影响= 1
犀利= 1 高清__init __(自我,名):
self.name =名称剑=武器(亮剑)
sword.wgt = 10
sword.impact = 6
sword.sharp = 7
dean.weapon =剑
dean.attack(汉密尔顿)
记住使用自
如您在其他 __的init __
方法,使实例的属性。
高清__init __(自我,名):
self.name =名称
self.hp = 300
...
self.weapon =无
另外,我觉得其他属性也应该是实例属性,也就是说,它们设置在 __的init __
方法和使用自
,例如: self.wgt
, self.impact
, self.broken_bones
等等。
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.
def __init__(self, name):
self.name = name
self.hp = 300
...
self.weapon = None
Also, I think the other attributes should also be instance attributes, i.e., set them in the __init__
methods and use self
, e.g. self.wgt
, self.impact
, self.broken_bones
, etc.
这篇关于类属性不会传递给对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!