You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python回合制文本战斗游戏无效输入处理问题求助

双玩家回合制Python文本战斗游戏无效输入处理异常修复

问题概述

开发的双玩家回合制Python文本战斗游戏中,玩家A、B可轮流执行攻击、特殊攻击或使用药水操作,有效选项功能正常,但输入无效选项时,本该让当前玩家重新输入,实际却出现每两次输入就判定一次无效,且直接跳转到对方回合的异常问题。

问题根源

  1. 条件结构错误:turnA和turnB函数中使用多个独立的if语句而非if-elif结构,导致输入有效选项(如"1"或"2")时,第三个if不成立,会触发后续的else分支,错误跳转到对方回合。
  2. 无效输入逻辑错误:无效输入分支中调用了对方回合函数(如turnA的else里调用turnB),而非重新调用当前回合函数,导致当前玩家失去输入机会,直接切换回合。
  3. 流程混乱:无效输入后错误执行了clear()、stats()等无关操作,打乱了回合流程。

修正后的完整代码

import os
import random

battle = True

nameA = "John"
nameB = "Marcus"

maxhpA = 50
hpA = 50  # amount of health
atkA = 5  # attack damage
potA = 10  # potion number
luckA = 30
goldA = 0  # amount of player_gold

maxhpB = 50
hpB = 50  # amount of health
atkB = 5  # attack damage
potB = 10  # potion number
luckB = 30
goldB = 0  # amount of player_gold

def draw():
    print("XX-----------------------XX")

def clear():
    os.system("cls")

clear()

def stop():
    input(">")

def healA(amount_healed):
    global hpA, maxhpA, nameA
    if hpA + amount_healed < maxhpA:
        hpA += amount_healed
        print(f"{nameA} healed {amount_healed} HP! Current HP: {hpA}")
    else:
        hpA = maxhpA
        print(nameA + "'s HP refilled to " + str(hpA) + "!")

def healB(amount_healed):
    global hpB, maxhpB, nameB
    if hpB + amount_healed < maxhpB:
        hpB += amount_healed
        print(f"{nameB} healed {amount_healed} HP! Current HP: {hpB}")
    else:
        hpB = maxhpB
        print(nameB + "'s HP refilled to " + str(hpB) + "!")

def turnA():
    global hpA, hpB, atkA, atkB, nameA, nameB, potA, potB
    
    choice = input(f"{nameA}'s turn >")
    
    if choice == "1":
        hpB -= atkA
        print(f"{nameA} attacked!")
        print(f"{nameB} took {str(atkA)} damage!")
        stop()
    elif choice == "2":
        if random.randint(1,100) < luckA:
            hpB -= 2*atkA
            print()
            print(f"{nameA} performed a special attack!!")
            print(f"{nameB} took {str(2*atkA)} damage!")
            stop()
        else:
            print(f"{nameA} missed their special attack!")
            stop()
    elif choice == "3":
        if potA > 0:
            potA -= 1
            print(f"{nameA} used a potion.")
            healA(25)
            stop()            
        else:
            print("No potions!")
            stop()    
    else:
        print("Invalid option! Please enter 1, 2, or 3.")
        turnA()  # 重新调用当前回合函数,让玩家重新输入

def turnB():
    global hpA, hpB, atkA, atkB, nameA, nameB, potA, potB
    
    choice = input(f"{nameB}'s turn >")
    
    if choice == "1":
        hpA -= atkB
        print(f"{nameB} attacked!")
        print(f"{nameA} took {str(atkB)} damage!")
        stop()
    elif choice == "2":
        if random.randint(1,100) < luckB:
            hpA -= 2*atkB
            print()
            print(f"{nameB} performed a special attack!!")
            print(f"{nameA} took {str(2*atkB)} damage!")
            stop()
        else:
            print(f"{nameB} missed their special attack!")
            stop()
    elif choice == "3":
        if potB > 0:
            potB -= 1
            print(f"{nameB} used a potion.")
            healB(25)
            stop()            
        else:
            print("No potions!")
            stop()
    else:
        print("Invalid option! Please enter 1, 2, or 3.")
        turnB()  # 重新调用当前回合函数,让玩家重新输入

def stats():
    print("Name:    {0:15}{1}".format(str(nameA), str(nameB)))
    print("HP:      {0:15}{1}".format(str(hpA), str(hpB)))
    print("ATK:     {0:15}{1}".format(str(atkA), str(atkB)))
    print("POTIONS: {0:15}{1}".format(str(potA), str(potB)))
    print("LUCK:    {0:15}{1}".format(str(luckA), str(luckB)))
    
    draw()
    
    print("1 - ATTACK")
    print("2 - RISKY ATTACK")
    print("3 - USE POTION (25HP)")
    
    draw()

while battle:
    stats()
    turnA()
    clear()
    
    # 检查玩家B是否战败,结束循环
    if hpB <= 0:
        print(f"{nameB} has been defeated! {nameA} wins!")
        battle = False
        break
    
    stats()
    turnB()
    clear()
    
    # 检查玩家A是否战败,结束循环
    if hpA <= 0:
        print(f"{nameA} has been defeated! {nameB} wins!")
        battle = False
        break

关键修正说明

  • 条件结构优化:将turnA和turnB中的多个独立if改为if-elif-elif-else,确保只有匹配的分支执行,避免有效输入触发无效分支。
  • 无效输入逻辑修正:无效输入时重新调用当前回合函数(turnA()/turnB()),让当前玩家重新输入,不再跳转到对方回合。
  • 补充战败判定:在每回合结束后检查玩家HP,若HP≤0则结束游戏,完善核心战斗逻辑。
  • 统一提示格式:使用f-string优化输出格式,同时在healA/healB中补充了非满血时的治愈提示,提升体验。

内容的提问来源于stack exchange,提问作者Tomas Whelan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 05:47:58