廢話不多說
上代碼
#CS反恐精英1.0
#1、定義戰(zhàn)士和敵人的類
classPerson:
"""人的屬性"""
def__init__(self,name):
"""姓名"""
self.name=name
"""血量"""
self.blood=100
"""人的***"""
"""給彈夾安裝子彈"""
definstall_bullet(self,clip,bullet):
"""彈夾放置子彈"""
clip.save_bullets(bullet)
"""給搶安裝彈夾"""
definstall_clip(self,gun,clip):
gun.mounting_clip(clip)
"""持槍"""
deftake_gun(self,gun):
self.gun=gun
"""開槍"""
deffire(self,enemy):
"""射擊敵人"""
self.gun.shoot(enemy)
def__str__(self):
returnself.name+"剩余血量為:"+str(self.blood)
"""掉血"""
deflose_blood(self,damage):
self.blood-=damage
"""定義表示彈夾的類"""
classClip:
def__init__(self,capacity):
"""最大容量"""
self.capacity=capacity
"""當(dāng)前容量"""
self.current_list=[]
"""安裝子彈"""
defsave_bulllets(self,bullet):
"""當(dāng)前子彈數(shù)量小于最大容量"""
iflen(self.current_list)<self.capacity:
self.current_list.append(bullet)
"""構(gòu)造一個函數(shù),返回現(xiàn)在的彈夾數(shù)量"""
def__str__(self):
return"彈夾當(dāng)前的子彈數(shù)量為:"+str(len(self.current_list))+"/"+str(self.capacity)
"""出子彈"""
deflaunch_bullet(self):
iflen(self.current_list)>0:
bullent=self.current_list[-1]
self.current_list.pop()
returnbullet
else:
returnNone
"""定義表示子彈的類"""
classBullet:
def__init__(self,damage):
"""傷害力"""
self.damage=damage
"""傷害敵人"""
defhurt(self,enemy):
"""讓敵人掉血"""
enemy.lose_blood(self.damage)
"""定義搶的類"""
classGun:
def__init__(self):
"""默認(rèn)沒有彈夾"""
self.clip=None
def__str__(self):
ifself.clip:
return"槍當(dāng)前有彈夾"
else:
return"槍沒有彈夾"
"""鏈接彈夾"""
defmounting_clip(self,clip):
ifnotself.clip:
self.clip=clip
"""射擊"""
defshoot(self,enemy):
bullet=self.launch_bullet()
"""射擊未擊中"""
ifbullet:
bullet.hurt(enemy)
else:
print('沒有子彈了,放了空槍。。。。')
"""創(chuàng)建一個戰(zhàn)士"""
soldier=Person("老王")
"""創(chuàng)建一個敵人"""
enemy=Person('敵人')
"""創(chuàng)建一個槍"""
gun=Gun()
print(enemy)
"""士兵拿槍"""
soldier.take_gun(gun)
"""士兵開槍"""
soldier.fire(enemy)
"""創(chuàng)建一個彈夾"""
clip=Clip(20)
"""創(chuàng)建一個子彈"""
bullet=Bullet(5)
"""戰(zhàn)士安裝子彈到彈夾"""
soldier.install_bullet(clip,bullet)
soldier.install_bullet(gun,clip)
"""輸出當(dāng)前彈夾中子彈的數(shù)量"""
print(clip)
print(gun)
print(clip)
print(enemy)
soldier.install_clip(gun,clip)
print(clip)
print(enemy)