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

Python文本格斗游戏格挡功能异常问题求助

文本格斗游戏格挡逻辑问题修复

问题根源

当前代码的核心问题在于攻击逻辑与格挡逻辑完全脱钩:

  • 玩家发起攻击(出拳/踢腿)时,会直接强制触发AI的格挡判定,不管AI本回合是否选择了格挡;
  • AI发起攻击时同理,强制触发玩家的格挡判定,无视玩家的实际操作选择;
  • 当玩家或AI主动选择格挡时,只是孤立执行了格挡判定,没有和后续的攻击行为建立关联,导致每次操作都会触发格挡提示,逻辑混乱。

修复方案

我们需要通过状态标记来关联格挡选择与攻击判定,具体调整如下:

  1. 新增状态变量记录玩家/AI是否选择格挡;
  2. 攻击行为仅在目标处于格挡状态时,才触发格挡判定;
  3. 修正回合流程:先记录双方操作状态,再处理攻击交互;
  4. 修复heal函数的赋值bug(原代码未实际增加生命值)。

修改后的完整代码

import random

ai_health = 100
player_health = 100
# 新增状态变量:标记当前回合是否选择格挡
player_blocking = False
ai_blocking = False

def heal():
    global player_health
    heal_amount = random.randint(0,20)
    player_health += heal_amount 
    print(f"Player has healed for: {heal_amount}, current health: {player_health}")

def player_attack(damage_min, damage_max):
    global ai_health
    damage = random.randint(damage_min, damage_max)
    # 只有AI选择格挡时,才触发格挡判定
    if ai_blocking:
        if block_function("AI"):
            print("AI took no damage!")
            return
    ai_health -= damage
    print(f"AI has been hit for {damage}")
    print(f"AI remaining health: {ai_health}")

def ai_attack(damage_min, damage_max):
    global player_health
    damage = random.randint(damage_min, damage_max)
    # 只有玩家选择格挡时,才触发格挡判定
    if player_blocking:
        if block_function("Player"):
            print("Player took no damage!")
            return
    player_health -= damage
    print(f"Player has been hit for {damage}")
    print(f"Player remaining health: {player_health}")

def ai_function():
    global ai_blocking
    ai_blocking = False  # 重置AI格挡状态
    ai_move = random.choice([1, 2, 3])
    if ai_move == 1:
        ai_attack(5, 15)  # AI出拳
    elif ai_move == 2:
        ai_attack(10, 20) # AI踢腿
    elif ai_move == 3:
        print("AI is blocking!")
        ai_blocking = True  # 标记AI选择格挡

def block_function(character):
    block = random.randint(0, 1)
    if block == 1:
        print(f"{character} successfully blocked the attack!")
        return True
    else:
        print(f"{character} failed to block the attack!")
        return False

game_over = False
while not game_over:
    global player_blocking
    player_blocking = False  # 重置玩家格挡状态
    try:
        move = int(input("Enter 1 to punch, 2 to kick, 3 to block, 4 to heal: "))
        if move == 1:
            player_attack(5, 15)  # 玩家出拳
        elif move == 2:
            player_attack(10, 20) # 玩家踢腿
        elif move == 3:
            print("Player is blocking!")
            player_blocking = True  # 标记玩家选择格挡
        elif move == 4:
            heal()
        else:
            print("Invalid move. Please enter 1(punch), 2(kick), 3(block), 4(heal).\n")
            continue
        
        ai_function()
        
        if ai_health <= 0:
            print("You have defeated the AI!")
            game_over = True
        elif player_health <= 0:
            print("You have been defeated!")
            game_over = True
    except ValueError:
        print("Invalid input. Please enter a number.")

关键调整说明

  • 新增player_blocking和ai_blocking变量,仅当角色选择格挡时设为True;
  • 重构攻击逻辑为通用的player_attack和ai_attack函数,避免代码重复;
  • 攻击时仅检查目标是否处于格挡状态,再触发格挡判定,符合真实格斗逻辑;
  • 修复了heal函数的赋值错误,现在能正确增加玩家生命值;
  • 新增了加血功能的调用入口(输入4),完善游戏体验。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 08:47:41