使用discord.py开发回合制对战游戏,如何实现回合切换逻辑?
核心问题定位
- 无回合状态维护:循环内始终只对战斗发起者做操作提示、消息校验,完全没有对手的参与入口
- 变量存储错误:提前将被@的用户、发起者转成mention字符串,丢失原始
discord.Member对象,后续无法对不同玩家做消息权限校验 - 消息校验逻辑bug:
and运算优先级高于or,原check函数的写法会绕过作者校验,任意人发送的punch/heal消息都会被判定为有效操作 - 技能逻辑无适配:所有操作的效果固定绑定发起者视角,没有根据回合归属动态调整伤害、治疗、投降的判定对象
回合切换核心实现逻辑
- 保留两个玩家的原始
discord.Member对象,独立维护双方血量值 - 新增
current_player变量标记当前回合操作人,初始值为战斗发起者 - 每轮循环根据
current_player发对应操作提示,动态校验消息是否来自当前回合玩家 - 若当前玩家操作完成后,双方血量未到判定胜利的阈值,直接将
current_player切换为另一方,进入下一轮循环 - 所有操作效果完全绑定当前回合归属:出拳打对面、治疗自己、投降则对方获胜
修正后完整可运行代码
import random import discord from discord.ext import commands # 替换为你自己的bot初始化配置 intents = discord.Intents.default() intents.message_content = True client = commands.Bot(command_prefix="!", intents=intents) @client.command() async def battle(ctx, user: discord.Member): challenger = ctx.author opponent = user challenger_hp = 5 opponent_hp = 5 if challenger == opponent: await ctx.send(f"You can't fight yourself {challenger.mention} <:BlobWuh:761383059349438464>") return await ctx.send(f'{challenger.mention} vs {opponent.mention}, who will win?') current_player = challenger while challenger_hp > 0 and opponent_hp > 0: await ctx.send(f'{current_player.mention}: `Punch, Heal, Surrender`') def check(m): valid_actions = ['punch', 'heal', 'surrender'] return (m.content.lower() in valid_actions) and (m.author == current_player) try: # 加60秒超时,避免玩家不操作导致bot永久等待 response = await client.wait_for('message', check=check, timeout=60) except: await ctx.send(f"等待{current_player.mention}操作超时,战斗终止") break action = response.content.lower() # 出拳判定:当前玩家出拳,扣除对方随机血量 if action == "punch": dmg = random.choice([0, 1, 2]) if current_player == challenger: opponent_hp -= dmg await ctx.send(f"你对对手造成{dmg}点伤害,对手剩余血量:**{opponent_hp}**") if opponent_hp <= 0: await ctx.send(f"**{challenger.mention} has won the battle**") break else: challenger_hp -= dmg await ctx.send(f"你对对手造成{dmg}点伤害,对手剩余血量:**{challenger_hp}**") if challenger_hp <= 0: await ctx.send(f"**{opponent.mention} has won the battle**") break # 治疗判定:当前玩家回血,上限5点 elif action == "heal": if current_player == challenger: if challenger_hp >= 5: await ctx.send(f"{current_player.mention} already has **{challenger_hp}** health, they lose a turn") else: challenger_hp += 1 await ctx.send(f"{current_player.mention} now has **{challenger_hp}** health") else: if opponent_hp >= 5: await ctx.send(f"{current_player.mention} already has **{opponent_hp}** health, they lose a turn") else: opponent_hp += 1 await ctx.send(f"{current_player.mention} now has **{opponent_hp}** health") # 投降判定:当前玩家投降,对方获胜 elif action == "surrender": if current_player == challenger: await ctx.send(f"{challenger.mention} has surrendered with **{challenger_hp}** health. {opponent.mention} has claimed victory with **{opponent_hp}** health remaining") else: await ctx.send(f"{opponent.mention} has surrendered with **{opponent_hp}** health. {challenger.mention} has claimed victory with **{challenger_hp}** health remaining") break # 未触发战斗结束时,切换回合 current_player = opponent if current_player == challenger else challenger
可选优化方向
- 加战斗状态锁,避免同一玩家同时发起多场对战导致状态冲突
- 抽取重复的判定逻辑为公共函数,减少代码冗余
- 增加操作提示的视觉反馈,比如用embed面板展示双方实时血量
内容的提问来源于stack exchange,提问作者Bagle
相关产品推荐
相关产品推荐

