繁体   English   中英

如何在一个方法中增加一个值(num)并减少在Python类中定义的另一个值?

[英]How do I increase a value (num) in a method and decrease another defined in a class in Python?

我正在用python做游戏,我需要创建一种方法来增加/减少我在类中定义的值。 该方法收到一个数字,我必须修改另一个数字。 这是我的代码:

class Ant:
    def __init__(self, name, steps, health, alcohol, state):
        self.name = name
        self.steps = []
        self.health = 100
        self.alcohol = 0
        self.state = state

我需要修改健康状况。请帮助我

使用单个参数定义方法并修改self.health

class Ant:
    def __init__(self, name, steps, health, alcohol, state):
        self.name = name
        self.steps = []
        self.health = 100
        self.alcohol = 0
        self.state = state

    def add_health(self, value):
        self.health += value

    # bonus
    def drink(self, value):
        self.alcohol += value
        self.health -= value

您可以创建一些从Ant的健康中减去的功能。 例如:

class Ant:
    def __init__(self, name, steps, health, alcohol, state):
        self.name = name
        self.steps = []
        self.health = 100
        self.alcohol = 0
        self.state = state

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM