Python---old king shot

Source: Internet
Author: User

class Person (object):
"" "Human" ""
def __init__ (self,name):
Super (Person,self). __init__ ()
Self.name = Name
Self.gun = None #用来保存枪的引用
SELF.HP = 100

def __str__ (self):
If Self.gun:
Return "%s of Blood:%d, he has gun%s"% (Self.name,self.hp,self.gun)
Else
If self.hp>0:
Return "%s Blood Volume:%d, he has no gun"% (Self.name, SELF.HP)
Else
Return '%s ' is hung!!! "% (Self.name)


def Kou_ban_ji (Self,diren):
"" "Let the gun fire bullets to hit the enemy" "
Self.gun.fire (Diren)

def diao_xue (self, Sha_shang_li):
"" "The blood is lost according to the corresponding lethality" "
SELF.HP-=sha_shang_li

def Anzhuang_zidan (self,dan_jia_temp,zi_dan_temp):
"" "Load the Bullets into the magazine."
Dan_jia_temp.baocun_zidan (zi_dan_temp)

def Anzhuang_danjia (self,gun_temp,dan_jia_temp):
"" "Install the magazine into the Gun" "
Gun_temp.baocun_danjia (dan_jia_temp)

def Naqiang (self,gun_temp):
"" "Pick up the action of the Gun" "
Self.gun =gun_temp



Class Gun (object):
"" "Gun Class" ""
def __init__ (self,name):
Super (Gun,self). __init__ ()
Self.name = Name #用来记录枪的类型
Self.danjia = None
def Baocun_danjia (self,dan_jia_temp):
Self.danjia =dan_jia_temp

def __str__ (self):
Return "Gun info:%s"% (Self.name)

def fire (Self,diren):
"" "The gun gets the bullet from the magazine" "
Zidan_temp = Self.danjia.tanchu_zidan ()
If zidan_temp:
Zidan_temp.dazhong (Diren)
Else
Print ("No bullets in the magazine")


Class Danjia (object):
"" "Magazine" ""
def __init__ (Self,max_num):
Super (Danjia,self). __init__ ()
Self.max_num = Max_num #用来记录弹夹的最大容量
Self.zidan_list = [] #用来记录所有的子弹的引用

def Baocun_zidan (self,zi_dan_temp):
"" "Save the Bullets" "
Self.zidan_list.append (zi_dan_temp)

def __str__ (self,):
Return "Clip information:%d/%d"% (Len (self.zidan_list), Self.max_num)

def Tanchu_zidan (self):
If self.zidan_list:
Return Self.zidan_list.pop ()
Else
Return None


Class Zidan (object):
"" "Bullets" ""
def __init__ (Self,sha_shang_li):
Super (Zidan,self). __init__ ()
Self.sha_shang_li = Sha_shang_li #用来记录子弹的威力

def dazhong (Self,diren):
""""""
Diren.diao_xue (Self.sha_shang_li)


def main ():
"" "the process used to control the entire program" "
# 1. Create an old King object
Laowang = Person ("Lao Wang")
# 2. Create a Gun object
AK47 = Gun ("Ak47")
# 3. Create a Magazine object
Dan_jan = Danjia (20)
# 4. Create some bullets
For I in range (15):
Zi_dan = Zidan (10)
# 5. Lao Wang put the bullets in the magazine
Laowang.anzhuang_zidan (Dan_jan, Zi_dan)

# 6. Lao Wang put the magazine in the gun
Laowang.anzhuang_danjia (Ak47,dan_jan)

#测试
Print (Dan_jan)
Print (AK47)

# 7. Lao Wang takes a gun
Laowang.naqiang (AK47)
Print (Laowang)
# 8. Create an Enemy
Geibi_laosong = person ("Li Gang")
Print (Geibi_laosong)
# 9. Lao Wang shoots the enemy
Laowang.kou_ban_ji (Geibi_laosong)
Print (Geibi_laosong)
Print (Laowang)

Laowang.kou_ban_ji (Geibi_laosong)
Print (Geibi_laosong)
Print (Laowang)

If __name__== ' __main__ ':
Main ()


Operation Result:

Python---old king shot

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.