Basic status/message, show completion time

This commit is contained in:
Joseph Ferano 2023-11-03 21:23:35 +07:00
parent 913e4b8858
commit cd09420da5
2 changed files with 42 additions and 16 deletions

View File

@ -3,6 +3,7 @@ from pygame import Rect, Vector2 as v2
from dataclasses import dataclass, field from dataclasses import dataclass, field
from enum import Enum from enum import Enum
from typing import Set from typing import Set
import time
import threading import threading
import sys import sys
@ -39,6 +40,17 @@ class InputMethod(Enum):
CENTER = 2, CENTER = 2,
BORDER = 3 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 # PYGAME INIT
@ -57,6 +69,7 @@ clock = pg.time.Clock()
# FONTS # FONTS
################## ##################
title_font = pg.font.Font(None, 64) title_font = pg.font.Font(None, 64)
status_font = pg.font.Font(None, 44)
filled_num_font = pg.font.Font(None, 60) filled_num_font = pg.font.Font(None, 60)
btn_font = pg.font.Font(None, 28) btn_font = pg.font.Font(None, 28)
pm_border_font = pg.font.Font(None, 18) pm_border_font = pg.font.Font(None, 18)
@ -79,6 +92,9 @@ board = []
pencil_marks = [] pencil_marks = []
sudoku = None sudoku = None
start_time = [0]
game_state = GameState()
data_lock = threading.Lock() data_lock = threading.Lock()
def data_callback(sudoku_data): def data_callback(sudoku_data):
@ -94,6 +110,9 @@ def data_callback(sudoku_data):
pencil_marks.append(pm) pencil_marks.append(pm)
global sudoku global sudoku
with data_lock: with data_lock:
game_state.start_time = time.time()
game_state.status = Status.SOLVING
game_state.status_msg = ""
sudoku = sudoku_data sudoku = sudoku_data
@ -198,21 +217,28 @@ def draw_numbers():
def draw_hud(): def draw_hud():
screen.blit(title, (screen.get_width() // 2 - title.get_width() // 2, 70)) 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(): 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): for i,cell in enumerate(board):
if cell.value == 0: if cell.value == 0:
print("You still have to finish the puzzle!") game_state.status_msg = "You still have to finish the puzzle!"
return return
if cell.value != sudoku.solution[i]: completion_time = time.time() - game_state.start_time
print("You made a mistake!") mins = int(completion_time // 60)
return secs = int(completion_time % 60)
print("You completed it!") game_state.status_msg = f"You finished it in {mins} min {secs} sec!"
game_state.status = Status.COMPLETED
def main(): def main():
running = True running = True
input_method = InputMethod.FILL input_method = InputMethod.FILL
while running: while running:
for event in pg.event.get(): for event in pg.event.get():
if event.type == pg.QUIT: if event.type == pg.QUIT:
@ -234,13 +260,13 @@ def main():
if btn.rect.collidepoint(event.pos): if btn.rect.collidepoint(event.pos):
match btn.id: match btn.id:
case "check": case "check":
if game_state.status == Status.SOLVING:
check_board() check_board()
case "load": case "load":
pass pass
if event.type == pg.KEYDOWN: if event.type == pg.KEYDOWN:
# keys = pg.key.get_pressed()
if (event.key == pg.K_h or event.key == pg.K_a): if (event.key == pg.K_h or event.key == pg.K_a):
cursor.col = (cursor.col - 1) % 9 cursor.col = (cursor.col - 1) % 9
if (event.key == pg.K_k or event.key == pg.K_w): if (event.key == pg.K_k or event.key == pg.K_w):

View File

@ -18,16 +18,16 @@
* DONE Wrap around the board * DONE Wrap around the board
* DONE Delete center pencil marks 1 num at a time * DONE Delete center pencil marks 1 num at a time
* DONE Pass difficulty through command line * DONE Pass difficulty through command line
* TODO Button to load a new puzzle * DONE Toast on valid solve
* TODO Show errors * DONE Show the time it took
* TODO Choose difficulty buttons * TODO Pausing functionality
* TODO Toast on valid solve
* TODO Show the time it took
* TODO Save current game
* TODO Create a pip file
* TODO Show current input method * TODO Show current input method
* TODO Show keybindings somehow * 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 Support undo?
* TODO Sudoku generator * TODO Sudoku generator
* TODO Sudoku solver * TODO Sudoku solver