fork download
  1. def draw_status():
  2. """Draws the status bar."""
  3. global is_draw
  4. if current_winner is None:
  5. message = f"{current_player.upper()}'s Turn"
  6. elif is_draw:
  7. message = "Game Draw!"
  8. else:
  9. message = f"{current_winner.upper()} Won!"
  10. font = pg.font.Font(None, 36)
  11. text = font.render(message, True, (255, 255, 255))
  12. screen.fill((0, 0, 0), (0, 400, WIDTH, 100))
  13. text_rect = text.get_rect(center=(WIDTH // 2, HEIGHT + 50))
  14. screen.blit(text, text_rect)
  15. pg.display.update()
  16.  
  17. def check_win():
  18. """Checks game grid for wins or draws."""
  19. global grid, current_winner, is_draw
  20. for row in range(3):
  21. if grid[row][0] == grid[row][1] == grid[row][2] and grid[row][0]:
  22. current_winner = grid[row][0]
  23. pg.draw.line(screen, (250, 0, 0), (0, (row + 0.5) * HEIGHT // 3), (WIDTH, (row + 0.5) * HEIGHT // 3), 5)
  24. break
  25. for col in range(3):
  26. if grid[0][col] == grid[1][col] == grid[2][col] and grid[0][col]:
  27. current_winner = grid[0][col]
  28. pg.draw.line(screen, (250, 0, 0), ((col + 0.5) * WIDTH // 3, 0), ((col + 0.5) * WIDTH // 3, HEIGHT), 5)
  29. break
  30. if grid[0][0] == grid[1][1] == grid[2][2] and grid[0][0]:
  31. current_winner = grid[0][0]
  32. pg.draw.line(screen, (250, 0, 0), (0, 0), (WIDTH, HEIGHT), 5)
  33. if grid[0][2] == grid[1][1] == grid[2][0] and grid[0][2]:
  34. current_winner = grid[0][2]
  35. pg.draw.line(screen, (250, 0, 0), (WIDTH, 0), (0, HEIGHT), 5)
  36. if all(cell for row in grid for cell in row) and not current_winner:
  37. is_draw = True
  38. draw_status()
  39.  
  40. def reset_game():
  41. """Restarts the game on win or draw."""
  42. global grid, current_player, current_winner, is_draw
  43. time.sleep(10) # Pause for 10 seconds
  44. current_player = 'x'
  45. current_winner = None
  46. is_draw = False
  47. grid = [[None] * 3 for _ in range(3)]
  48. game_initiating_window()
  49.  
Success #stdin #stdout 0.03s 9652KB
stdin
Standard input is empty
stdout
Standard output is empty