python射击游戏大作业_Python 简单小程序 射击游戏_8_开枪

class Person(object):

'''创建Persion 类,并传入人名'''

def __init__(self,name):

self.name=name

self.gun="" #保存枪的引用

self.hp=100 # 保存血量

def __str__(self):

if self.gun:

return "%s的血量为%d,他的枪信息为:%s"%(self.name,self.hp,self.gun)

else:

if self.hp>0:

return "%s的血量为%d,他还没有枪"%(self.name,self.hp)

else:

return "已挂......"

def save_dan_jia(self,dan_jia_temp,zi_dan_temp): # 射手拿起子弹和弹夹,

dan_jia_temp.save_zi_dan(zi_dan_temp) # 弹夹要容纳子弹

def intert_dan_jia_into_gun(self,dan_jia_temp,gun_temp):

gun_temp.contain_dan_jia(dan_jia_temp)

def get_gun(self,gun):

self.gun=gun

def kou_ban_ji(self,guai_wu): # 写扣扳机方法,

self.gun.fire(guai_wu) # 扣扳机后,能引发枪射击

def diao_xue(self,execution):

self.hp-=execution

class Gun(object):

'''创建Gun 类,并传入枪名'''

def __init__(self,name):

self.name=name

self.dan_jia=""

def __str__(self):

if self.dan_jia:

return "%s弹夹的信息为:%s"%(self.name,self.dan_jia)

else:

return "%s弹夹为空,请创建弹夹对象弹夹的%"(self.name)

def contain_dan_jia(self,dan_jia_temp):

self.dan_jia=dan_jia_temp

def fire(self,guai_wu):

pop_zi_dan=self.dan_jia.tan_chu_zi_dan(guai_wu) #枪射击会引发 弹夹弹出子弹,并保存返回的子弹

if pop_zi_dan:

pop_zi_dan.shanghai_guai_wu(guai_wu) #触发伤害方法

class Clip(object):

'''创建弹夹 类,并传入能装多少子弹'''

def __init__(self,max_capacity):

self.max_capacity=max_capacity

self.saved_zi_dan=[] #保存装入子弹的引用

def __str__(self):

return "弹夹一共有%d/%d 子弹"%(len(self.saved_zi_dan),self.max_capacity)

def save_zi_dan(self,zi_dan_temp): # 两个对象无法实现包含关系,只能用引用指向关系

self.saved_zi_dan.append(zi_dan_temp) # 使用append方法 装入到引用list

def tan_chu_zi_dan(self,guai_wu):

if self.saved_zi_dan:

return self.saved_zi_dan.pop()

else:

print( "大兄弟没子弹了就不要再打了,请加子弹吧" +"弹夹一共有%d/%d 子弹"%(len(self.saved_zi_dan),self.max_capacity))

class Bullet(object):

'''创建子弹类,并设置杀伤力'''

def __init__(self,execution):

self.execution=execution

def shanghai_guai_wu(self,guai_wu):

guai_wu.diao_xue(self.execution)

def main():

#1.创建猎人对象 ,创建猎人对象要有 Person 类。

shooter=Person("Wujing")

#2.枪

k24=Gun("k24")

#3.拿枪是不能打敌人, 枪要有弹夹对象,

dan_jia=Clip(15)

#4.弹夹里要有子弹对象

#创建一些子弹用for循环

for i in range(15):

zi_dan=Bullet(10)

# 5.子弹放在弹夹

shooter.save_dan_jia(dan_jia,zi_dan) # 射手把子弹安装到弹夹中 实现这个方法

#6.弹夹放入枪内

shooter.intert_dan_jia_into_gun(dan_jia,k24)

# test ,弹夹 和 子弹

print(dan_jia)

print(k24)

#7.猎人拿枪

shooter.get_gun(k24)

print(shooter)

#8.怪物

guai_wu=Person("xi_xue_gui")

print(guai_wu)

# 9.猎人扣扳机射击

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

for i in range(15):

zi_dan=Bullet(10)

# 5.子弹放在弹夹

shooter.save_dan_jia(dan_jia,zi_dan) # 射手把子弹安装到弹夹中 实现这个方法

shooter.kou_ban_ji(guai_wu)

print(guai_wu)

print(shooter)

if __name__=='__main__':

main()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值