我正在学习 python,并尝试编写一个基于角色热区的伤口系统。这是我写的。别对我评价太多。
class Character:
def __init__ (self, agility, strength, coordination):
self.max_agility = 100
self.max_strength = 100
self.max_coordination = 100
self.agility = agility
self.strength = strength
self.coordination = coordination
def hit (self, hit_region, wound):
self.hit_region = hit_region
self.wound = wound
#Hit Zones
l_arm=[]
r_arm=[]
l_leg=[]
r_leg=[]
hit_region_list = [l_arm , r_arm, l_leg, r_leg]
#Wound Pretty Names
healthy = "Healthy"
skin_cut = "Skin Cut"
muscle_cut = "Muscle Cut"
bone_cut = "Exposed Bone"
hit_region.append(wound)
john = Character(34, 33, 33)
john.hit(l_arm, skin_cut)
我希望 Skin_cut 输入被识别为“Skin Cut”,然后添加到 l_arm,我将其定义为列表。但是,我总是收到名称错误(l_arm 未定义)。如果我使用“wound”作为第一个参数重写该方法,则名称错误现在会出现“wound”未定义的情况。这告诉我这是我错过的课程结构中的一些东西,但我不知道是什么。
我改变了我之前的回答。
class Character:
def __init__ (self, agility, strength, coordination):
self.max_agility = 100
self.max_strength = 100
self.max_coordination = 100
self.agility = agility
self.strength = strength
self.coordination = coordination
self.l_arm=[]
self.r_arm=[]
self.l_leg=[]
self.r_leg=[]
self.hit_region_list = [self.l_arm , self.r_arm, self.l_leg, self.r_leg]
self.healthy = "Healthy"
self.skin_cut = "Skin Cut"
self.muscle_cut = "Muscle Cut"
self.bone_cut = "Exposed Bone"
def hit (self, hit_region, wound):
self.hit_region = hit_region
self.wound = wound
hit_region.append(wound)
#Hit Zones
#Wound Pretty Names
john = Character(34, 33, 33)
john.hit(john.l_arm,john.skin_cut)
print john.hit_region
print john.l_arm
运行上面的代码后我得到了这个输出
output:
['Skin Cut']
['Skin Cut']
根据帖子,我认为这就是您想要的。根据您之前的代码,您的声明只能在函数内部访问。现在,您可以通过在构造函数内声明它们来操作特定实例的数据和这些变量。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)