From bd16bac7632988512692f66aa27687d0cac127dc Mon Sep 17 00:00:00 2001 From: brandonedgarton <61728761+brandonedgarton@users.noreply.github.com> Date: Wed, 24 Apr 2024 00:08:04 -0800 Subject: [PATCH] Added a feature to play multiple games w/o exiting --- tictactoe.py | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/tictactoe.py b/tictactoe.py index 058c302..1b26fda 100644 --- a/tictactoe.py +++ b/tictactoe.py @@ -25,12 +25,13 @@ LINE_COLOR = (23, 145, 135) CIRCLE_COLOR = (239, 231, 200) CROSS_COLOR = (66, 66, 66) +FONT_COLOR = (255, 0, 0) # ------ # SCREEN # ------ screen = pygame.display.set_mode( (WIDTH, HEIGHT) ) -pygame.display.set_caption( 'TIC TAC TOE' ) +pygame.display.set_caption( 'TIC TAC TOE (Press y to play again or n to quit!)' ) screen.fill( BG_COLOR ) # ------------- @@ -142,6 +143,37 @@ def restart(): for row in range(BOARD_ROWS): for col in range(BOARD_COLS): board[row][col] = 0 + return False + +# Pygame font initialization (if not already included) +pygame.font.init() + +def ask_play_again(): + # Ensures screen is clear before displaying the message + screen.fill(BG_COLOR) + draw_lines() + + # Using a common system font if specific one fails + font = pygame.font.SysFont('comicsans', 32) # 'None' can be replaced with 'comicsans' for a commonly available font + + text = font.render('Play again? (Y/N)', True, FONT_COLOR) + text_rect = text.get_rect(center=(WIDTH // 2, HEIGHT // 2)) + screen.blit(text, text_rect) + pygame.display.update() + + waiting_for_input = True + while waiting_for_input: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + sys.exit() + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_y: + restart() + return True # Continue playing + elif event.key == pygame.K_n: + pygame.quit() + sys.exit() # Exit the game draw_lines() @@ -157,6 +189,7 @@ def restart(): while True: for event in pygame.event.get(): if event.type == pygame.QUIT: + pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN and not game_over: @@ -177,10 +210,12 @@ def restart(): draw_figures() if event.type == pygame.KEYDOWN: - if event.key == pygame.K_r: - restart() - player = 1 - game_over = False + if event.key == pygame.K_y: + game_over = restart() + #player = 1 + elif event.key == pygame.K_n: + pygame.quit() + sys.exit() pygame.display.update()