|
from abc import ABCMeta, abstractclassmethod
import random
import time
class Fighter(object, metaclass=ABCMeta):
__slots__ = ('_name', '_hp')
def __init__(self, name, hp):
'''初始化方法
:param name:名字
:param hp: 生命值
'''
self._name = name
self._hp = hp
@property
def name(self):
return self._name
@property
def hp(self):
return self._hp
@hp.setter
def hp(self, hp):
self._hp = hp if hp >= 0 else 0
@property
def alive(self):
return self._hp > 0
@abstractclassmethod
def attack(self, other):
'''
攻击
:param other:被攻击的对象
:return:
'''
pass
class Ultraman(Fighter):
'''奥特曼'''
__slots__ = ('_name', '_hp', '_mp')
def __init__(self, name, hp, mp):
super().__init__(name, hp)
self._mp = mp
def reduce(self):
re_blood = random.randint(15, 25)
return re_blood
def magic_attack_reduce(self):
re_blood = random.randint(10, 15)
return re_blood
def attack(self, other):
other.hp -= self.reduce()
def huge_attack(self, other):
'''
打掉对方至少50点或四分之三的血
:param other: 被攻击的对象
:return: 使用成功返回True,否则返回False
'''
if self._mp >= 50:
self._mp -= 50
injury = other.hp * 3 // 4
injury = injury if injury >= 50 else 50
other.hp -= injury
return True
else:
self.attack(other)
return False
def huge_attack_reduce(self, other):
huge_reduce = other.hp * 3 // 4
huge_reduce = huge_reduce if huge_reduce >= 50 else 50
return huge_reduce
def magic_attack(self, others):
'''
魔法攻击
:param others:被攻击的群体
:return: 使用魔法攻击成功后返回True否则返回False
'''
if self._mp >= 20:
self._mp -= 20
for temp in others:
if temp.alive:
temp.hp -= self.magic_attack_reduce()
return True
else:
return False
def resume(self):
'''回复魔法值'''
incr_point = random.randint(1, 10)
self._mp += incr_point
return incr_point
def __str__(self):
time.sleep(1)
return '~~~%s奥特曼~~~\n' % self._name + \
'生命值:%d\n' % self._hp + \
'魔法值:%d\n' % self._mp
class Monster(Fighter):
__slots__ = ('_name', '_hp')
def reduce_blood(self):
re_blood = random.randint(10, 20)
return re_blood
def attack(self, other):
other.hp -= self.reduce_blood()
def __str__(self):
return '~~~%s小怪兽~~~' % self._name + \
'生命值:%d' % self._hp
def is_any_alive(monsters):
for monster in monsters:
if monster.alive > 0:
return True
return False
def select_alive_one(monsters):
monsters_len = len(monsters)
while True:
index = random.randrange(monsters_len)
monster = monsters[index]
if monster.alive > 0:
return monster
def display_info(ultraman, monsters):
print(ultraman)
for monster in monsters:
print(monster, end=' \n' )
time.sleep(0.3)
def main():
u = Ultraman('泰罗', 1000, 120)
m1 = Monster('狄仁杰', 250)
m2 = Monster('李元芳', 500)
m3 = Monster('傻逼', 750)
ms = [m1, m2, m3]
while u.alive and is_any_alive(ms):
m = select_alive_one(ms)
skill = 10
if:
time.sleep(1)
if u.huge_attack(m):
print('%s使用究极必杀技虐了%s,造成了%d伤害' % (u.name, m.name, u.huge_attack_reduce()))
else:
print('%s使用了普通攻击打了%s,造成了%d伤害' % (u.name, m.name, u.reduce()))
print('%s的魔法值回复了%d点.' % (u.name, u.resume()))
if m.alive > 0:
time.sleep(1)
print('%s回击了%s.造成%d点伤害' % (m.name, u.name, m.reduce_blood()))
m.attack(u)
display_info(u, ms)
fight_round += 1
time.sleep(1)
print('\n=======战斗结束!=======\n')
if u.alive > 0:
print('%s奥特曼胜利!' % u.name)
else:
print('小怪兽胜利!')
if __name__ == '__main__':
main()
这是我学习来,并自己改进的一些地方,实现了对普通攻击和魔法伤害的数值的print,但是必杀技就不能print了,raise这个“TypeError: huge_attack_reduce() missing 1 required positional argument: 'other'”求解答
|
|