尝试使用变量引用属性(Python)

2024-09-27 07:17:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python自动取款机里混日子,试着做一个小游戏类型的东西。到目前为止还没有做太多的工作,但我希望有一个作战系统可以让你瞄准特定的身体部位(我选择将它们定义为一个物体)。下面的代码旨在获取一个目标单元、该单元的一部分和用来攻击的手(在本代码段中仅右侧),并调用目标bodypart的一个方法来“破坏”它。你知道吗

    def attack(self, target, part, hand):
        if hand == 'right':
             if self.righthand.o <> []:    #this checks if the righthand has a weapon
                 dmg = self.righthand.o[0].getdmg()  #gets damage from the weapon
                 target.part.deltahealth(dmg)  #reduces the bodyparts HP value

现在问题出现在上面的第5行。在我的脑子里,这是为了一个特定的部分。例如target.lefthand.deltahealth(dmg)或target.righthand.deltahealth=目标.righthand.deltahealth(德国马克)

    class Bodypart(object):

        def __init__(self, e, t):
            self.MHP = self.gmh(e,t) #this calculates a health, not relevant really
            self.CHP = self.gmh(e,t)
            self.holding()
            self.status = 'Healthy'

       def deltahealth(self,d):
            self.CHP = self.CHP - d  #specifically this

但是,它不起作用,因为它查找的是bodypart“part”,而不是part的值。不管怎样,有没有办法让这一切按我所期待的方式进行?你知道吗


Tags: theselftarget目标ifdefthis单元

热门问题