如何更新Python类实例的值

如何更新Python类实例的值,python,python-3.x,class,oop,Python,Python 3.x,Class,Oop,我正在做一个有趣的文字游戏,但找不到一个类似蟒蛇的方法来更新玩家的数据 我尝试了一些变量,比如h=100玩家=职业(h,d,a,w)(健康,防御,攻击,体重),但是为了改变玩家的健康状况,我必须这样做:h-=敌人攻击(ea)玩家=职业(h,d,a,w)。这是很长的一段时间,后来当我加上防御和武器时,刹车。变量可以工作,但当我开始用它们做很多数学运算时,它们非常长而且不稳定,使它更有趣。我也研究了类方法,但找不到一种方法来向玩家传递一些属性,以便更新 以运行方式导入随机 类客户端: 定义初始(自我

我正在做一个有趣的文字游戏,但找不到一个类似蟒蛇的方法来更新玩家的数据

我尝试了一些变量,比如h=100玩家=职业(h,d,a,w)(健康,防御,攻击,体重),但是为了改变玩家的健康状况,我必须这样做:h-=敌人攻击(ea)玩家=职业(h,d,a,w)。这是很长的一段时间,后来当我加上防御和武器时,刹车。变量可以工作,但当我开始用它们做很多数学运算时,它们非常长而且不稳定,使它更有趣。我也研究了类方法,但找不到一种方法来向玩家传递一些属性,以便更新

以运行方式导入随机
类客户端:
定义初始(自我、健康、防御、攻击、体重):
自我健康
自卫
自我攻击
自重
NPC类:
定义初始(自我、健康、防御、攻击):
自我健康
自卫
自我攻击
#客户清单
投资=[]
#用于创建循环的一组true或false语句
活着=真
敌人活着=错误
活动时==True:
消息=输入()
播放器=客户端(100,0,0,0)
敌人=NPC(100,0,20)
#随时制动环路的一种方法
如果消息==“q”:
活动=错误
#现在我们需要一种选择wepon的方法
如果len(inv)==0:
如果消息==“捡拾弓”:
打印(“弓现在在您的库存中。”)
投资附加(“船首”)
#这是我最近一次尝试将玩家的攻击设置为弓的伤害
player.attack+=25
战斗=真实
如果len(inv)>0且战斗==真:
如果敌人活着==假:
打印(““”敌人正在向你发起攻击!
马力:100“
#既然玩家有弓,我们就开始战斗
敌人活着=真的
如果在inv中为“弓形”:
#这就是我对自己说的,如果我的攻击能直接影响生命值,那么当战斗循环被打破时,球员的统计数据将与他们保持一致,这样数学就更容易确定了
如果minotaur.defense==0:
如果消息==“射击”:
hit=ran.randint(0,9)
如果命中率=9:
打印(“头像”)
敌方生命=0
如果命中率=5:
打印(“点击!”)
敌方健康=敌方健康-玩家攻击
打印(“敌人生命:+str(敌人生命))
如果敌方防御>0:
#我这样做是为了看看它是否有效,但如上所述,没有正确的保存
player.attack=player.attack/minotar.defence
如果消息==“射击”:
hit=ran.randint(0,9)
如果命中率=9:
打印(“头像”)
敌方生命=0
如果点击4:
打印(“点击!”)
敌方健康=敌方健康-玩家攻击
打印(“敌人生命:+str(敌人生命))

如果敌方.health这通常与OO设计有关,我建议您使用角色基类(虽然不是必需的),并让客户端和NPC继承该基类,然后使用攻击和接收攻击方法

class Character():
    def __init__(self, health, defense, damage, weight):
        self.health = health
        self.defense = defense
        self.damage = damage
        self.weight = weight

    def attack(self,target):
        target.receive_attack(self.damage)

    def receive_attack(self,damage):
        self.health -= damage

class Client(Character):
    pass

class NPC(Character):
    pass

class NPC_harder_to_take_down(Character):
    def receive_attack(self,damage):
        self.health -= (damage/2)


me = Client(100,100,100,100)
other = NPC(10,10,10,10)
print(me.health)
>>>100
other.attack(me)
print(me.health)
>>>90

你可以考虑游戏中的每个对象处理它自己的行为:例如,<代码>播放器对象可以有一个<代码> STATS 对象,当某些事件发生时,该对象会被更新。

当它与一种类型的<代码>播放器 >结合时,可以考虑子类修改行为;使其更难杀死,例如更快的恢复,等等

也许是这样的:

class Player:
    def __init__(self, name, stats):
        self.name = name
        self.stats = stats

    def get_hit(self, hit_value):
        self.stats.update_health(-hit_value)

    def rest(self):
        self.stats.update_health(10)

    def __str__(self):
        return f'Player {self.name}: {self.stats}'


class FastRecoveryPlayer(Player):
    def rest(self):
        self.stats.update_health(20)


class Stats:
    """maintains the Player's stats up to date
    """
    def __init__(self, health=100, defense=100, attack=100):
        self.health = health
        self.defense = defense
        self.attack = attack

    def update_health(self, value):
        self.health += value
        self.health = max(0, self.health)
        self.health = min(100, self.health)

    def __str__(self):
        return f'health: {self.health}, defense: {self.defense}, attack: {self.attack}'


hero = Player('Python King', Stats())
print(hero)

print('the Hero gets hit hard')
hero.get_hit(30)
print(hero)
print('the Hero rests a little bit')
hero.rest()
print(hero)

print()
hero2 = FastRecoveryPlayer('Python Emperor', Stats())
print(hero2)

print('the Hero2 gets hit hard')
hero2.get_hit(30)
print(hero2)
print('the Hero2 rests a little bit')
hero2.rest()
print(hero2)
输出:
这有很多问题,但我的第一个问题是,它在任何时候都会更新吗,就像刚开始时一样?还是不?谢谢你!我尝试了一下使用这些类的方法,并使其发挥作用。我现在可以自由地让统计数据保持最新而不被破坏。你应该去@PythonKing多深的抽象兔子洞?IDK,但这里有一篇有趣的文章:
Player Python King: health: 100, defense: 100, attack: 100
the Hero gets hit hard
Player Python King: health: 70, defense: 100, attack: 100
the Hero rests a little bit
Player Python King: health: 80, defense: 100, attack: 100

Player Python Emperor: health: 100, defense: 100, attack: 100
the Hero2 gets hit hard
Player Python Emperor: health: 70, defense: 100, attack: 100
the Hero2 rests a little bit
Player Python Emperor: health: 90, defense: 100, attack: 100