1 class Person(object):
2 """人的类"""
3 def __init__(self,name):
4 super(Person,self).__init__()
5 self.name = name
6 self.gun = None # 用来保存枪对象的引用
7 self.hp = 100 # 血量
8
9 def anzhuang_zidan(self,dan_jia_temp,zi_dan_temp):
10 """把子弹装到弹夹中"""
11 dan_jia_temp.bao_cun_zidan(zi_dan_temp)
12
13 def anzhuang_danjia(self,gun_temp,dan_jia_temp):
14 """把弹夹安装到枪中"""
15 gun_temp.bao_cun_danjia(dan_jia_temp)
16
17 def naqiang(self,gun_temp):
18 """拿起一把枪"""
19 self.gun = gun_temp
20
21 def __str__(self):
22 if self.gun:
23 return "%s的血量为:%d,他有枪:%s"%(self.name,self.hp,self.gun)
24 else:
25 if self.hp>0:
26 return "%s的血量为:%d,他没有枪"%(self.name,self.hp)
27 else:
28 return "%s 已挂!"%self.name
29
30 def kou_ban_ji(self,diren):
31 """让枪发射子弹去打敌人"""
32 self.gun.fire(diren)
33
34 def diao_xue(self,sha_shang_li):
35 """根据杀伤力,掉相应血量"""
36 self.hp -= sha_shang_li
37
38
39
40
41
42
43 class Gun(object):
44 """枪的类"""
45 def __init__(self,name):
46 super(Gun,self).__init__()
47 self.name = name # 用来记录枪的类型
48 self.danjia = None # 用来记录弹夹对象的引用
49
50
51 def bao_cun_danjia(self,dan_jia_temp):
52 """用一个属性保存弹夹的引用"""
53 self.danjia = dan_jia_temp
54
55 def __str__(self):
56 if self.danjia:
57 return "枪的信息为:%s, 弹夹信息:%s"%(self.name, self.danjia)
58 else:
59 return "枪的信息为:%s,这把枪中没有弹夹"%(self.name)
60
61 def fire(self,diren):
62 """枪中弹夹中获取一发子弹,然后让这发子弹去击中敌人"""
63 #先取弹夹中取子弹
64 zidan_temp = self.danjia.tanchu_zidan()
65 #让这个子弹去伤害敌人
66 if zidan_temp:
67 #子弹.打中敌人(敌人)
68 zidan_temp.dazhong(diren)
69 else:
70 print("弹夹中没有子弹啦!")
71
72
73 class Danjia(object):
74 """枪的类"""
75 def __init__(self,max_num):
76 super(Danjia,self).__init__()
77 self.max_num = max_num # 用来记录枪夹得容量
78 self.zidan_list = [] # 用来记录所有子弹的引用
79
80 def bao_cun_zidan(self,zi_dan_temp):
81 """将这个子弹保存在弹夹"""
82 self.zidan_list.append(zi_dan_temp)
83
84 def __str__(self):
85 return "弹夹的信息为:%d/%d"%(len(self.zidan_list),self.max_num)
86
87 def tanchu_zidan(self):
88 """弹出最上面的那颗子弹"""
89 if self.zidan_list:
90 return self.zidan_list.pop()
91 else:
92 return None
93
94
95 class Zi_dan(object):
96 """枪的类"""
97 def __init__(self,sha_shang_li):
98 super(Zi_dan,self).__init__()
99 self.sha_shang_li = sha_shang_li # sha_shang_li子弹威力
100
101 def dazhong(self,diren):
102 """让敌人掉血"""
103 #敌人.掉血(一个子弹的威力)
104 diren.diao_xue(self.sha_shang_li)
105
106
107
108 def main():
109 """用来控制这个程序流程"""
110
111 # 1创建一个老赵对象
112 laozhao = Person("老赵")
113
114
115 # 2创建一个枪对象
116 ak47 = Gun("AK47")
117
118 # 3创建一个弹夹对象
119 dan_jia = Danjia(20)
120
121 # 4创建一些子弹
122 for i in range(15):
123 zi_dan = Zi_dan(10)
124
125 # 5老赵把子弹安装在弹夹中
126 # laozhao安装子弹到弹夹中(弹夹,子弹)
127 laozhao.anzhuang_zidan(dan_jia,zi_dan)
128
129
130 # 6老赵把弹夹安装到抢中
131 # 老赵.安装弹夹到枪中
132 laozhao.anzhuang_danjia(ak47, dan_jia)
133
134 """
135 # test:测试弹夹信息
136 print(dan_jia)
137 # test:枪测试
138 print(ak47)
139 """
140
141 # 7老赵拿枪
142 laozhao.naqiang(ak47)
143
144 # 测试老王拿枪
145 print(laozhao)
146
147
148 # 8创建一个敌人
149 gebi_laosong = Person("隔壁老宋")
150 print(gebi_laosong)
151
152 # 9老赵开枪打敌人
153 # 老王.扣扳机(隔壁老宋)
154 laozhao.kou_ban_ji(gebi_laosong)
155 print(gebi_laosong)
156 print(laozhao)
157
158 #老赵向老宋连续开15枪
159 for i in range(15):
160 laozhao.kou_ban_ji(gebi_laosong)
161 print(gebi_laosong)
162 print(laozhao)
163
164
165
166 if __name__ == '__main__':
167 main()
Python 简单的枪击游戏
点赞
收藏