人狗大战2.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))
class Hunter(Person):
    role = 'hunter'
    def dagou(self,dog):
        if random.randint(0,2)==0:
            dog.smz -= 2*self.gjl
            print('%s冲上去,打了%s,产生了暴击,造成%d伤害,%s剩余生命值%d' % (self.name, dog.name, 2*self.gjl, dog.name, dog.smz))
        else:
            dog.smz -= self.gjl
            print('%s冲上去,打了%s,造成%d伤害,%s剩余生命值%d' % (self.name, dog.name, self.gjl, dog.name, dog.smz))
class Magician(Person):
    role = 'magician'
    def FlameStrike(self,d):
        print('%s发动了烈焰风暴,对所有敌人造成5点伤害' % self.name)
        for i in d:
            i.smz -= 5
            print('%s剩余血量%d'%(i.name,i.smz))
class Jinmao(Dog):
    role = 'jinmao'
    def jiaxue(self,d):
        a=d[0]
        for i in range(len(d)):
            if d[i].smz < a.smz:
                a=d[i]
        a.smz += 5
        if a.name == self.name:
            print('%s给%s加了5点生命值,%s的生命值增加到%d'%(self.name,'自己',a.name,a.smz))
        else:
            print('%s给%s加了5点生命值,%s的生命值增加到%d'%(self.name,a.name,a.name,a.smz))

第二部分,编写人狗大战程序

from tests import *

p=[]
d=[]
xm = Person('小明',10,30)
p.append(xm)
xh = Magician('小红',1,20)
p.append(xh)
lz = Hunter('老张',15,50)
p.append(lz)
wc = Dog('旺财',11,40)
d.append(wc)
za = Dog('藏獒',18,80)
d.append(za)
jm = Jinmao('金毛',1,50)
d.append(jm)
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 j in range(len(p)):
            if d ==[]:
                flag_d=0
                break
            else:
                if p[j].role == 'magician':
                    p[j].FlameStrike(d)
                for h in d:
                    if h.smz<=0:
                        print('%s挂了'%h.name)
                        d.remove(h)
                print('%s攻击回合,请选择攻击对象:'%p[j].name)
                for i in range(len(d)):
                    print(i,d[i].name+'  ',end='')
                a=int(input(':'))
                while a>(len(d)-1):
                    a=int(input('不存在的敌人,请重新选择:'))
                print('你选择了%d'%a)
                p[j].dagou(d[a])
                if d[a].smz <=0:
                    print('%s挂了'%d[a].name)
                    d.remove(d[a])
                time.sleep(1)
    if p == []:
        flag_p = 0
        break
    elif d == []:
        flag_d = 0
        break
    else:
        for i in range(len(d)):
            if d[i].role == 'jinmao':
                d[i].jiaxue(d)
            b=random.randint(0,len(p)-1)
            d[i].yaoren(p[b])
            if p[b].smz<=0:
                print('%s挂了'%p[b].name)
                p.remove(p[b])
            time.sleep(1)
if flag_d==0:
    print('游戏结束,人胜利')
else:
    print('游戏结束,狗胜利')

 

posted @ 2021-11-29 11:21  大器晚成的渣男  阅读(152346)  评论(0编辑  收藏  举报