Tiny_Lu
不忘初心

day 19 作业

作业

什么是对象

对象就是有一定特征和技能的结合体

什么是类

类就是所有相似的对象特征和技能的结合体

绑定方法的特点

对象的绑定方法是由对象来调用的,特点是在调用时会自动传入对象本身,不同的对象调用该绑定方法时,会将不同的对象放入到绑定方法中

对战小游戏

class Hero:
    def __init__(self, name, health, magic, attk):
        self.name = name
        self.health = health
        self.magic = magic
        self.attk = attk

    def attack(self, monster):
        monster.health -= self.attk

        print(f'{self.name}攻击了{monster.name},'
              f'{monster.name}当前生命值:{monster.health}')
        if monster.health <= 0:
            return True


class Monster:
    def __init__(self, name, health, magic, attk):
        self.name = name
        self.health = health
        self.magic = magic
        self.attk = attk

    def attack(self, hero):
        hero.health -= self.attk

        print(f'{self.name}攻击了{hero.name},'
              f'{hero.name}当前生命值:{hero.health}')
        if hero.health <= 0:
            return True


hero1 = Hero('Tiny', 1000, 200, 100)
monster1 = Monster('goblin', 800, 100, 50)



while True:

    flag1 = hero1.attack(monster1)
    if flag1:
        break


    flag2 = monster1.attack(hero1)
    if flag2:
        break
posted @ 2019-10-09 18:19  二二二二白、  阅读(103)  评论(0编辑  收藏  举报