【问题标题】:discord.py join a game commanddiscord.py 加入游戏命令
【发布时间】:2021-10-06 21:22:24
【问题描述】:

我想在第二个玩家使用 %tttjoin 加入游戏时让游戏开始。我尝试了我的想法,但我真的不知道该怎么做,所以我将我的 Player2 设置为 True。而当有人使用命令 %tttjoin p2 时设置为 False。然后使用“如果 p2 为 False:”行,游戏开始。但是这个功能不起作用,我不知道我的想法是如何工作的......我希望有人知道该怎么做......而且我还没有收到任何错误。游戏运行良好,没有任何错误。当我写 %ttt 时,机器人会发送消息,但是当有人加入 %tttjoin 时,没有任何反应。我的错误应该是“如果 p2 为 False”这行,但我不知道我必须更改什么。‎‎‎‎‎‎‎ ‎‎‎‎‎‎ ‎‎‎‎

   async def tttjoin(self, ctx):
        global p2
        global p2name
        p2 = ctx.author
        p2 = False
        p2name = ctx.author.name

    @commands.command(aliases=["tiktaktoe", "ttt"])
    async def tictactoe(self, ctx):
        global player1
        global player2
        global p2
        global turn
        global gameOver
        global count
        player1 = ctx.author
        player2 = p2
        p1name = ctx.author.name
        msg = await ctx.send(f"<%tttjoin> um mitzuspielen. \n Player 1: {p1name} Player 2: / ")
        if p2 is False:
            await msg.edit(f"Player 1: {p1name} Player 2: {p2name} ")
            if gameOver:
                global board
                board = [":white_large_square:", ":white_large_square:", ":white_large_square:",
                         ":white_large_square:", ":white_large_square:", ":white_large_square:",
                         ":white_large_square:", ":white_large_square:", ":white_large_square:"]
                turn = ""
                gameOver = False
                count = 0

                line = ""
                for x in range(len(board)):
                    if x == 2 or x == 5 or x == 8:
                        line += " " + board[x]
                        await ctx.send(line)
                        line = ""
                    else:
                        line += " " + board[x]

                num = random.randint(1, 2)
                if num == 1:
                    turn = player1
                    await ctx.send(f"{player1.mention} fängt an!")
                elif num == 2:
                    turn = player2
                    await ctx.send(f"{player2.mention} fängt an!")

            else:
                await ctx.send("Es wird bereits gespielt! Bitte wartet bis das Spiel fertig ist!")

    @commands.command()
    async def place(self, ctx, pos: int = None):
        global turn
        global player1
        global player2
        global board
        global count

        if pos is None:
            await ctx.send("Du musst eine Position wählen!")

        if not gameOver:
            mark = ""
            if turn == ctx.author:
                if turn == player1:
                    mark = ":regional_indicator_x:"
                elif turn == player2:
                    mark = ":o2:"
                if 0 < pos < 10:
                    if board[pos - 1] == ":white_large_square:":
                        board[pos -1] = mark
                        count += 1

                        line = ""
                        for x in range(len(board)):
                            pass
                        checkWinner(winningC, mark)
                        if gameOver:
                            await ctx.send(f"{mark} hat gewonnen!")
                        elif count >= 9:
                            await ctx.send("Unentschieden")


                        if turn == player1:
                            turn = player2
                        elif turn == player2:
                            turn = player1

    @commands.command()
    @commands.has_role("Admin")
    async def tttquit(self, ctx):
        global gameOver
        global p2
        p2 = True
        gameOver = True
        await ctx.send("Das Spiel wurde beendet!")‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ 

【问题讨论】:

    标签: python discord.py command


    【解决方案1】:

    我目前不在电脑旁,但看起来if p2 is False: 行实际上导致了问题,因为该函数将在第二个玩家有机会调用tttjoin 函数之前到达这一点,这意味着代码将直接传递到底部的else

    要解决这个问题,您可以使用 while 循环,这样代码只会在 p2 == False 时向前移动。 比如:

    global x
    while x:
       if not(p2):
           await msg.edit(f"Player 1: {p1name} Player 2: {p2name} ")
          x = False
    

    希望这是有道理的 :) P.s 我没有测试它,因为我不在我的电脑上,所以我不确定。如果这不起作用,我可能会在我回来时编辑解决方案。

    【讨论】:

    • 谢谢,你的方法并不完全有效,但我找到了解决它的方法!
    【解决方案2】:

    我让 %ttt 代码在机器人获得第一个玩家后结束。然后在 %tttjoin 我检查了 player2 == True 并将他设置为 False 然后我检查了 player2 == False: 并从 %ttt 复制并粘贴代码。

        async def tictactoe(self, ctx):
            global player1
            global player2
            global p2
            global p1
            global turn
            global gameOver
            global count
            global x
            global p1fullname
            global msg
            p1 = ctx.author
            player1 = p1
            p1fullname = ctx.author.name
            if gameOver:
                global board
                board = [":white_large_square:", ":white_large_square:", ":white_large_square:",
                         ":white_large_square:", ":white_large_square:", ":white_large_square:",
                         ":white_large_square:", ":white_large_square:", ":white_large_square:"]
                turn = ""
                gameOver = False
                count = 0
    
                line = ""
                for x in range(len(board)):
                    if x == 2 or x == 5 or x == 8:
                        line += " " + board[x]
                        await ctx.send(line)
                        line = ""
                    else:
                        line += " " + board[x]
    
        @commands.command()
        async def place(self, ctx, pos: int = None):
            global turn
            global player1
            global player2
            global board
            global count
            global p1name
    
            if pos is None:
                await ctx.send("Du musst eine Position wählen!")
    
            if not gameOver:
                mark = ""
                if turn == ctx.author:
                    if turn == player1:
                        mark = ":regional_indicator_x:"
                    elif turn == player2:
                        mark = "?️"
                    if 0 < pos < 10:
                        if board[pos - 1] == ":white_large_square:":
                            board[pos - 1] = mark
                            count += 1
    
                            line = ""
                            for x in range(len(board)):
                                if x == 2 or x == 5 or x == 8:
                                    line += " " + board[x]
                                    await ctx.send(line)
                                    line = ""
                                else:
                                    line += " " + board[x]
    
                            checkWinner(winningC, mark)
                            if gameOver:
                                await ctx.send(f"{mark} hat gewonnen!")
                            elif count >= 9:
                                await ctx.send("Unentschieden")
    
                            if turn == player1:
                                turn = player2
                            elif turn == player2:
                                turn = player1
    
                        else:
                            await ctx.send("Dieses Feld ist bereits belegt!")
                    else:
                        await ctx.send("Du musst ein Feld zwischen 1 und 9 wählen!")
                else:
                    await ctx.send("Du bist noch nicht dran!")
            else:
                await ctx.send("Du musst zu erst ein neues Spiel starten!")
    
        @commands.command(aliases=["tttj", "jointtt"])
        async def tttjoin(self, ctx):
            global p2
            global player2
            global p2name
            global p2check
            global turn
    
            print("Hiii")
            if p2check is True:
                print("Something")
                p2 = ctx.author
                p2check = False
                p2name = ctx.author.name
    
                if p2check == False:
                    print("YES")
                    player2 = p2
                    num = random.randint(1, 2)
                    if num == 1:
                        turn = player1
                        await ctx.send(f"{player1.mention} fängt an!")
                    elif num == 2:
                        turn = player2
                        await ctx.send(f"{player2.mention} fängt an!")
                        return
    
        @commands.command()
        @commands.has_role("Admin")
        async def tttquit(self, ctx):
            global gameOver
            global p2
            p2 = True
            gameOver = True
            await ctx.send("Das Spiel wurde beendet!")```
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-09
      • 2021-05-22
      • 2021-02-28
      • 1970-01-01
      • 2021-06-04
      • 1970-01-01
      • 2021-03-16
      • 2021-07-25
      相关资源
      最近更新 更多