美文网首页
《Python 程序设计》

《Python 程序设计》

作者: 麥丹慈 | 来源:发表于2020-10-07 16:32 被阅读0次

12.7 阶段案例源码参考:

class Bullet(object):
    def __init__(self, damage):
        if(damage < 0):
            raise ValueError(r'Damage should larger or equal to 0.')
        self.__damage = damage

    def hurt(self, character):
        character.lose_blood(self.__damage)


class Clip(object):
    def __init__(self, capacity):
        if capacity <= 0:
            raise Exception(r'Capacity should be an integer larger than 0.')
        self.__current_list = []
        self.__capacity = capacity

    def __repr__(self):
        return r'Bullet(s) in the clip: ' + str(len(self.__current_list)) + r'/' + str(self.__capacity) + r'.'

    def add_bullet(self, bullet):
        if len(self.__current_list) < self.__capacity:
            self.__current_list.append(bullet)
            return True
        print(r'Clip is full.')

    def shoot_bullet(self):
        if len(self.__current_list) > 0:
            return self.__current_list.pop()
        else:
            return


class Gun(object):
    def mount(self, clip):
        self.__clip = clip

    def __init__(self, clip=None):
        self.mount(clip)

    def __repr__(self):
        if self.__clip:
            return r'Clip exists.'
        else:
            return r'Clip offline.'

    def shoot(self, character):
        bullet = self.__clip.shoot_bullet()
        if bullet:
            bullet.hurt(character)
        else:
            print(r'No bullet in the clip any more!')


class Character(object):
    def __handle_clip(self, clip):
        self.__clip = clip

    def take_gun(self, gun):
        self.__gun = gun

    def __init__(self, name, clip=None, gun=None):
        self.__name = name
        self.__handle_clip(clip)
        self.take_gun(gun)
        self.__blood = 100  # 0 ~ 100

    def __repr__(self):
        return r'Character named ' + self.__name + r' remains blood: ' + str(self.__blood) + r'.'

    def lose_blood(self, damage):
        if self.__blood < 0:
            print(r'%s is dead.' % self.__name)
            del self
        if damage < 0:
            raise ValueError(r'Damage should not less than 0.')
        self.__blood -= damage

    def install_bullet(self, bullet, clip=None):
        if clip:
            self.__handle_clip(clip)
        self.__clip.add_bullet(bullet)

    def install_clip(self, gun=None, clip=None):
        if gun:
            self.take_gun(gun)
        if clip:
            self.__handle_clip(clip)
        self.__gun.mount(self.__clip)

    def fire(self, character):
        if self.__gun:
            self.__gun.shoot(character)
        else:
            print('Danger! There is no gun in character %s\'s hand.' % self.name)


class Enemy(Character):
    pass


class Soldier(Character):
    pass


def main():
    soldier = Soldier(r'soldier')

    clip = Clip(20)
    print(clip)
    for i in range(5):
        soldier.install_bullet(Bullet(5), clip)
    print(clip)

    gun = Gun()  # Must with `()`
    print(gun)
    soldier.install_clip(gun, clip)
    print(gun)

    enemy = Enemy(r'enemy')

    for i in range(2):
        soldier.fire(enemy)
        print(clip, enemy)


main()

相关文章

网友评论

      本文标题:《Python 程序设计》

      本文链接:https://www.haomeiwen.com/subject/higfpktx.html