From cd09420da5dacf21631936955722dbb8414c334a Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Fri, 3 Nov 2023 21:23:35 +0700 Subject: [PATCH] Basic status/message, show completion time --- pydoku.py | 42 ++++++++++++++++++++++++++++++++++-------- pydoku.todo | 16 ++++++++-------- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/pydoku.py b/pydoku.py index 63d735e..98374fd 100644 --- a/pydoku.py +++ b/pydoku.py @@ -3,6 +3,7 @@ from pygame import Rect, Vector2 as v2 from dataclasses import dataclass, field from enum import Enum from typing import Set +import time import threading import sys @@ -39,6 +40,17 @@ class InputMethod(Enum): CENTER = 2, BORDER = 3 +class Status(Enum): + FETCHING = 1, + SOLVING = 2, + COMPLETED = 3 + + +@dataclass +class GameState: + start_time: float = 0 + status: Status = Status.FETCHING + status_msg: str = "" ################## # PYGAME INIT @@ -57,6 +69,7 @@ clock = pg.time.Clock() # FONTS ################## title_font = pg.font.Font(None, 64) +status_font = pg.font.Font(None, 44) filled_num_font = pg.font.Font(None, 60) btn_font = pg.font.Font(None, 28) pm_border_font = pg.font.Font(None, 18) @@ -79,6 +92,9 @@ board = [] pencil_marks = [] sudoku = None +start_time = [0] +game_state = GameState() + data_lock = threading.Lock() def data_callback(sudoku_data): @@ -94,6 +110,9 @@ def data_callback(sudoku_data): pencil_marks.append(pm) global sudoku with data_lock: + game_state.start_time = time.time() + game_state.status = Status.SOLVING + game_state.status_msg = "" sudoku = sudoku_data @@ -198,21 +217,28 @@ def draw_numbers(): def draw_hud(): screen.blit(title, (screen.get_width() // 2 - title.get_width() // 2, 70)) + status_txt = status_font.render(game_state.status_msg, True, "black") + screen.blit(status_txt, (screen.get_width() // 2 - status_txt.get_width() // 2, + screen.get_height() - status_txt.get_height() - 70)) def check_board(): + for i,cell in enumerate(board): + if cell.value != 0 and cell.value != sudoku.solution[i]: + game_state.status_msg = "You made a mistake!" + return for i,cell in enumerate(board): if cell.value == 0: - print("You still have to finish the puzzle!") + game_state.status_msg = "You still have to finish the puzzle!" return - if cell.value != sudoku.solution[i]: - print("You made a mistake!") - return - print("You completed it!") + completion_time = time.time() - game_state.start_time + mins = int(completion_time // 60) + secs = int(completion_time % 60) + game_state.status_msg = f"You finished it in {mins} min {secs} sec!" + game_state.status = Status.COMPLETED def main(): running = True input_method = InputMethod.FILL - while running: for event in pg.event.get(): if event.type == pg.QUIT: @@ -234,13 +260,13 @@ def main(): if btn.rect.collidepoint(event.pos): match btn.id: case "check": - check_board() + if game_state.status == Status.SOLVING: + check_board() case "load": pass if event.type == pg.KEYDOWN: - # keys = pg.key.get_pressed() if (event.key == pg.K_h or event.key == pg.K_a): cursor.col = (cursor.col - 1) % 9 if (event.key == pg.K_k or event.key == pg.K_w): diff --git a/pydoku.todo b/pydoku.todo index ad48d44..c8cc285 100644 --- a/pydoku.todo +++ b/pydoku.todo @@ -18,16 +18,16 @@ * DONE Wrap around the board * DONE Delete center pencil marks 1 num at a time * DONE Pass difficulty through command line -* TODO Button to load a new puzzle -* TODO Show errors -* TODO Choose difficulty buttons -* TODO Toast on valid solve -* TODO Show the time it took -* TODO Save current game -* TODO Create a pip file +* DONE Toast on valid solve +* DONE Show the time it took +* TODO Pausing functionality * TODO Show current input method * TODO Show keybindings somehow -* TODO Pause button +* TODO Show errors +* TODO Button to load a new puzzle +* TODO Choose difficulty buttons +* TODO Save current game +* TODO Create a pip file * TODO Support undo? * TODO Sudoku generator * TODO Sudoku solver