Python 使用其他类修改类的属性

Python 使用其他类修改类的属性,python,Python,我制作了一个简短的游戏,只是为了尝试使用类和函数,但是当我使用另一个类修改一个类的属性时,它只返回原始的、未修改的属性。我如何解决这个问题?下面是我的代码。我发现将新hp分配给一个变量是可行的,但我想直接用另一个类修改该类的属性。多谢各位 import random class Player: """Describes the main player.""" def __init__(self,level,health

我制作了一个简短的游戏,只是为了尝试使用类和函数,但是当我使用另一个类修改一个类的属性时,它只返回原始的、未修改的属性。我如何解决这个问题?下面是我的代码。我发现将新hp分配给一个变量是可行的,但我想直接用另一个类修改该类的属性。多谢各位

import random

class Player:
    """Describes the main player."""
    def __init__(self,level,health,will):
        """Initializes stats"""
        self.level = level
        self.health = health
        self.full_health = health
        self.will = will

    def default_attack(self, enemy_hp):
        """Normal attack that damages enemy target."""
        damage = 0
        if random.randint(1,100) <= 95:
            damage = (self.level * 2)
            critical_hit = random.randint(1,10)
            if critical_hit == 1:
                damage += int(self.level * 0.5)
                print("Critical hit!")
        enemy_hp -= damage
        print("The enemy took " + str(damage) + " damage.")

    def heal(self):
        """Heals self by 10%."""
        recovered = int(self.full_health * 0.10)
        self.health += recovered
        if self.health > self.full_health:
            self.health = self.full_health
        print("Recovered " + str(recovered) + " HP.")


class enemy:
    """Describes common easy enemy."""
    def __init__(self,level,health,magic):
        """Initializes stats"""
        self.level = level
        self.health = health
        self.magic = magic

    def default_attack(self, protag_hp):
        """Normal attack that damages protagonist target."""
        chance_to_hit = random.randint(1,100)
        damage = 0
        if chance_to_hit <= 90:
            damage = (self.level * 2)
            if chance_to_hit <= 10:
                damage += int(self.level * 0.5)
        protag_hp -= damage
        print("You took " + str(damage) + " damage.")

# Spawn enemies
def spawn_enemy(level):
    if level == "easy":
        level = random.randint(1,5)
        health = int(level * 5)
        magic = int(level * 2)
        enemy = {"level": level,"health": health,"magic": magic}
        return enemy
    elif level == "medium":
        level = random.randint(6, 10)
        health = int(level * 5)
        magic = int(level * 2)
        enemy = {"level": level, "health": health, "magic": magic}
        return enemy
    elif level == "hard":
        level = random.randint(11, 15)
        health = int(level * 5)
        magic = int(level * 2)
        enemy = {"level": level, "health": health, "magic": magic}
        return enemy

# Start
enemy_minion = enemy(**spawn_enemy("easy"))
print("The enemy's level is " + str(enemy_minion.level) + ".")
Rachel = Player(1,100,10)
Rachel.default_attack(enemy_minion.health)
enemy_minion.default_attack(Rachel.health)
print(Rachel.health)
Rachel.heal()
print(Rachel.health)

在这里,当您将health属性传递给敌人的默认攻击函数时,您不是传递该变量,而是传递其值。因此,当您在函数中编辑它时,它不会影响变量

要解决此问题,请传入character类,而不是传入health:

敌方仆从。默认攻击(雷切尔) 并在攻击函数中编辑角色的健康状况:

def default_攻击(自身,玩家):
“”“伤害主角目标的正常攻击。”“”
...
player.health-=伤害#这是找零
...
你也应该对玩家的攻击功能做同样的操作:

def默认_攻击(自身、敌人):
“”“伤害敌人目标的正常攻击。”“”
...
敌人的生命-=伤害
打印(“敌人拿走了”+str(伤害)+“伤害”。)

protag\u hp-=damage
覆盖本地名称
protag\u hp
,而不更改基础值。您可能会更幸运地将
return-protag\u hp
放在函数的末尾,然后将其称为
Rachel.health=busy\u minion.default\u attack(Rachel.health)
,因为此时返回的值将被分配给
Rachel.health
protag\u hp
不是任何类型的“类属性”。它只是一个数值,碰巧来自一个属性,但与类绝对没有持续的联系。一个选项是将
Rachel
传递给方法,而不是
Rachel.health
——假设此参数名为
protag
,则可以执行
protag.health-=damage
以获得所需的结果。
The enemy's level is 3.
The enemy took 2 damage.
You took 7 damage.
100                   # here I want it to say 93
Recovered 10 HP.
100