Pygame 模块在程序暂停时识别点击 pygame.time.delay(x)

Pygame module recognises clicks while the program is paused with pygame.time.delay(x)

我目前正在学习使用模块“pygame”编写我的第一个 Python 游戏。我已经在视频教程的帮助下编写了一个太空飞船射击游戏,现在我正在自己编写一个 Tic-Tac-Toe 游戏。我已经设法做到了这一点,而我现在要做的是添加两种不同的游戏模式(玩家对玩家/玩家对 AI)。但这不是我遇到的问题。当代码检测到有人赢了(或平局)时,将显示一条获胜消息,我正在使用 pygame.time.delay(2000) 在下一轮开始前将该消息显示 2 秒。但是当我在那段时间点击某个地方时,一旦消息消失并且下一轮开始,我点击的地方就会设置一个标记。 我已经尝试过不仅在回合结束时重置棋盘,还在下回合开始时重置棋盘,但这并没有改变任何东西

def main():
    global current_player, cross_score, circle_score, game_started, game_mode

    reset_cells()

    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                quit()

            for rectangle in RECTANGLES:
                if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:          # checking for left mouse-button
                    clicked = bool(rectangle.collidepoint(pygame.mouse.get_pos()))
                    if clicked:
                        for cell in CELLS:
                            if cell.x == rectangle.x and cell.y == rectangle.y and cell.content == '' and game_started:
                                if game_mode == 'ai' and current_player == 'Cross' or game_mode == 'multiplayer':
                                    cell.content = current_player
                                    current_player = 'Circle' if current_player == 'Cross' else 'Cross'
                                    print(cell.x, cell.y)

            if not game_started and event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:    # checking if a button on the title screen has been pressed
                clicked_multiplayer = bool(TITLE_BUTTON_MULTIPLAYER_BORDER.collidepoint(pygame.mouse.get_pos()))
                clicked_ai = bool(TITLE_BUTTON_AI_BORDER.collidepoint(pygame.mouse.get_pos()))

                if clicked_multiplayer:
                    game_mode, game_started = 'multiplayer', True
                elif clicked_ai:
                    game_mode, game_started = 'ai', True

            if game_started and event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                game_started = False
                game_mode = None
                reset_cells()
                cross_score, circle_score = 0, 0

            if game_started and game_mode == 'ai' and current_player == 'Circle' and win_check()[0] == '':
                cell = get_computer_move()
                cell.content = 'Circle'
                current_player = 'Cross'

        winner = win_check()
        win_text = ''
        if winner[0] == 'Cross':
            win_text = 'Cross wins!'
            cross_score += 1
        elif winner[0] == 'Circle':
            win_text = 'Circle wins!'
            circle_score += 1
        elif winner[0] == 'Tie':
            win_text = "It's a tie!"
            draw_window(game_started)
            draw_winner(win_text)
            break

        if win_text != '':
            draw_window(game_started)
            try:                           
                pygame.draw.rect(WIN, RED, get_win_indicating_line(winner[1]))
            except TypeError:
                WIN.blit(get_win_indicating_line(winner[1]), (0, 0))
            draw_winner(win_text)
            break

        draw_window(game_started)

    main()

调用pygame.time.delay(2000)时,事件仍然存储在事件队列中,然后由pygame.event.get()使用和清空。
为了忽略 2 秒内发生的任何事件,您可以在 2 秒延迟后直接调用 pygame.event.get()。我建议仍然检查此 pygame.event.get() 中是否有 pygame.QUIT 事件,因为此事件也会被丢弃。