[Python] 纯文本查看 复制代码 class Actor:
def __init__(self, name, xl, atknum, defs, team):
self.name = name
self.hp = xl
self.atknum = atknum
self.defs = defs
self.team = team
# 另外一个actor
def atk(self, other_actor):
# 检测是否为同一阵营
if self.team == other_actor.team:
print(f"{other_actor.name} 是自己人,再打我告你嗷!!")
else:
other_actor.xl = other_actor.hurt - self.atknum * (1 - other_actor.defs)
# 攻击后打印 xx攻击xx 造成多少伤害,还有多少血量
print(f"{self.name} 攻击 {other_actor.name}造成了 {other_actor.hurt:.2f} 点伤害。")
print(f"{other_actor.name} 还有 {other_actor.xl:.2f} 点血量。")
# 设计一个英雄类(Hero),继承Actor,阵营固定为1 super继承
class Hero(Actor):
def __init__(self, name, xl, atk, defs, team=1):
super().__init__(name, xl, atk, defs, team)
# 设计一个怪物类(Monster),继承Actor,阵营固定为2
class Monster(Actor):
def __init__(self, name, xl, atk, defs, team=2):
super().__init__(name, xl, atk, defs, team)
# 创建英雄对象和怪物对象
hero = Hero("帝皇侠", 100, 20, 0.1)
monster = Monster("阿里嘎多美羊羊桑", 200, 15, 0.2)
# 英雄对怪物进行一次或多次攻击
hero.atk(monster)
monster.atk(hero)
hero.atk(monster)
monster.atk(hero)
hero.atk(monster)
monster.atk(hero) |