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 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):

View File

@ -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