"""
人类:
属性:
姓名
血量
手里的枪
方法:
装子弹
装单夹
取枪
子弹类
属性
杀伤力
方法
伤害敌人
弹夹类
属性
容量
剩余子弹
方法
添加子弹
消耗子弹
枪类
属性
弹夹(默认没有,需要安装)
方法
连接弹夹
射击子弹
"""
class Ren:
def __init__(self, name):
self.name = name
self.xue = 100
self.qiang = None
def naqiang(self, qiang):
if self.qiang == None:
self.qiang = qiang
else:
print("you qiang le")
def anzidan(self, zidan, danjia):
danjia.baocunzidan(zidan)
def andanjia(self, qiang, danjia):
qiang.lianjiedanjia(danjia)
def kaiqiang(self, diren):
self.qiang.she(diren)
def diaoxue(self, shanghaili):
self.xue -= shanghaili
def __str__(self):
return "%s的剩余血量为:%d" % (self.name, self.xue)
class ZiDan:
def __init__(self, shanghaili):
self.shanghaili = shanghaili
def shanghai(self, diren):
diren.diaoxue(self.shanghaili)
class Qiang:
def __init__(self):
self.danjia = None
def lianjiedanjia(self, danjia):
if self.danjia == None:
self.danjia = danjia
def she(self, diren):
zidan = self.danjia.chuzidan()
if zidan:
zidan.shanghai(diren)
else:
print("No ZiDan")
class Danjia:
def __init__(self, rongliang):
self.rongliang = rongliang
self.ronglianglist = []
def baocunzidan(self, zidan):
if len(self.ronglianglist) < self.rongliang:
self.ronglianglist.append(zidan)
def chuzidan(self):
if len(self.ronglianglist) > 0:
zidan = self.ronglianglist.pop()
return zidan
else:
return None
def __str__(self):
return "剩余子弹数量:%d/%d" % (len(self.ronglianglist), self.rongliang)
# create Mr.Wang
laoWang = Ren("Mr.Wang")
# create danjia
danjia = Danjia(20)
# While to tianjia zidan
i = 0
while i < 15:
zidan = ZiDan(5)
laoWang.anzidan(zidan, danjia)
i += 1
print(danjia)
# create qiang
qiang = Qiang()
laoWang.andanjia(qiang, danjia)
# create diren
diren = Ren("敌人")
# 老王拿起枪
laoWang.naqiang(qiang)
# Mr.Wang she diren
laoWang.kaiqiang(diren)
print(diren)
print(danjia)
python小项目--》老王开枪
最新推荐文章于 2024-09-09 11:36:16 发布