from abc import ABCMeta, abstractmethod
from random import randint, randrange
import timeclass Fighter(object, metaclass=ABCMeta):"""战斗者"""# 通过__slots__魔法限定对象可以绑定的成员变量__slots__ = ('_name', '_hp')def __init__(self, name, hp):"""构造器:param name: 名字:param hp: 生命值"""self._name = nameself._hp = hp@propertydef name(self):return self._name@propertydef hp(self):return self._hp@hp.setterdef hp(self, hp):self._hp = hp@propertydef alive(self):return self._hp > 0@abstractmethoddef attack(self, other):"""攻击:param other:被攻击的对象:return:"""passclass Ultraman(Fighter):"""奥特曼"""__slots__ = ('_name', '_hp', '_mp')def __init__(self, name, hp, mp):"""构造器:param name: 名字:param hp: 生命值:param mp: 魔法值"""super().__init__(name, hp)self._mp = mpdef attack(self, other):other.hp -= randint(15, 25)def huge_attack(self, other):"""究极必杀技(打掉对方至少50滴血或者四分之三):param other: 被攻击对象:return: 使用必杀技成功返回True否则返回False"""if self._mp >= 50:self._mp -= 50injury = other.hp * 3 // 4if injury >= 50:injury = injuryelse:injury = 50other.hp -= injuryreturn Trueelse:self.attack(other)return Falsedef magic_attack(self, others):"""魔法攻击:param others: 被攻击的群体:return: 使用魔法成功返回True 不成功False"""if self._mp >= 20:self._mp -= 20for temp in others:if temp.alive:temp.hp -= randint(10, 15)return Trueelse:return Falsedef resume(self):"""恢复魔法值"""incr_point = randint(1, 10)self._mp += incr_pointreturn incr_pointdef __str__(self):return '~~~%s奥特曼~~~\n' % self._name + \'生命值:%d\n' % self._hp + \'魔法值:%d\n' % self._mpclass Monster(Fighter):"""小怪兽"""__slots__ = ('_name', '_hp')def attack(self, other):other.hp -= randint(10, 20)def __str__(self):return '~~~%s小怪兽~~~\n' % self._name + \'生命值:%d\n' % self._hpdef is_any_alive(monsters):"""判断有没有小怪兽是活着的"""for monster in monsters:if monster.alive > 0:return Truereturn Falsedef select_alive_one(monsters):"""选中一只或者的怪兽"""monsters_len = len(monsters)while True:index = randrange(monsters_len)monster = monsters[index]if monster.alive > 0:return monsterdef display_info(ultraman, monsters):"""显示奥特曼和小怪兽的信息"""print(ultraman)for monster in monsters:print(monster, end=' ')def main():u = Ultraman('她与断剑', 1000, 120)m1 = Monster('哥斯拉', 250)m2 = Monster('大白鲨', 500)m3 = Monster('苍井空', 750)ms = [m1, m2, m3]fight_round = 1while u.alive and is_any_alive(ms):print('========第%02d回合========' % fight_round)m = select_alive_one(ms) # 选中一只小怪兽skill = randint(1, 10) # 通过随机数选择使用哪种攻击方法if skill < 6: # 60%概率是普攻print('%s使用普通攻击打了%s的伤害' % (u.name, m.name))u.attack(m)print('%s的魔法值回复了%d点' % (u.name, u.resume()))elif skill <= 9: # 30%概率使用了魔法攻击,魔法值不足还会失败if u.magic_attack(ms):print('%s使用了魔法攻击' % u.name)else:print('%s使用魔法失败' % u.name)else: # 10%的概率使用大招 魔法不足放不出来if u.huge_attack(m):print('%s使用大招虐了%s' % (u.name, m.name))else:print('%s普攻了%s' % (u.name, m.name))u.attack(m)print('%s的魔法值回复了%d点' % (u.name, u.resume()))if m.alive > 0:print('%s回击了%s' % (m.name, u.name))m.attack(u)display_info(u, ms) # 每个回合结束后显示奥特曼和小怪兽的信息fight_round += 1time.sleep(3)print('\n========战斗结束!========\n')if u.alive > 0:print('%s奥特曼胜利' % u.name)else:print('小怪兽胜利')if __name__ == '__main__':main()
粘贴进去就能跑,拿走不谢!