import random
# 创建人类,来实现共同的属性( name , life )和功能(返回生命值和对死亡生命的显示处理)
class Person: def __init__ (self , name): self .name = name self .life = 100
def __str__ (self ): if self .life <= 0 : self .life = 0
print ("%s 已经死亡 " % self .name) return "%s 现在生命值是 %d" % (self .name, self .life)
# 定义恐怖分子类,来实现对反恐精英的伤害
class Terrorist(Person): def fire (self , h): # 随机产生对反恐精英的伤害值
damage = random.randint(1 , 100 ) # 恐怖分子命中率的波动,并对反恐精英的伤害进行分层展示
if 10 <= damage <= 20 : h.life -= damage print ("%s 向 %s 开枪,造成 %d 点伤害 " % (self .name, h.name, damage)) else : print ("%s 没有打中 ……" % self .name) if h.life == 100 : print ("%s 没有受到任何伤害,继续战斗! " % h.name)
elif 70 <= h.life <= 99 : print ("%s 受到轻伤,还可以继续战斗! " % h.name)
elif 1 <= h.life <= 69 : print ("%s 受伤严重,还在顽强战斗! " % h.name)
elif h.life <= 0 : print ("%s 因为受伤严重,失血过多,壮烈牺牲! " % h.name)
class Police(Person): def fire (self , h): # 随机产生对恐怖分子的伤害值
damage = random.randint(1 , 50 ) # 反恐精英命中率的波动
if 20 <= damage <= 30 : h.life -= damage print ("%s 向 %s 开枪,造成 %d 点伤害 " % (self .name, h.name, damage)) else : print ("%s 的子弹被躲过,没有打中 ……" % self .name)
def main (): # 创建对应的对象 h = Police(" 【反恐精英】 " ) t1 = Terrorist(" 【恐怖分子 1 】 " ) t2 = Terrorist(" 【恐怖分子 2 】 " ) t3 = Terrorist(" 【恐怖分子 3 】 " )
while True : # 随机对对方开枪
r = random.randint(1 , 6 ) # 对生命值进行判断,并进行相对的分析操作 if r == 1 : if t1.life <= 0 : continue
else : t1.fire(h) print (h) elif r == 2 : if t2.life <= 0 : continue
else : t2.fire(h) print (h) elif r == 3 : if t3.life <= 0 : continue
else : t3.fire(h) print (h) elif r == 4 : if t1.life <= 0 : h.fire(t1) continue
else : h.fire(t1) print (t1) elif r == 5 : if t2.life <= 0 : continue
else : h.fire(t2) print (t2) elif r == 6 : if t3.life <= 0 : continue
else : h.fire(t3) print (t3) # 判断战争是否结束
if t1.life <= 0 and t2.life <= 0 and t3.life == 0 : print (t1) print (t2) print (t3) print (" 坏人全部死亡,战斗结束 " ) break
if h.life <= 0 : print ("%s 死亡,战斗结束 " % h.name) break
main()
|