练习1 _ 类和对象

"""
aggressivity  攻击力
armor         护甲
damage_value  伤害值
"""
class Riven:
    camp = 'Noxus'

    def __init__(self, nickname,
                 aggressivity=200,
                 life_value=600,
                 money=1000,
                 armor=10):
        self.nickname = nickname
        self.aggressivity = aggressivity
        self.life_value = life_value
        self.money = money
        self.armor = armor

    def attack(self, enemy):
        if enemy.life_value <= 0:
            return True

        if self.life_value:
            damage_value = self.aggressivity - enemy.armor  # 一次攻击伤害值
            enemy.life_value -= damage_value  # 被攻击一次后剩余的生命值
            print(
                f'''
                [{self.nickname}]攻击:[{enemy.nickname}]
                [{enemy.nickname}]掉血:[{damage_value}]
                还剩血量:[{enemy.life_value}]            
            ''')
class Garen:
    camp = 'Demacia'

    def __init__(self, nickname,
                 aggressivity=250,
                 life_value=500,
                 money=1100,
                 armor=5):
        self.nickname = nickname
        self.aggressivity = aggressivity
        self.life_value = life_value
        self.money = money
        self.armor = armor

    def attack(self, enemy):
        if enemy.life_value <= 0:
            return True

        if self.life_value:
            damage_value = self.aggressivity - enemy.armor  # 一次攻击伤害值
            enemy.life_value -= damage_value  # 被攻击一次后剩余的生命值
            print(
                f'''
                [{self.nickname}]攻击:[{enemy.nickname}]
                [{enemy.nickname}]掉血:[{damage_value}]
                还剩血量:[{enemy.life_value}]            
            ''')
class BlackCleaver:
    def __init__(self, price=500, aggrev=10, life_value=100):
        self.price = price
        self.aggrev = aggrev
        self.life_value = life_value

    def update(self, obj):
        obj.money -= self.price  # 减钱
        obj.aggressivity += self.aggrev  # 加攻击
        obj.life_value += self.life_value  # 加生命值

    def fire(self, obj):  # 主动攻击技能,喷火,烧死对方
        obj.life_value -= 1000  # 火烧得攻击力定为1000
r1 = Riven('飞灵')
g1 = Garen('盖轮')
b1 = BlackCleaver()

while True:
    flag1 = r1.attack(g1)
    if flag1:
        print(f'{g1.nickname}已挂')
        break

    flag2 = g1.attack(r1)
    if flag2:
        print(f'{r1.nickname}已挂')
        break

扩展:

# print(r1.aggressivity, r1.life_value, r1.money)
# if r1.money > b1.price:
#     r1.b1 = b1
#     b1.update(r1)

# print(r1.aggressivity, r1.life_value, r1.money)
# print(g1.aggressivity, g1.life_value, g1.money)
# r1.attack(g1)  # 普通攻击
# print(g1.life_value)
# r1.b1.fire(g1)  # 用装备攻击
# print(g1.life_value)  # g1的生命值小于等于0就死了
上一篇:Oracle用户密码过期的处理方法


下一篇:python面向对象学习