3.20 python web second

122 阅读1分钟
# 士兵和敌人:
#     属性:name,blood,gun
#     方法:安装弹夹,安装子弹,拿枪,开枪
# 子弹类:
#     属性:杀伤力
#     方法:伤害敌人 (掉血)
# 弹夹类:
#     属性:容量,当前保存的子弹数
#     方法:安装子弹,弹出子弹(开枪)
# 枪类:
#     属性:弹夹
#     方法:连接弹夹,射子弹
class person:
    def __init__(self,name):
        self.name=name
        self.blood=100

     def install_bullet(self,clip,bullet):
         clip.savebullets(bullet)

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

    def fire(self,enemy):
        self.gun.shoot(enemy)

class Bullet:
    def __init__(self,damage):
        self.damage=damage
    def hurt(self,enemy):
        enemy.lost_blood(self.damage)

class clip:
    def __init__(self,capacity):
        self.capacity=capacity
        self.current_list=[]

    def save_bullets(self,buttet):
        if len(self.current_list)<self.capacity:
            self.current_list.append(buttet)

    def launch_bullet(self):
        if len(self.current_list)>0:
            bullet=self.current_list[-1]
            self.current_list.pop()
            return bullet
        else:
            return None

    def __str__(self):
        return "弹夹当前的子弹数量:"+str(len(self.current_list)+'/'+str(self.capacity))
class Gun:
    def __init__(self):
        self.clip=None

    def __str__(self):
        if self.clip:
            return "枪当前弹夹"
        else:
            return "枪没有弹夹"

    def mounting_clip(self,clip):
        if not self.clip:
            self.clip=clip

    def shoot(self,enamy):
        bullet=self.clip.launch_bullet()
        if bullet:
            bullet.hurt((enamy))
        else:
            print("没有子弹了,放了空枪....")

if __name__=="__main__":
    gun=Gun()
    print(gun)

    soldier=person("老张")
    clip=clip(20)
    print(clip)
    i=0
    while i<10:
        bullet=Bullet()
        soldier.install_bullet(clip,bullet)
        i+=1
    print(clip)
    gun=Gun()
    print(gun)
    soldier.install_bullet(gun,clip)