编程语言
首页 > 编程语言> > python 人物对打 类

python 人物对打 类

作者:互联网

class Person:
    def __init__(self,name,hp,atk):
        self.name=name
        self.hp=hp
        self.atk=atk
        pass
    def __new__(cls, *args, **kwargs):
         #确定是否比赛开始 创建函数
        if input('比赛是否开始yes / no:') == 'yes':
            return object.__new__(cls) #执行2次是应为构造是要input 构造类有input一次
    def __str__(self):
        return "{}当前血量{}".format(self.name,self.hp)
        pass
    def attk(self,enemy):
        enemy.hp -= self.atk
        print("{}攻击了{}一下 伤害{}".format(self.name,enemy.name,self.atk))
        self.toux()
    def toux(self):
        if self.hp <=20:
            print('我是{}血量太少,我想投降'.format(self.name))
            if random.randint(0,10)==9:
                print('处罚投降概率 当场投降!!!')
                global flag
                flag = False
                return
            print('触发失败!!!')
            pass
        pass
    def recove(self):
        self += self.hp*0.1
    pass
one=Person('奥特曼',1000,12)
two=Person('丁伟亮',1000,12)
# 代码演示区域
import random
flag  =True
n=0
if bool(one):
    while flag:
        if one.hp <= 0 or two.hp <= 0:
            print('游戏结束 {}获胜'.format(two.name) if one.hp <= 0 else '游戏结束 {}获胜'.format(one.name))
            break
        if random.randint(1, 2) == 1:
            print(one)
            one.attk(two)
            print(two)
            pass
        else:
            print(two)
            two.attk(one)
            print(one)
            pass
        n += 1
        print("当前比赛第%d轮" %n)
        pass
else:
    print('比赛失败!!!')
人物对打

 

标签:__,人物,python,self,atk,hp,def,name
来源: https://www.cnblogs.com/inkser/p/15612120.html