# 类
# 有四个对象 人,弹夹,枪,子弹
# 人会调用其它三个对象
# 弹夹会调用子弹对象
# 枪会调用弹夹对象
# 反恐精英
# 人射击会调用枪,枪会调用子弹对人造成伤害
class Person:#人对象def __init__(self, name):self.name = nameself.blood = 100def hand_take_gun(self, gun):#拿枪,还没用到self.gun = gundef fire(self, enermy1):gun.fire_enermy(enermy1)print("射击成功,敌方剩余血量:{}".format(enermy1.blood))def install_bull(self, clip, bullet): # 装子弹方法clip.save_bullets(bullet) # 调用弹夹安装子弹方法来实现def install_clip(self, clip, Gun): # 给枪装弹夹Gun.install_clip(clip)def loseBlood(self, bullet):#掉血方法self.blood -= bullet.damage#根据子弹伤害掉血class Clip:def __init__(self, capacity):self.capacity = capacity#最大容量self.current_list = []#子弹列表def save_bullets(self, bullet): # 安装子弹if len(self.current_list) < self.capacity:self.current_list.append(bullet)print("安装子弹成功")print("当前子弹数目{}/{}".format(len(self.current_list), self.capacity))def launch_bullet(self):#弹夹射击出子弹if len(self.current_list) > 0:#子弹列表大于0才有子弹可以射击bullet = self.current_list[-1]self.current_list.remove(bullet)# self.current_list.pop()return bulletelse:print("请装子弹")return Noneclass bullet:#子弹对象def __init__(self, damage):self.damage = damagedef hurt(self, person):#子弹会伤害人,调用人loseblood方法person.loseBlood()class Gun:#枪对象def __init__(self):#枪构造方法,默认最开始没有弹夹self.clip = Nonedef install_clip(self, clip):#给枪装弹夹if not self.clip:self.clip = clipprint("枪已经装上弹夹,目前弹夹容量{}/{}".format(len(clip.current_list), clip.capacity))def fire_enermy(self, person):#枪开火k = self.clip.launch_bullet()#返回值k为子弹对象if k:person.loseBlood(k)else:print("枪没有子弹了,请装弹")if __name__ == '__main__':Solider1 = Person("牛战士")#实例化对象enermy = Person("怪兽")#实例化敌人(都是通过Person对象完成)print(enermy.blood)clip = Clip(30)i = 0while True:#给弹夹装弹bullet1 = bullet(10)Solider1.install_bull(clip, bullet1)if i < 5:i = i + 1else:breakgun = Gun() # 生成一把枪,实例化# print(clip.current_list )Solider1.install_clip(clip, gun)#给枪装弹夹Solider1.fire(enermy)#开火# Solider1.hand_take_gun(gun)