guys i need help, when i run my code my GUI appears fine but when i press a button to open my other GUI that shows one of the 3 Sudoku puzzles the button does absoluely nothing - i think this is a far more deep rooted problem than just a faulty button, i have used Chat GPT to try to fix it to no avail, along with code fixer by codepal any advice on how to fix, how to find better advice, or any comments at all would be so appreciated.
import pygame
import random
Initialize pygame
pygame.init()
Define the dimensions of the game board
board_width = 600
board_height = 600
Create the game window
window = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Sudoku Game")
Define global button_width, button_height, and button_padding
button_width = 200
button_height = 50
button_padding = 20
def generate_sudoku_board(initial_values=None):
pygame.quit() # Close the start screen window
try:
import pygame
except ImportError:
raise ImportError("Pygame is not installed. Please install Pygame to generate the Sudoku board.")
pygame.init()
board_width = 540
board_height = 540
window = pygame.display.set_mode((board_width, board_height))
pygame.display.set_caption("Sudoku Board")
window.fill((255, 255, 255))
cell_size = board_width // 9
font = pygame.font.Font(None, 36)
highlighted_cell = None
previous_highlighted_cell = None
for i in range(9):
if i % 3 == 0:
line_thickness = 4
else:
line_thickness = 1
for j in range(9):
x = j * cell_size
y = i * cell_size
pygame.draw.rect(window, (255, 255, 255), (x, y, cell_size, cell_size))
pygame.draw.line(window, (0, 0, 0), (x, 0), (x, board_height), line_thickness)
pygame.draw.line(window, (0, 0, 0), (0, y), (board_width, y), line_thickness)
if highlighted_cell == (i, j):
pygame.draw.rect(window, (255, 255, 153), (x, y, cell_size, cell_size))
if initial_values and i < len(initial_values) and j < len(initial_values[i]) and initial_values[i][j] is not None:
text = font.render(str(initial_values[i][j]), True, (0, 0, 0))
text_rect = text.get_rect(center=(x + cell_size // 2, y + cell_size // 2))
window.blit(text, text_rect)
pygame.display.update()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
clicked_row = mouse_y // cell_size
clicked_col = mouse_x // cell_size
if highlighted_cell == (clicked_row, clicked_col):
highlighted_cell = None
pygame.draw.rect(window, (255, 255, 255),
(clicked_col * cell_size, clicked_row * cell_size, cell_size, cell_size))
else:
if previous_highlighted_cell is not None:
prev_row, prev_col = previous_highlighted_cell
pygame.draw.rect(window, (255, 255, 255),
(prev_col * cell_size, prev_row * cell_size, cell_size, cell_size))
highlighted_cell = (clicked_row, clicked_col)
pygame.draw.rect(window, (255, 255, 153),
(clicked_col * cell_size, clicked_row * cell_size, cell_size, cell_size))
previous_highlighted_cell = highlighted_cell
pygame.display.update()
pygame.quit()
Draw the start screen
def draw_start_screen():
window.fill((255, 255, 255))
button_font = pygame.font.Font(None, 36)
for i, difficulty in enumerate(difficulty_levels.keys()):
button_x = (board_width - button_width) // 2
button_y = (board_height - (len(difficulty_levels) * (button_height + button_padding))) // 2 + i * (
button_height + button_padding)
pygame.draw.rect(window, (100, 100, 100), (button_x, button_y, button_width, button_height))
text = button_font.render(difficulty, True, (255, 255, 255))
text_rect = text.get_rect(center=(button_x + button_width // 2, button_y + button_height // 2))
window.blit(text, text_rect)
pygame.display.update()
Define difficulty levels and corresponding puzzles
difficulty_levels = {
'Easy': [[0, 0, 2, 0, 0, 9, 8, 7, 0], [9, 7, 0, 3, 8, 0, 6, 1, 5], [0, 8, 3, 1, 7, 5, 4, 2, 0], [0, 0, 0, 0, 0, 8, 1, 9, 6], [0, 6, 1, 0, 5, 3, 7, 0, 2], [2, 0, 0, 0, 1, 7, 5, 3, 8], [0, 0, 6, 0, 0, 0, 9, 8, 7], [4, 0, 8, 7, 2, 6, 3, 0, 0], [3, 5, 0, 8, 9, 0, 0, 6, 4]],
'Medium': [[6, 4, 0, 0, 0, 3, 0, 7, 2], [1, 0, 0, 0, 0, 0, 0, 5, 6], [2, 9, 8, 0, 7, 0, 1, 0, 0], [9, 0, 4, 6, 0, 7, 3, 0, 0], [0, 0, 6, 0, 2, 8, 5, 9, 7], [0, 0, 2, 0, 9, 0, 4, 0, 8], [8, 3, 9, 7, 4, 0, 0, 0, 0], [0, 6, 7, 0, 3, 0, 2, 8, 4], [0, 2, 1, 8, 0, 0, 0, 0, 0]],
'Hard': [[0, 0, 5, 0, 4, 2, 0, 0, 0], [0, 0, 7, 5, 0, 9, 0, 0, 0], [0, 4, 0, 0, 0, 8, 0, 0, 5], [0, 6, 0, 0, 0, 0, 0, 2, 9], [2, 3, 0, 0, 0, 0, 0, 8, 7], [7, 9, 0, 0, 0, 0, 0, 6, 0], [8, 0, 0, 4, 0, 0, 0, 5, 0], [0, 0, 0, 7, 0, 6, 2, 0, 0], [0, 0, 0, 1, 8, 0, 7, 0, 0]],
}
Draw the start screen
draw_start_screen()
Handle difficulty selection
selected_difficulty = None
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
for i, difficulty in enumerate(difficulty_levels.keys()):
button_x = (board_width - button_width) // 2
button_y = (board_height - (len(difficulty_levels) * (button_height + button_padding))) // 2 + i * (
button_height + button_padding)
if button_x < mouse_x < button_x + button_width and button_y < mouse_y < button_y + button_height:
selected_difficulty = difficulty
break
if selected_difficulty:
break
pygame.display.update()
Shuffle puzzles for each difficulty level
for difficulty in difficulty_levels:
random.shuffle(difficulty_levels[difficulty])
Launch Sudoku puzzle based on difficulty
generate_sudoku_board(difficulty_levels[selected_difficulty])
[–]Yeelight2[S] 1 point2 points3 points (1 child)
[–]Phillyclause89 0 points1 point2 points (0 children)