Tic Tac Toe用pygame改变

问题描述 投票:0回答:2

所以我把这个代码放到游戏的主循环中,一切似乎都按照预期进行,除了转换,我不知道是什么问题我已经被困在这2天了,无法弄明白这里是链接到完整的脚本,以防任何人想要检查我是否在功能本身做错了https://github.com/AmrBinBashir/Tic-Tac-Toe-Pygame

run = True
while run:
    draw_board_onetime(board_boxes, win)
    make_board(board_boxes)
    board = [" "] * 10
    game_on = True
    while game_on:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

            if event.type == pygame.MOUSEMOTION:
                pos = pygame.mouse.get_pos()
                for box in board_boxes:
                    if box.is_hover(pos):
                        box.hovered = True
                        box.draw(win)
                    else:
                        box.hovered = False
                        box.draw(win)
            turn = "player1"
            if turn == "player1":
                if event.type == pygame.MOUSEBUTTONDOWN:
                    pos = pygame.mouse.get_pos()
                    for i in range(1, 10):
                        if board_boxes[i].is_hover(pos):
                            board_boxes[i].text = x_marker
                            board[i] = x_marker
                            print(board)
                            if check_win(board, x_marker):
                                game_on = False  # WIN
                            else:
                                if full_board_check(board):
                                    game_on = False  # TIE
                                else:
                                    turn = "player2"
            else:
                if event.type == pygame.MOUSEBUTTONDOWN:
                    pos = pygame.mouse.get_pos()
                    for i in range(1, 10):
                        if board_boxes[i].is_hover(pos):
                            board_boxes[i].text = o_marker
                            board[i] = o_marker
                            print(board)
                            if check_win(board, o_marker):
                                game_on = False  # WIN
                            else:
                                if full_board_check(board):
                                    game_on = False  # TIE
                                else:
                                    turn = "player1"

        pygame.display.update()
    sys.exit()
sys.exit()
python pygame tic-tac-toe
2个回答
2
投票

我不完全确定你的代码(我自己没有执行过),但我很确定压倒一切

turn = "player1"
#will always slip into:
        if turn == "player1":

将代码“player1”移到代码中的其他位置,例如用于测试的全局变量。现在似乎即使你把它设置为“player2”,它也会在下一次运行中被覆盖。


一点点额外的,善意的建议:字符串不能成为好的比较器。除非您喜欢可读性,否则请考虑使用布尔值或至少使用整数。稍后您将了解它们如何更加通用且不易出错(例如简单的拼写错误或意外大写)。

例如,布尔可以像player = not player一样轻松翻转(在True之间来回切换P1和False用于P2)。

整数可以很容易地像player_id += 1一样增加(对于有3名或更多玩家的棋盘游戏,它开始变得很方便)。


0
投票

解决方案,如果有人在功能中看到这一点,只需将turn变量移动到game_on循环之外,这样就不能在每个循环中覆盖它

run = True
while run:
    draw_board_onetime(board_boxes, win)
    make_board(board_boxes)
    board = [" "] * 10
    game_on = True
    turn = 'player1'
    while game_on:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

            if event.type == pygame.MOUSEMOTION:
                pos = pygame.mouse.get_pos()
                for box in board_boxes:
                    if box.is_hover(pos):
                        box.hovered = True
                        box.draw(win)
                    else:
                        box.hovered = False
                        box.draw(win)

            if turn == "player1":
                if event.type == pygame.MOUSEBUTTONDOWN:
                    pos = pygame.mouse.get_pos()
                    for i in range(1, 10):
                        if board_boxes[i].is_hover(pos):
                            board_boxes[i].text = x_marker
                            board[i] = x_marker
                            print(board)
                            if check_win(board, x_marker):
                                game_on = False  # WIN
                            else:
                                if full_board_check(board):
                                    game_on = False  # TIE
                                else:
                                    turn = "player2"
            else:
                if event.type == pygame.MOUSEBUTTONDOWN:
                    pos = pygame.mouse.get_pos()
                    for i in range(1, 10):
                        if board_boxes[i].is_hover(pos):
                            board_boxes[i].text = o_marker
                            board[i] = o_marker
                            print(board)
                            if check_win(board, o_marker):
                                game_on = False  # WIN
                            else:
                                if full_board_check(board):
                                    game_on = False  # TIE
                                else:
                                    turn = "player1"

        pygame.display.update()
    sys.exit()
sys.exit()
© www.soinside.com 2019 - 2024. All rights reserved.