人狗大战1.0


import time
import random
class Person:
role = 'person'
def __init__(self,name,gjl,smz):
self.name = name
self.gjl = gjl
self.smz = smz
def run(self):
print('%s冲了上去'%self.name)
def dagou(self,dog):
dog.smz -=self.gjl
print('%s冲上去,打了%s,造成%d伤害,%s剩余生命值%d'%(self.name,dog.name,self.gjl,dog.name,dog.smz))
class Dog:
role='dog'
def __init__(self,name,gjl,smz):
self.name = name
self.gjl = gjl
self.smz = smz
def yaoren(self,person):
person.smz -=self.gjl
print('%s冲上去,咬了%s,造成%d伤害,%s剩余生命值%d' % (self.name, person.name, self.gjl, person.name, person.smz))
xm = Person('小明',10,30)
xh = Person('小红',8,20)
lz = Person('老张',15,50)
wc = Dog('旺财',11,40)
za = Dog('藏獒',18,80)
p=[]
d=[]
p.append(xm)
p.append(xh)
p.append(lz)
d.append(wc)
d.append(za)

flag_p=1
flag_d=1

while flag_p and flag_d:
if p==[]:
flag_p=0
break
elif d==[]:
flag_d=0
break
else:
for i in p:
a = random.randint(0, (len(d) - 1))
i.dagou(d[a])
if d == []:
flag_d=0
break
for j in d:
if j.smz<=0:
print("%s挂了" % j.name)
d.remove(j)
time.sleep(1)

if p == []:
flag_p = 0
break
elif d == []:
flag_d = 0
break
else:
for i in d:
a = random.randint(0, (len(p) - 1))
i.yaoren(p[a])
if p == []:
flag_p = 0
break
for j in p:
if j.smz <= 0:
print("%s挂了" % j.name)
p.remove(j)
time.sleep(1)

if flag_d==0:
print('游戏结束,人胜利')
else:
print('游戏结束,狗胜利')
posted @ 2021-11-25 13:16  大器晚成的渣男  阅读(1900)  评论(0编辑  收藏  举报