Python面向对象练习——奥特曼大战小怪兽

一,知识点

二,代码(奥特曼大战小怪兽)

from abc import ABCMeta, abstractmethod
from random import randint, randrange


class Fighter(metaclass=ABCMeta):
    """战斗着"""
    __slots__ = ('_name', '_hp')

    def __init__(self, name, 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

    @abstractmethod
    def attack(self, other):
        """攻击
        :param other:被攻击对象
        """
        pass


class Ultraman(Fighter):
    """奥特曼"""
    __slots__ = ('_name', '_hp', '_mp')

    def __init__(self, name, hp, mp):
        super().__init__(name, hp)
        self._mp = mp

    def attack(self, other):
        other.hp -= randint(15, 25)

    def huge_attack(self, other):
        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 magic_attack(self, others):
        if self._mp >= 20:
            self._mp -= 20
            for temp in others:
                if temp.alive > 0:
                    temp.hp -= randint(10, 15)
                    return True
        else:
            return False

    def resume(self):
        """回复魔法值"""
        incr_point = randint(1, 10)
        self._mp += incr_point
        return incr_point

    def __str__(self):
        return '---%s奥特曼---\n生命值:%d\n魔法值: %d\n' % (self._name, self._hp, self._mp)


class Monster(Fighter):
    """小怪兽"""
    __slots__ = ('_name', '_hp')

    def attack(self, other):
        other.hp -= randint(10, 20)

    def __str__(self):
        return '---%s小怪兽---\n生命值:%d\n' % (self._name, self._hp)


def is_any_alive(monsters):
    """判断有没有小怪兽是活着的"""
    for monster in monsters:
        if monster.alive > 0:
            return True
        else:
            return False


def select_alive_one(monsters):
    """选中一只活着的小怪兽"""
    monsters_len = len(monsters)
    while True:
        index = randrange(monsters_len)
        monster = monsters[index]
        if monster.alive > 0:
            return monster


def display_info(ultreman, monsters):
    """显示小怪兽和奥特曼的信息"""
    print(ultreman)
    for monster in monsters:
        print(monster, end='')


def main():
    u = Ultraman('迪迦奥塔曼', 1000, 120)
    m1 = Monster('哥斯拉', 250)
    m2 = Monster('贝利亚', 400)
    m3 = Monster('杰顿', 300)
    ms = [m1, m2, m3]
    fight_round = 1
    while u.alive > 0 and is_any_alive(ms):
        print("===========第%02d回合==========" % fight_round)
        m = select_alive_one(ms)  # 选中一只怪兽
        skill = randint(1, 10)  # 通过随机数选择使用哪种技能
        if skill <= 6:  # %60的概率普通攻击
            print('%s 使用了普通攻击打了 %s' % (u.name, m.name))
            u.attack(m)
        elif skill <= 9:  # 30%的几率使用魔法攻击,可能因为魔法不足而失败
            if u.magic_attack(ms):
                print('%s 使用了魔法攻击' % u.name)
            else:
                print('%s 使用了魔法失败' % u.name)
        else:  # 10%的几率使用终极必杀
            if u.huge_attack(m):
                print('%s 使用终极必杀技虐了 %s' % (u.name, m.name))
            else:
                print('%s 使用了普通攻击打死了 %s' % (u.name, m.name))
                print('%s 的魔法值恢复了 %d' % (u.name, u.resume()))
        if m.alive:  # 如果选中小怪兽没死就回击奥特曼
            print('%s 回击了 %s' % (m.name, u.name))
            m.attack(u)
        display_info(u, ms)
        fight_round += 1
    print('\n=======战斗结束=======\n')
    if u.alive:
        print('%s 奥特曼胜利!' % u.name)
    else:
        print('小怪兽胜利!')


if __name__ == '__main__':
    main()

三,结语

道友们可以加我的微信公众号 梦码城 ,接下来的日子里我会在公众号上发布各种与编程方面的总结学习知识,也会送各位小伙伴大量的学习资源,梦码城,期待你的加入
文章不易,小伙伴们点个赞加个关注鼓励下梦码呗

猜你喜欢

转载自blog.csdn.net/qq_45724216/article/details/106672466