简体   繁体   中英

Pygame program doesn't always quit after pressing the red cross

I am trying to make a simple tic tac toe game using Pygame. While the game is being played, it sometimes requires more than 1 press of the "red cross" to exit the game.

while playing is True:
    clock.tick(30)
    if round_number % 2 == 1:
        current_player = 1
    else:
        current_player = 2

    while True:
        pygame.event.get()
        if pygame.event.get(eventtype=pygame.QUIT):
            pygame.quit()
            exit()
        if pygame.mouse.get_pressed()[0] == 1:
            break

    while True:
        pygame.event.get()
        if pygame.mouse.get_pressed()[0] == 0:
            break

    place_object(current_player, pygame.mouse.get_pos())
    print(round_number)
    pygame.display.update()
    if 7 < pygame.mouse.get_pos()[0] < 393 and 7 < pygame.mouse.get_pos()[1] < 393:
        round_number += 1

    if round_number >= 3:
        if check_win(board) != "No winner" and check_win(board) != "tie":
            message = f"The winner is {check_win(board)}!"
            message_display(message)
            pygame.display.update()
            playing = False
        elif check_win(board) == "tie":
            message_display("Game is tied...")
            pygame.display.update()
            playing = False

pygame.event.get() returns a list of events and remove them from the queue.
You've to handle the list in a loop, otherwise you'll miss events:

eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM