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)