当前位置: 移动技术网 > IT编程>脚本编程>Python > 田忌赛马小游戏

田忌赛马小游戏

2020年10月24日  | 移动技术网IT编程  | 我要评论
```pythonimport randomimport time class Role: def __init__(self, name='【角色】'): self.name=name self.life= random.randint(100,150) self.attack=random.randint(30,50)class Knight(Role): def __init__(self,name='【圣光骑士】'): .

``

import random
import time 

class Role:
    def __init__(self, name='【角色】'):
        self.name=name
        self.life= random.randint(100,150)
        self.attack=random.randint(30,50)

class Knight(Role):
    def __init__(self,name='【圣光骑士】'):
        Role.__init__(self,name)
        self.life=self.life*5
        self.attack=self.attack*3

    def fight_buff(self,opponent,str1,str2):
        if opponent.name=='【暗影刺客】':
            self.attack=int(self.attack*1.5)
            print('『%s』【圣光骑士】对 『%s』【暗影刺客】说:“让无尽光芒制裁你的堕落!”'%(str1, str2))

class Assassin(Role):
    def __init__(self,name='【暗影刺客】'):
        Role.__init__(self,name)
        self.life=self.life*3
        self.attack=self.attack*5

    def fight_buff(self,opponent,str1,str2):
        if opponent.name== '【精灵弩手】':
            self.attack=int(self.attack*1.5)
            print('『%s』【暗影刺客】对 『%s』【精灵弩手】说:“主动找死,就别怪我心狠手辣。”'%(str1, str2)) 

class Bowman(Role):
    def __init__(self,name='【精灵弩手】'):
        Role.__init__(self,name)
        self.life=self.life*4
        self.attack=self.attack*4

    def fight_buff(self, opponent,str1,str2):
        if opponent.name == '【圣光骑士】':
            self.attack=int(self.attack*1.4)
            print('『%s』【精灵弩手】对 『%s』【圣光骑士】说:“骑着倔驴又如何?你都碰不到我衣服。”'%(str1, str2))

class Game():
    def __init__(self):
        self.players=[]
        self.enemies=[]
        self.score=0
        self.i=0#统计轮次
        self.game_start()
        self.born_role()
        self.show_role()
        self.order_role()
        self.pk_role()
        self.show_result()
    def game_start(self):
        print('------------ 欢迎来到“炼狱角斗场” ------------')
        print('在昔日的黄昏山脉,奥卢帝国的北境边界上,有传说中的“炼狱角斗场”。')
        print('鲜血与战斗是角斗士的归宿,金钱与荣耀是角斗士的信仰!')
        print('今日,只要你【你的队伍】能取得胜利,你将获得一笔够花500年的财富。')
        time.sleep(2)
        print('将随机生成【你的队伍】和【敌人队伍】!')
        input('\n狭路相逢勇者胜,请按任意键继续。\n')

    def born_role(self):
        for i in range(3):
            self.players.append(random.choice([Knight(),Assassin(),Bowman()]))
            self.enemies.append(random.choice([Knight(),Assassin(),Bowman()]))
    def show_role(self):
        print('----------------- 角色信息 -----------------')
        print('你的队伍:')
        for i in range(3):
            print('『我方』%s 血量:%s  攻击:%s'%(self.players[i].name,self.players[i].life,self.players[i].attack))

        print('敌人队伍:')

        for i in range(3):

            print('『敌方』%s 血量:%s  攻击:%s'%(self.enemies[i].name,self.enemies[i].life,self.enemies[i].attack))
        print('--------------------------------------------')
        input('请按回车键继续。\n')

    def order_role(self):
        order_dict={}
        for i in range (3):
            order=int(input('你想将%s放在第几个上场'%self.players[i].name))
            order_dict[order]=self.players[i]
        self.players=[]
        for i in range(1,4):
            self.players.append(order_dict[i])

        print('\n你的队伍出场顺序是:%s, %s, %s'
            %(self.players[0].name,self.players[1].name,self.players[2].name))
        print('敌人队伍出场顺序是:%s、%s、%s'
        %(self.enemies[0].name,self.enemies[1].name,self.enemies[2].name))

    def pk_role(self):
        for i in range(3):
            print('\n----------------- 【第%s轮】 -----------------' % (i+1))
            # 每一局开战前加buff
            self.players[i].fight_buff(self.enemies[i],'我方','敌方')
            self.enemies[i].fight_buff(self.players[i],'敌方','我方')
            input('\n战斗双方准备完毕,请按回车键继续。')
            print('--------------------------------------------')

            while self.players[i].life>0 and self.enemies[i].life>0:
                self.enemies[i].life-=self.players[i].attack
                self.players[i].life-=self.enemies[i].attack

                print('我方%s 发起了攻击,敌方%s 剩余血量 %s'%
                (self.players[i].name,self.enemies[i].name,self.enemies[i].life))
                print('敌方%s 发起了攻击,我方%s 剩余血量 %s'%
                (self.enemies[i].name,self.players[i].name,self.players[i].life))
                print('--------------------------------------------')
                time.sleep(1)

            if self.players[i].life <= 0 and self.enemies[i].life> 0:
                    print('\n很遗憾,我方%s 挂掉了!'%(self.players[i].name))
                    self.score -= 1

            elif self.players[i].life >0 and self.enemies[i].life<= 0: 
                    print('\n恭喜,我方%s 活下来了。'% (self.players[i].name))
                    self.score += 1
            else:
                    print('\n我的天,他们俩都死了啊!')
    def show_result(self):
            input('\n请按回车查看最终结果。\n')
            if self.score >0:
                print('【最终结果】\n你赢了,最终的财宝都归你了!')
            elif self.score == 0:
                print('【最终结果】\n你没有胜利,但也没有失败,在夜色中灰溜溜离开了奥卢帝国。')
            else:
                print('【最终结果】\n你输了。炼狱角斗场又多了几具枯骨。')

game = Game()
time.sleep(5)


用python实现田忌赛马的小游戏,并在人物设置上设置了相克的选项,增加了趣味性

本文地址:https://blog.csdn.net/D0644727/article/details/109260384

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网