from random import randint # 导入randint函数
class Monster(object):
def __init__(self, name, energy):
self.name = name
self.energy = energy
if self.energy >= 100:
print(self.name + "满血战斗")
else:
print(self.name + "目前有" + str(self.energy) + "的血量")
print("警告:" + self.name + "需要补血")
def eat(self, energy):
if self.energy <= 100:
for i in range(10, 100, 10):
self.energy += energy # 执行吃的语句,吃就+10,如果>100,就不吃了
print("正在补血.....")
print(self.name + "现在有" + str(self.energy) + "的血量")
class Ultraman(object):
def __init__(self, name, alk):
self.name = name
self.alk = alk
return
def beat(self, other, c):
other.energy = other.energy - c * self.alk
if other.energy <= 0:
print(self.name + "攻击" + other.name + str(c) + "次")
print("恭喜" + self.name + "成功打死" + other.name)
else:
print(self.name + "攻击" + other.name + str(c) + "次")
print("很遗憾" + self.name + "没有打死" + other.name)
M1 = Monster('月月', 10)
M1.eat(10)
U1 = Ultraman('水水', 999)
U1.beat(M1, 1)
原文链接:https://blog.csdn.net/qq_53806297/article/details/112345655