discord.py(重写)如何为 rps(石头剪刀布)游戏创建获胜条件?

discord.py (rewrite) How do I create a win condition for a rps(rock, paper, scissors) game?

我正在尝试为剪刀石头布游戏设置获胜条件。我试过这样做,但它只是坏了,完全不起作用。这里有一些代码可以解决:

import discord
from discord.ext import commands, tasks
import os
import random
import asyncio
from asyncio import gather

@client.command()
async def rps(ctx, user_choice):
    rpsGame = ['rock', 'paper', 'scissors']
    if user_choice == 'rock' or user_choice == 'paper' or user_choice == 'scissors':
        await ctx.send(f'Choice: `{user_choice}`\nBot Choice: `{random.choice(rpsGame)}`')
        bot_choice = f'{random.choice(rpsGame)}'
    else:
        await ctx.send('**Error** This command only works with rock, paper, or scissors.')

    # Rock Win Conditions #
    if user_choice == 'rock' and bot_choice == 'paper':
        await ctx.send('I won!')
    if user_choice == 'rock' and bot_choice == 'scissors':
        await ctx.send('You won!')
    # Paper Win Conditions #
    if user_choice == 'paper' and bot_choice == 'rock':
        await ctx.send('You won!')
    if user_choice == 'paper' and bot_choice == 'scissors':
        await ctx.send('I won!')
    # Scissor Win Conditions #
    if user_choice == 'scissors' and bot_choice == 'paper':
        await ctx.send('You won!')
    if user_choice == 'scissors' and bot_choice == 'rock':
        await ctx.send('I won!')


client.run('token')

这里重写了你的代码,我修复了一些无用的 f 字符串,并在 if user_choice is in rpsGame 中移动了 win/lose 条件的缩进,以防止 variable referenced before assignment

@client.command()
async def rps(ctx, user_choice):
    rpsGame = ['rock', 'paper', 'scissors']
    if user_choice.lower() in rpsGame: # Better use this, its easier. [lower to prevent the bot from checking a word like this "rOcK or pApeR"
        bot_choice = random.choice(rpsGame)
        await ctx.send(f'Choice: `{user_choice}`\nBot Choice: `{bot_choice}`')
        user_choice = user_choice.lower() # Also prevent a random word such as "rOcK"
        if user_choice == bot_choice:
            await ctx.send('We tied')
        # Rock Win Conditions #
        if user_choice == 'rock' and bot_choice == 'paper':
            await ctx.send('I won!')
        if user_choice == 'rock' and bot_choice == 'scissors':
            await ctx.send('You won!')
        # Paper Win Conditions #
        if user_choice == 'paper' and bot_choice == 'rock':
            await ctx.send('You won!')
        if user_choice == 'paper' and bot_choice == 'scissors':
            await ctx.send('I won!')
        # Scissor Win Conditions #
        if user_choice == 'scissors' and bot_choice == 'paper':
            await ctx.send('You won!')
        if user_choice == 'scissors' and bot_choice == 'rock':
            await ctx.send('I won!')
    else:
        await ctx.send('**Error** This command only works with rock, paper, or scissors.')

你应该把 bot_choice 变成一个 1 变量,唯一让它坏掉的东西是因为机器人发送了一个与 ctx.send 下的 bot_choice 不同的选择 我也添加了绑定选项。