首页 > 解决方案 > 轮流使用 Discord.py 游戏

问题描述

我正在尝试做的事情:我正在尝试使用 discord.py 创建回合制格斗游戏。虽然两名玩家的生命值都大于 0,但消息作者和提及的用户将轮流使用拳打、治疗或投降(不言自明)。

问题:我不确定在合并转弯切换时从哪里开始。下面我发布了我用于消息作者的全部代码,但我似乎无法找到一种方法来在消息作者转弯后进行下一次转弯,反之亦然。

@client.command()
async def battle(ctx, user: discord.Member):
    aut = 5
    ops = 5
    user = user.mention
    cmam = ctx.message.author.mention
    if user == cmam:
        await ctx.send(f"You can't fight yourself {cmam} <:BlobWuh:761383059349438464>")
    else:
        await ctx.send(f'{cmam} vs {user}, who will win?')
        while aut > 0 and ops > 0:
            await ctx.send(f'{cmam}: `Punch, Heal, Surrender`')

            def check(m):
                return m.content == 'punch' or m.content == 'heal' or m.content == 'surrender' and m.author == ctx.message.author

            response = await client.wait_for('message', check = check)
            if "punch" in response.content.lower():
                if ops > 0:
                    dmg = [0, 1, 2]
                    dmg = (random.choice(dmg))
                    ops = ops - dmg
                    await ctx.send(f"{user} is down to **{ops}** health")
                    if ops <= 0:
                        await ctx.send(f"**{cmam} has won the battle**")
                        break
                elif ops <= 0:
                    await ctx.send(f"**{cmam} has won the battle**")
                    break
            if "heal" in response.content.lower():
                if aut >= 5:
                    await ctx.send(f"{cmam} already has **{aut}** health, they lose a turn")
                else:
                    aut = aut + 1
                    await ctx.send(f"{cmam} now has **{aut}** health")
            if "surrender" in response.content.lower():
                await ctx.send(f"{cmam} has surrendered with **{aut}** health. {user} has claimed victory with **{ops}** health remaining")
                aut = aut - 20
                ops = ops - 20
                break      

标签: pythondiscord.pydiscord.py-rewrite

解决方案


好的,这适用于再次偶然发现此问题的任何人,它几乎是相同的代码,但有一个转折变量来存储当前转折。(感谢@Ethan MH 的建议)

@client.command()
async def test(ctx, user: discord.Member):
    turn = 0 #adding a turn variable for turn taking
    aut = 5
    ops = 5
    user = user.mention
    cmam = ctx.message.author.mention
    if user == cmam:
        await ctx.send(f"You can't fight yourself {cmam}")
    else:
        await ctx.send(f"{cmam} vs {user}, who will win?")
        while aut > 0 and ops > 0:

            if turn == 0:
                await ctx.send(f"{cmam}: `test`")
                def check(m):
                    return m.content == "test" and m.author == ctx.message.author
                response = await client.wait_for('message', check = check)
                if "test" in response.content.lower() and turn == 0:#turn == 0 is here since it doesn't work sometimes
                    await ctx.send("a nice test")
                    turn = turn + 1

            elif turn == 1:
                await ctx.send(f"{user}: `test`")
                def check(o):
                    return o.content == "test" and o.author == discord.Member
                response = await client.wait_for('message', check = check)
                if "test" in response.content.lower() and turn == 1:#turn == 1 is here since (elif turn == 1) doesn't work sometimes
                    await ctx.send("the test is strong with this one")
                    turn = turn - 1

推荐阅读