1 __author__="Stephen Yuan"2 Import Time3 4 classPerson (object):5 """man's class"""6 def __init__(self, name):7Super (person, self).__init__()8Self.name = Name#record a person's name9Self.gun = None#The default person is no gun.TenSELF.HP = 100#the default person's blood volume is One A defAnzhuang_zidan (self, Dan_jia_temp, zi_dan_temp): - """put the bullets in the magazine .""" - Dan_jia_temp.baocun_zidan (zi_dan_temp) the - defAnzhuang_danjia (self, AK47, Dan_jia): - """put the magazine in the gun .""" - Ak47.baocun_danjia (Dan_jia) + - defNa_qiang (Self, gun_temp): + """pick up a gun .""" ASelf.gun =gun_temp at - def __str__(self): - ifSelf.gun: - return "amount of blood in%s:%d,%s, he shot, boom! "%(Self.name, SELF.HP, Self.gun) - Else: - ifSELF.HP = = 100: in return "The amount of%s,%d, he didn't have a gun."%(Self.name, SELF.HP) - elifSELF.HP >0: to return "%s of blood,%d, he has no gun, he is being attacked by gunfire, be careful! He got a shot ..."%(Self.name, SELF.HP) + Else: - return "%s is dead ..."%Self.name the * defKai_qiang (self, Diren, zi_dan_temp, dan_jia_temp): $ """hit the enemy"""Panax NotoginsengDIREN.HP-= Zi_dan_temp.sha_shang_li#Blood loss -Dan_jia_temp.zidan_list.pop ()#Bullet Reduction the + classGun (object): A """Gun Class""" the def __init__(self, name): +Super (Gun, self).__init__() -Self.name = Name#type of recording gun $Self.danjia = None#There's no mag in the default gun. $ - defBaocun_danjia (Self, dan_jia_temp): - """gun, Save the mag .""" theSelf.danjia =dan_jia_temp - Wuyi def __str__(self): the ifSelf.danjia: - return "Gun Info:%s,%s"%(Self.name, Self.danjia) Wu Else: - return "Gun Info:%s, this gun doesn't have a mag ."%Self.name About $ classDanjia (object): - """Magazine Class""" - def __init__(self, max_num): -Super (Danjia, self).__init__() ASelf.max_num =max_num#maximum size of the magazine +Self.zidan_list = []#Save the number of bullets with a list the - defBaocun_zidan (Self, zi_dan_temp): $ """Magazine, save Bullets .""" the self.zidan_list.append (zi_dan_temp) the the def __str__(self): the return "Clip information:%d/%d"%(Len (self.zidan_list), Self.max_num) - in classZidan (object): the """Bullet Class""" the def __init__(self, sha_shang_li): AboutSuper (Zidan, self).__init__() theSelf.sha_shang_li = Sha_shang_li#the bullet's lethality. the the defMain (): + """the process used to control the entire program""" - #Create an old king theLao_wang = Person ('Lao Wang')Bayi the #Create a gun theAK47 = Gun ("AK47") - - #Create a magazine theDan_jia = Danjia (30) the the #Create Bullets theZi_dan = Zidan (10) - the forIinchRange (25): the #Lao Wang put the Bullets in the magazine . the Lao_wang.anzhuang_zidan (Dan_jia, Zi_dan)94 the #Lao Wang put the magazine in the gun the Lao_wang.anzhuang_danjia (AK47, Dan_jia) the 98 #test the clip information About #print (Dan_jia) - 101 #the test gun information102 #print (AK47)103 104 #Lao Wang takes a gun the Lao_wang.na_qiang (AK47)106 107 #test the old king object108 #print (Lao_wang)109 the #Create an enemy111Gebi_laowang = Person ("The old king next door") the 113 forIinchRange (10): the #Lao Wang takes a gun and hits the next door, old song . the Lao_wang.kai_qiang (Gebi_laowang, Zi_dan, Dan_jia) the Print(Lao_wang)117 Print(Gebi_laowang)118Time.sleep (2)119 Print("") - 121 if __name__=='__main__':122Main ()
Execution Result:
Lao Wang's Blood Volume: 100, Gun information: AK47, magazine information: 24/30He's shooting, boom! The blood volume of the Old king next door,90, he has no gun, he is being attacked by gunfire, be careful! He got a shot ... Lao Wang's blood Volume:100, gun information: AK47, magazine information: 23/30He's shooting, boom! The blood volume of the Old king next door,80, he has no gun, he is being attacked by gunfire, be careful! He got a shot ... Lao Wang's blood Volume:100, gun information: AK47, magazine information: 22/30He's shooting, boom! The blood volume of the Old king next door,70, he has no gun, he is being attacked by gunfire, be careful! He got a shot ... Lao Wang's blood Volume:100, gun information: AK47, magazine information: 21/30He's shooting, boom! The blood volume of the Old king next door,60, he has no gun, he is being attacked by gunfire, be careful! He got a shot ... Lao Wang's blood Volume:100, gun information: AK47, magazine information: 20/30He's shooting, boom! The blood volume of the Old king next door,50, he has no gun, he is being attacked by gunfire, be careful! He got a shot ... Lao Wang's blood Volume:100, gun information: AK47, magazine information: 19/30He's shooting, boom! The blood volume of the Old king next door,40, he has no gun, he is being attacked by gunfire, be careful! He got a shot ... Lao Wang's blood Volume:100, gun information: AK47, magazine information: 18/30He's shooting, boom! The blood volume of the Old king next door,30, he has no gun, he is being attacked by gunfire, be careful! He got a shot ... Lao Wang's blood Volume:100, gun information: AK47, magazine information: 17/30He's shooting, boom! The blood volume of the Old king next door,20, he has no gun, he is being attacked by gunfire, be careful! He got a shot ... Lao Wang's blood Volume:100, gun information: AK47, magazine information: 16/30He's shooting, boom! The blood volume of the Old king next door,10, he has no gun, he is being attacked by gunfire, be careful! He got a shot ... Lao Wang's blood Volume:100, gun information: AK47, magazine information: 15/30He's shooting, boom! The Old King is dead next door ...
python-Old King shot