1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 屎山代码实现奥特曼打小怪兽

屎山代码实现奥特曼打小怪兽

时间:2018-07-18 03:13:24

相关推荐

屎山代码实现奥特曼打小怪兽

修改器处需注意无返回值,并使用self._hp接收,与访问器不同

from random import randintclass fight(object):__slots__ = ('_name','_hp')def __init__(self,name,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 if hp > 0 else 0 # 此处需要注意是用self._hp接收,而非直接returndef attack(self,other):other.hp -= randint(1,15)class man(fight):__slots__ = ('_name','_hp','_mp')def __init__(self,name,hp,mp):super().__init__(name,hp)self._mp = mp@propertydef mp(self):return self._mp@mp.setterdef mp(self,mp):self._mp = mp if mp > 0 else 0def big_attack(self,other):self._mp -= 20if other.hp > 50:other.hp -= 50else:other.hp *= 1//4class Monster(fight):__slots__ = ('_name','_hp')def exist(monsters):for monster in monsters:if monster.hp > 0:return monsterreturn monsters[0]def existnum(monsters):num = 0for monster in monsters:if monster.hp > 0:num += 1return numdef main():superman = man('雷欧',300,100)m1 = Monster('1号',100)m2 = Monster('2号',100)m3 = Monster('3号',100)m = [m1,m2,m3]live = exist(m)session = 1while live.hp != 0 and superman.hp != 0:print(f'---------第{session}局开始-------------')if randint(1,10) < 8:superman.attack(live)print(f'{superman.name}使用普通攻击殴打了{live.name}')print(f'-----{live.name}的生命值为{live.hp}')else:if superman.mp >= 20:superman.big_attack(live)print(f'{superman.name}使用大招殴打了{live.name}')print(f'-----{live.name}的生命值为{live.hp}')num = existnum(m)live = exist(m)t = m.copy()while num != 0:live.attack(superman)num -= 1print(f'{live.name}使用普通攻击击打了{superman.name}')print(f'-----{superman.name}的生命值为{superman.hp}')t.remove(live)if t:live = exist(t)else:num = 0live = exist(m)recover(superman)session += 1print('game over')def recover(_man):if _man.mp < 100:_man.mp += 5if __name__ == '__main__':main()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。