Python面向对象练习
class Enty:
blood=100
name=""
atackvalue=100
team=0
domain=['1'] #1,land 2 air
def setTeam(self,team=0):
self.team=team
def atack(self,Enty):
if self.team==Enty.team:
print("不能向盟军开火")
self.info()
pass
else :
# print(self.domain)
s=set(self.domain)
es=set(Enty.domain)
if s&es :
print(f'{self.name}/{self.blood}/{self.atackvalue}攻击{Enty.name}/{Enty.blood}/{Enty.atackvalue}\n开始攻击')
Enty.blood-=self.atackvalue
print(f'{Enty.name}被掉血{self.atackvalue},当前血量{Enty.blood}')
else:
print("不在火力范围")
def info(self):
print(f'血量{self.blood},名称{self.name}')
class tank(Enty):
def __init__(self,team=0):
self.name="坦克"
self.blood=1000
super().setTeam(team)
pass
class Soldier(Enty):
atackvalue = 20
blood = 200
def __init__(self,team=0):
self.name="大兵"
super().setTeam(team)
pass
class FlySoldier(Soldier):
def __init__(self,team=0):
self.name='飞行兵'
self.domain=['2']
super().setTeam(team)
pass
class DefenseSoldier(Soldier):
def __init__(self,team=0):
self.name="防空兵"
self.domain=['1','2']
super().setTeam(team)
t=tank()
s=Soldier()
f=FlySoldier()
d=DefenseSoldier()
t1=tank(1)
s1=Soldier(1)
f1=FlySoldier(1)
d1=DefenseSoldier(1)
s.atack(f)
t.atack(s)
s.atack(t)
s.atack(f1)
t.atack(f1)
d.atack(f1)