给大家分享一个简单模拟精灵战斗游戏的Python小程序

视频效果如下:

精灵战斗游戏视频效果

代码如下,粘贴即可运行:

# 初始化状态
states = [
    ["无"],  # 无状态
    ["曼陀罗之毒", 10000, 24, "伤害", "曼陀罗"],  # 曼陀罗之毒
    ["石像之力", 8848, 3, "增防", "顽石魔像"]  # 石像之力
]
# 初始化技能
skills = [
    ["曼陀罗之吻", states[1], "对敌附加", 20],  # 曼陀罗之吻
    ["石像之力", states[2], "对己附加", 20],  # 石像之力
]
# 初始化精灵
spirit_name = "曼陀罗"  # 精灵名称
spirit_hp = 1000  # 精灵血量
spirit_mp = 100  # 精灵蓝量
spirit_atk = 1000  # 精灵攻击力
spirit_def = 20  # 精灵防御力
spirit_spd = 46  # 精灵速度
spirit_skill = skills[0]  # 精灵技能  # 新建精灵
enemy_name = "顽石魔像"  # 敌人名称
enemy_hp = 10000  # 敌人血量
enemy_mp = 100  # 敌人蓝量
enemy_atk = 50  # 敌人攻击力
enemy_def = 673  # 敌人防御力
enemy_spd = 20  # 敌人速度
enemy_skill = skills[1]  # 敌人技能  # 新建敌人
# 初始化战斗数据
temp_spirit_hp = spirit_hp  # 精灵血量
temp_spirit_mp = spirit_mp  # 精灵蓝量
temp_spirit_atk = spirit_atk  # 精灵攻击力
temp_spirit_def = spirit_def  # 精灵防御力
temp_spirit_spd = 0  # 精灵速度
temp_spirit_states = list(states[0])  # 精灵状态  # 新建精灵
temp_enemy_hp = enemy_hp  # 敌人血量
temp_enemy_mp = enemy_mp  # 敌人蓝量
temp_enemy_atk = enemy_atk  # 敌人攻击力
temp_enemy_def = enemy_def  # 敌人防御力
temp_enemy_spd = 0  # 敌人速度
temp_enemy_states = list(states[0])  # 敌人状态  # 新建敌人
attacker = None
temp_attacker_hp = 0  # 攻击者血量
temp_attacker_mp = 0  # 攻击者蓝量
temp_attacker_atk = 0  # 攻击者攻击力
temp_attacker_def = 0  # 攻击者防御力
temp_attacker_spd = 0  # 攻击者速度
temp_attacker_states = None  # 攻击者状态
attacker_skill = None  # 攻击者技能  # 新建攻击者
target = None
temp_target_hp = 10000  # 目标血量
temp_target_mp = 100  # 目标蓝量
temp_target_atk = 50  # 目标攻击力
temp_target_def = 250  # 目标防御力
temp_target_spd = 0  # 目标速度
temp_target_states = None  # 目标状态  # 新建目标

# 开始战斗
time = 1  # 初始局数
dead = False  # 死亡状态
while True:
    print('\n' * 100)
    print("----- 第", time, "局 -----")
    # 清除多余的空状态
    if len(temp_spirit_states) != 1:  # 调整我方状态
        # 删除空状态
        for state in temp_spirit_states:
            if state[0] == "无":
                temp_spirit_states.remove(state)
        # 添加空状态
        if len(temp_spirit_states) == 0:
            temp_spirit_states.append(list(states[0]))
    if len(temp_enemy_states) != 1:  # 调整敌方状态
        # 删除空状态
        for state in temp_enemy_states:
            if state[0] == "无":
                temp_enemy_states.remove(state)
        # 添加空状态
        if len(temp_enemy_states) == 0:
            temp_enemy_states.append(list(states[0]))
    # 创建状态展示栏
    temp_spirit_states_show = ""  # 创建我方状态展示栏
    temp_enemy_states_show = ""  # 创建敌方状态展示栏
    # 更新我方状态展示栏
    for state in temp_spirit_states:  # 更新我方状态展示栏
        if temp_spirit_states[0] == state:  # 第一个状态
            temp_spirit_states_show = state[0]  # 加名称,不加空格
        else:  # 其他状态
            temp_spirit_states_show = temp_spirit_states_show + " " + state[0]  # 加名称,加空格
        if temp_spirit_states[0][0] != "无":
            temp_spirit_states_show = temp_spirit_states_show + "(" + str(state[2]) + ")"  # 加持续时间
    # 更新敌方状态展示栏
    for state in temp_enemy_states:  # 更新敌方状态展示栏
        if temp_enemy_states[0] == state:  # 第一个状态
            temp_enemy_states_show = state[0]  # 加名称,不加空格
        else:  # 其他状态
            temp_enemy_states_show = temp_enemy_states_show + " " + state[0]  # 加名称,加空格
        if temp_enemy_states[0][0] != "无":
            temp_enemy_states_show = temp_enemy_states_show + "(" + str(state[2]) + ")"  # 加持续时间
    # 展示双方信息
    print("[", spirit_name, ']\n',  # 展示我方精灵
          "血量:", str(temp_spirit_hp) + "/" + str(spirit_hp), '\n',
          "蓝量:", str(temp_spirit_mp) + "/" + str(spirit_mp), '\n',
          "攻击力:", temp_spirit_atk, '\n',
          "防御力:", temp_spirit_def, '\n',
          "速度:", spirit_spd, '\n',
          "状态:", temp_spirit_states_show, '\n',
          "技能:", spirit_skill[0], '\n',
          sep="")
    print("[", enemy_name, ']\n',  # 展示敌方精灵
          "血量:", str(temp_enemy_hp) + "/" + str(enemy_hp), '\n',
          "蓝量:", str(temp_enemy_mp) + "/" + str(enemy_mp), '\n',
          "攻击力:", temp_enemy_atk, '\n',
          "防御力:", temp_enemy_def, '\n',
          "速度:", enemy_spd, '\n',
          "状态:", temp_enemy_states_show, '\n',
          "技能:", enemy_skill[0], '\n',
          sep="")
    input("任意键继续>>> ")
    # 决定攻击者
    while True:
        # 速度叠加
        temp_spirit_spd += spirit_spd  # 叠加我方速度
        temp_enemy_spd += enemy_spd  # 叠加敌方速度
        # 决定先后
        if temp_spirit_spd >= 100:  # 若我方精灵快
            temp_spirit_spd -= 100  # 清空速度
            attacker = spirit_name  # 攻击者
            temp_attacker_hp = temp_spirit_hp  # 攻击者血量
            temp_attacker_mp = temp_spirit_mp  # 攻击者蓝量
            temp_attacker_atk = temp_spirit_atk  # 攻击者攻击力
            temp_attacker_def = temp_spirit_def  # 攻击者防御力
            temp_attacker_spd = temp_spirit_spd  # 攻击者速度
            temp_attacker_states = temp_spirit_states  # 攻击者状态
            attacker_skill = spirit_skill  # 攻击者技能

            target = enemy_name  # 目标
            temp_target_hp = temp_enemy_hp  # 目标血量
            temp_target_mp = temp_enemy_mp  # 目标蓝量
            temp_target_atk = temp_enemy_atk  # 目标攻击力
            temp_target_def = temp_enemy_def  # 目标防御力
            temp_target_spd = temp_enemy_spd  # 目标速度
            temp_target_states = temp_enemy_states  # 目标状态
            break
        if temp_enemy_spd >= 100:  # 若敌方精灵快
            temp_enemy_spd -= 100  # 清空速度
            attacker = enemy_name  # 攻击者
            temp_attacker_hp = temp_enemy_hp  # 攻击者血量
            temp_attacker_mp = temp_enemy_mp  # 攻击者蓝量
            temp_attacker_atk = temp_enemy_atk  # 攻击者攻击力
            temp_attacker_def = temp_enemy_def  # 攻击者防御力
            temp_attacker_spd = temp_enemy_spd  # 攻击者速度
            temp_attacker_states = temp_enemy_states  # 攻击者状态
            attacker_skill = enemy_skill  # 攻击者技能
            target = spirit_name  # 目标
            temp_target_hp = temp_spirit_hp  # 目标血量
            temp_target_mp = temp_spirit_mp  # 目标蓝量
            temp_target_atk = temp_spirit_atk  # 目标攻击力
            temp_target_def = temp_spirit_def  # 目标防御力
            temp_target_spd = temp_spirit_spd  # 目标速度
            temp_target_states = temp_spirit_states  # 目标状态
            break
    # 状态结算
    if temp_attacker_states[0] != "无":  # 若敌方有状态
        for state in temp_attacker_states:  # 遍历查看是否有执行的状态
            if state[0] != "无":
                match state[3]:  # 匹配状态类型
                    case "伤害":  # 扣除血量
                        temp_attacker_hp -= int(state[1] * (1 - temp_attacker_def / state[1]))
                        print("受到", state[0], "状态影响,", attacker, "损失了",
                              int(state[1] * (1 - temp_attacker_def / state[1])), "点生命")
                        input("任意键继续>>> ")
                    case "增防":  # 增加攻击
                        if state[2] == 0:
                            temp_attacker_def -= state[1]  # 状态结束,恢复防御
                state[2] -= 1  # 状态次数减少
                if state[2] == 0:  # 状态消失检测
                    temp_attacker_states.remove(state)  # 移除状态
                if temp_attacker_hp <= 0:  # 死亡检测
                    print(attacker, "被", state[4], "击败!游戏结束!")
                    dead = True
                    break
    # 是否结束战斗
    if dead:
        break
    # 发起攻击
    if temp_attacker_mp >= attacker_skill[3]:  # 蓝量充足,发起攻击
        temp_attacker_mp -= attacker_skill[3]  # 扣除蓝量
        print(attacker, "对", target, "发起攻击!")
        input("任意键继续>>> ")
        print(attacker, "使用了", attacker_skill[0], "!")
        input("任意键继续>>> ")
        # 技能做功
        if attacker_skill[2] == "对敌附加":  # 对敌附加型技能
            exist = False
            for state in temp_target_states:
                if state[0] == attacker_skill[1][0]:  # 存在该状态
                    state[2] = attacker_skill[1][2]  # 只刷新次数
                    exist = True
                    break
            if not exist:  # 不存在该状态
                temp_target_states.append(list(attacker_skill[1]))  # 新建状态
            print(target, "被附加了", attacker_skill[1][0], "状态!每次行动前将受到伤害!")
        if attacker_skill[2] == "对己附加":  # 对己附加型技能
            exist = False
            for state in temp_attacker_states:
                if state[0] == attacker_skill[1][0]:  # 存在该状态
                    state[2] = attacker_skill[1][2]  # 只刷新次数
                    exist = True
                    break
            if not exist:  # 不存在该状态
                temp_attacker_states.append(list(attacker_skill[1]))  # 新建状态
                temp_attacker_def += attacker_skill[1][1]
            print(attacker, "被附加了", attacker_skill[1][0], "状态,增加了", attacker_skill[1][1], "点防御!")
    else:  # 蓝量不足,跳过
        print(attacker, "蓝量不足,无法发动攻击!")
    # 结算
    if attacker == spirit_name:  # 当我方出手时
        temp_spirit_hp = temp_attacker_hp  # 我方生命
        temp_spirit_mp = temp_attacker_mp  # 我方法力
        temp_spirit_atk = temp_attacker_atk  # 我方攻击
        temp_spirit_def = temp_attacker_def  # 我方防御
        temp_spirit_states = temp_attacker_states  # 我方状态
        temp_enemy_hp = temp_target_hp  # 敌方血量
        temp_enemy_mp = temp_target_mp  # 敌方法力
        temp_enemy_atk = temp_target_atk  # 敌方攻击
        temp_enemy_def = temp_target_def  # 敌方防御
        temp_enemy_states = temp_target_states  # 敌方状态
    if attacker == enemy_name:  # 当敌方出手时
        temp_enemy_hp = temp_attacker_hp  # 敌方血量
        temp_enemy_mp = temp_attacker_mp  # 敌方法力
        temp_enemy_atk = temp_attacker_atk  # 敌方攻击
        temp_enemy_def = temp_attacker_def  # 敌方防御
        temp_enemy_states = temp_attacker_states  # 敌方状态
        temp_spirit_hp = temp_target_hp  # 我方血量
        temp_spirit_mp = temp_target_mp  # 我方法力
        temp_spirit_atk = temp_target_atk  # 我方攻击
        temp_spirit_def = temp_target_def  # 我方防御
        temp_spirit_states = temp_target_states  # 我方状态
    input("任意键继续>>> ")
    time += 1  # 次数迭代

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值