反恐精英

有四个对象 人,弹夹,枪,子弹

人会调用其它三个对象

弹夹会调用子弹对象

枪会调用弹夹对象

反恐精英

人射击会调用枪,枪会调用子弹对人造成伤害

class Person:#人对象
def init(self, name):
self.name = name
self.blood = 100

def hand_take_gun(self, gun):#拿枪,还没用到
    self.gun = gun

def fire(self, enermy1):
    gun.fire_enermy(enermy1)
    print("射击成功,敌方剩余血量:{}".format(enermy1.blood))

def install_bull(self, clip, bullet):  # 装子弹方法
    clip.save_bullets(bullet)  # 调用弹夹安装子弹方法来实现

def install_clip(self, clip, Gun):  # 给枪装弹夹
    Gun.install_clip(clip)

def loseBlood(self, bullet):#掉血方法
    self.blood -= bullet.damage#根据子弹伤害掉血

class Clip:
def init(self, capacity):
self.capacity = capacity#最大容量
self.current_list = []#子弹列表

def save_bullets(self, bullet):  # 安装子弹
    if len(self.current_list) < self.capacity:
        self.current_list.append(bullet)
        print("安装子弹成功")
        print("当前子弹数目{}/{}".format(len(self.current_list), self.capacity))

def launch_bullet(self):#弹夹射击出子弹
    if len(self.current_list) > 0:#子弹列表大于0才有子弹可以射击
        bullet = self.current_list[-1]
        self.current_list.remove(bullet)
        # self.current_list.pop()
        return bullet
    else:
        print("请装子弹")
        return None

class bullet:#子弹对象
def init(self, damage):
self.damage = damage

def hurt(self, person):#子弹会伤害人,调用人loseblood方法
    person.loseBlood()

class Gun:#枪对象
def init(self):#枪构造方法,默认最开始没有弹夹
self.clip = None

def install_clip(self, clip):#给枪装弹夹
    if not self.clip:
        self.clip = clip
    print("枪已经装上弹夹,目前弹夹容量{}/{}".format(len(clip.current_list), clip.capacity))

def fire_enermy(self, person):#枪开火
    k = self.clip.launch_bullet()#返回值k为子弹对象
    if k:
        person.loseBlood(k)
    else:
        print("枪没有子弹了,请装弹")

if name == ‘main’:
Solider1 = Person(“牛战士”)#实例化对象
enermy = Person(“怪兽”)#实例化敌人(都是通过Person对象完成)
print(enermy.blood)
clip = Clip(30)
i = 0
while True:#给弹夹装弹
bullet1 = bullet(10)
Solider1.install_bull(clip, bullet1)
if i < 5:
i = i + 1
else:
break
gun = Gun() # 生成一把枪,实例化
# print(clip.current_list )
Solider1.install_clip(clip, gun)#给枪装弹夹
Solider1.fire(enermy)#开火
# Solider1.hand_take_gun(gun)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值