标签:logs shang 对象 range color tar lis temp 方法
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 #安装子弹的方法,里面的参数有弹夹和子弹
10 def anzhuang_zidan(self, dan_jia_temp, zi_dan_temp):
11 #弹夹保存子弹
12 dan_jia_temp.baocun_zidan(zi_dan_temp)
13
14 #把弹夹安装到枪里面
15 def anzhuang_danjia(self, gun_temp, dan_jia_temp):
16 #保存弹夹
17 gun_temp.baocun_danjia(dan_jia_temp)
18
19 def naqiang(self, gun_temp):
20 """拿起一把枪"""
21 self.gun = gun_temp
22
23 def __str__(self):
24 if self.gun:
25 return "%s的血量为:%d, 他有枪 %s"%(self.name, self.hp, self.gun)
26 else:
27 if self.hp>0:
28 return "%s的血量为%d, 他没有枪"%(self.name, self.hp)
29 else:
30 return "%s 已挂...."%self.name
31
32 def kou_ban_ji(self, diren):
33 """让枪发射子弹去打敌人"""
34 #枪.开火(敌人)
35
36 self.gun.fire(diren)
37
38 def xiao_xue(self, sha_shang_li):
39 """根据杀伤力,掉相应的血量"""
40 self.hp -= sha_shang_li
41
42 class Gun(object):
43 """枪类"""
44 def __init__(self, name):
45 super(Gun, self).__init__()
46 self.name = name#用来记录枪的类型
47 self.danjia = None#用来记录弹夹对象的引用
48
49 def baocun_danjia(self, dan_jia_temp):
50 """用一个属性来保存这个弹夹对象的引用"""
51 self.danjia = dan_jia_temp
52
53 def __str__(self):
54 if self.danjia:
55 return "枪的信息为:%s, %s"%(self.name, self.danjia)
56 else:
57 return "枪的信息为:%s,这把枪中没有弹夹"%(self.name)
58
59 def fire(self, diren):
60 """枪从弹夹中获取一发子弹,然后让这发子弹去击中敌人"""
61
62 #先从弹夹中取子弹
63 #弹夹.弹出一发子弹()
64 zidan_temp = self.danjia.tanchu_zidan()
65
66 #让这个子弹去伤害敌人
67 if zidan_temp:
68 #子弹.打中敌人(敌人)
69 zidan_temp.dazhong(diren)
70 else:
71 print("弹夹中没有子弹了。。。。")
72
73
74
75 class Danjia(object):
76 """弹夹类"""
77 def __init__(self, max_num):
78 super(Danjia, self).__init__()
79 self.max_num = max_num#用来记录弹夹的最大容量
80 self.zidan_list = []#用来记录所有的子弹的引用
81
82 def baocun_zidan(self, zi_dan_temp):
83 """将这颗子弹保存"""
84 self.zidan_list.append(zi_dan_temp)
85
86 def __str__(self):
87 return "弹夹的信息为:%d/%d"%(len(self.zidan_list), self.max_num)
88
89 def tanchu_zidan(self):
90 """弹出最上面的那颗子弹"""
91 if self.zidan_list:
92 return self.zidan_list.pop()
93 else:
94 return None
95
96 class Zidan(object):
97 """子弹类"""
98 def __init__(self, sha_shang_li):
99 super(Zidan, self).__init__()
100 self.sha_shang_li = sha_shang_li#这颗子弹的威力
101
102 def dazhong(self, diren):
103 """让敌人掉血"""
104
105 #敌人.掉血(一颗子弹的威力)
106 diren.xiao_xue(self.sha_shang_li)
107
108
109 def main():
110 """用来控制整个程序的流程"""
111
112 #1. 创建老王对象
113 laowang = Person("老王")
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 = Zidan(10)
124
125 #5. 老王把子弹安装到弹夹中
126 #老王.安装子弹到弹夹中(弹夹,子弹)
127 laowang.anzhuang_zidan(dan_jia, zi_dan)
128
129 #6. 老王把弹夹安装到枪中
130 #老王.安装弹夹到枪中(枪,弹夹)
131 laowang.anzhuang_danjia(ak47, dan_jia)
132
133 #test:测试弹夹的信息
134 #print(dan_jia)
135
136 #test:测试枪的信息
137 #print(ak47)
138
139 #7. 老王拿枪
140 #老王.拿枪(枪)
141 laowang.naqiang(ak47)
142
143 #test:测试老王对象
144 print(laowang)
145
146 #8. 创建一个敌人
147 gebi_laosong = Person("隔壁老宋")
148 print(gebi_laosong)
149
150 #9. 老王开枪打敌人
151 #老王.扣扳机(隔壁老宋)
152 laowang.kou_ban_ji(gebi_laosong)
153 print(gebi_laosong)
154 print(laowang)
155 laowang.kou_ban_ji(gebi_laosong)
156 print(gebi_laosong)
157 print(laowang)
158 laowang.kou_ban_ji(gebi_laosong)
159 print(gebi_laosong)
160 print(laowang)
161 laowang.kou_ban_ji(gebi_laosong)
162 print(gebi_laosong)
163 print(laowang)
164 laowang.kou_ban_ji(gebi_laosong)
165 print(gebi_laosong)
166 print(laowang)
167 laowang.kou_ban_ji(gebi_laosong)
168 print(gebi_laosong)
169 print(laowang)
170 laowang.kou_ban_ji(gebi_laosong)
171 print(gebi_laosong)
172 print(laowang)
173 laowang.kou_ban_ji(gebi_laosong)
174 print(gebi_laosong)
175 print(laowang)
176 laowang.kou_ban_ji(gebi_laosong)
177 print(gebi_laosong)
178 print(laowang)
179 laowang.kou_ban_ji(gebi_laosong)
180 print(gebi_laosong)
181 print(laowang)
182 laowang.kou_ban_ji(gebi_laosong)
183 print(gebi_laosong)
184 print(laowang)
185
186 if __name__ == ‘__main__‘:
187 main()
标签:logs shang 对象 range color tar lis temp 方法
原文地址:http://www.cnblogs.com/suiyisuixing/p/7630187.html