Bootstrap

python随机属性小游戏

赛制是三局两胜。

import time
import random

global life
global attack
global shield
global through
class Part:
    def __init__(self,life,attack,shield,through):
        self.life = life
        self.attack = attack
        self.shield = shield
        self.through = through




def my_input1():
    global player_part
    global enemy_part
    while True:
        player_part = input('请选择你的角色')
        for i in part:
            if i == player_part:
                print('选择成功')
                break
        else:
            print('请重新选择')
            continue
        break
    while True:
        enemy_part = random.choice(parts)
        if enemy_part != player_part:
            break
    return player_part,enemy_part
while True:
    player_victory = 0
    enemy_victory = 0
    i = 0
    sum1 = 3
    while True:
        while i < sum1:
            i += 1
            if i <= 3:
                print('  \n——————现在是第 %s 局————————' % i)
            else:
                print('\n——————加赛第 %s 局————————' %(i - 3))
            part = {'龙水': Part(random.randint(100,150),random.randint(30,50),random.randint(0,30),0),
                    '固土': Part(random.randint(140,190),random.randint(30,50),random.randint(0,30),0),
                    '云木': Part(random.randint(100,150),random.randint(30,50),random.randint(10,40),0),
                    '炎火': Part(random.randint(100,150),random.randint(45,65),random.randint(0,30),0),
                    '裂金': Part(random.randint(100,150),random.randint(30,50),random.randint(0,30),random.randint(8,20)),}
            parts = list(part)
            my_input1()
            player_attack = part[player_part].attack
            enemy_attack = part[enemy_part].attack
            if player_part == parts[parts.index(enemy_part)-1]:
                print('本局敌人具有属性压制,敌人基础伤害+10')
                enemy_attack = enemy_attack + 10
            elif enemy_part == parts[parts.index(player_part)-1]:
                print('本局你具有属性压制,你的基础伤害+10')
                player_attack = player_attack + 10
            else:
                print('本局双方毫无关系')
            player_shield = part[player_part].shield
            enemy_shield = part[enemy_part].shield
            player_life = part[player_part].life
            enemy_life = part[enemy_part].life
            player_through = part[player_part].through
            enemy_through = part[enemy_part].through
            
            
            if player_part == '裂金':
                print('【玩家】\n角色:{}\n血量:{}\n攻击:{}\n防御:{}\n穿透:{}'.format(player_part,player_life,player_attack,player_shield,player_through))
                print('-----------------------')
                time.sleep(1)
                print('【敌人】\n角色:{}\n血量:{}\n攻击:{}\n防御:{}'.format(enemy_part,enemy_life,enemy_attack,enemy_shield))
                print('-----------------------')
                time.sleep(1)
            elif enemy_part == '裂金':
                print('【玩家】\n角色:{}\n血量:{}\n攻击:{}\n防御:{}'.format(player_part,player_life,player_attack,player_shield))
                print('-----------------------')
                time.sleep(1)
                print('【敌人】\n角色:{}\n血量:{}\n攻击:{}\n防御:{}\n穿透:{}'.format(enemy_part,enemy_life,enemy_attack,enemy_shield,enemy_through))
                print('-----------------------')
                time.sleep(1)
            else:
                print('【玩家】\n角色:{}\n血量:{}\n攻击:{}\n防御:{}'.format(player_part,player_life,player_attack,player_shield))
                print('-----------------------')
                time.sleep(1)
                print('【敌人】\n角色:{}\n血量:{}\n攻击:{}\n防御:{}'.format(enemy_part,enemy_life,enemy_attack,enemy_shield))
                print('-----------------------')
                time.sleep(1)
            if player_part == '裂金':
                enemy_shield = enemy_shield - player_through
            elif enemy_part =='裂金':
                player_shield = player_shield - enemy_through
            else:
                pass
            while player_life > 0 and enemy_life > 0:
                player_present = float(1) + random.randint(0,10)*0.1
                enemy_present = float(1) + random.randint(0,10)*0.1
                if player_part == '龙水':
                    player_present = 1.3 + random.randint(0,10)*0.1
                elif enemy_part == '龙水':
                    enemy_present = 1.3 + random.randint(0,10)*0.1
                else:
                    pass
                print('本回合你的暴击效果是%f' %(player_present))
                time.sleep(0.5)
                print('本回合敌人的暴击效果是%f' %(enemy_present))
                time.sleep(1)

                if player_shield >= 0 and enemy_shield >= 0:
                    player_life = player_life - enemy_attack * enemy_present + player_shield 
                    enemy_life = enemy_life - player_attack *player_present + enemy_shield
                elif player_shield < 0 and enemy_shield >= 0:
                    player_life = player_life - enemy_attack * enemy_present
                    enemy_life = enemy_life - player_attack *player_present + enemy_shield
                elif player_shield >= 0 and enemy_shield < 0:
                    player_life = player_life - enemy_attack * enemy_present + player_shield 
                    enemy_life = enemy_life - player_attack *player_present
                else:
                    player_life = player_life - enemy_attack * enemy_present
                    enemy_life = enemy_life - player_attack *player_present

                print('你发起了攻击,【敌人】剩余血量%f' % enemy_life)
                print('敌人向你发起了攻击,【玩家】的血量剩余%f' % player_life)
                print('-----------------------')
                time.sleep(1)

            if player_life > 0 and enemy_life <= 0:
                player_victory += 1
                print('敌人死翘翘了,你赢了!')
            elif player_life <= 0 and enemy_life > 0:
                enemy_victory += 1
                print('悲催,敌人把你干掉了!')
            else:
                print('哎呀,你和敌人同归于尽了!')
            time.sleep(1)
            print('现在你积%s分,敌人积%s分' %(player_victory,enemy_victory))
            time.sleep(1.5)

        time.sleep(1)
        if player_victory > enemy_victory :
            print('\n【最终结果:你赢了!】')
            break
        elif enemy_victory > player_victory:
            print('\n【最终结果:你输了!】')
            break
        else: 
            print('\n【平局,加赛一场!】')
            sum1 += 1

    a1 = input('要继续游戏吗,请输入n退出,输入其他继续:')  # 在 while True 循环中设置跳出条件。
    if a1 == 'n':
        break

 

;