[英]How do I increase a value (num) in a method and decrease another defined in a class in Python?
I am making a game in python and I need to create a method to increase/decrease a value that I define in a class. 我正在用python做游戏,我需要创建一种方法来增加/减少我在类中定义的值。 The method receives a number and with that i have to modify the other.
该方法收到一个数字,我必须修改另一个数字。 This is my code:
这是我的代码:
class Ant:
def __init__(self, name, steps, health, alcohol, state):
self.name = name
self.steps = []
self.health = 100
self.alcohol = 0
self.state = state
I need to modify the health.. Help me please 我需要修改健康状况。请帮助我
Define a method with a single argument and modify the self.health
: 使用单个参数定义方法并修改
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
You can make some function that subtracts from the Ant
s health. 您可以创建一些从
Ant
的健康中减去的功能。 For example: 例如:
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.