Add pausing functionality. Keybinding for "Check"

This commit is contained in:
Joseph Ferano 2023-11-15 19:39:35 +07:00
parent cd09420da5
commit c56d14bdf6
2 changed files with 43 additions and 7 deletions

View File

@ -32,6 +32,10 @@ class Button:
txt: pg.Surface
rect: Rect
# @dataclass
# class PlayerInput:
class InputMethod(Enum):
"""
This enum helps representing Snyder notation in the cells
@ -48,9 +52,10 @@ class Status(Enum):
@dataclass
class GameState:
start_time: float = 0
elapsed_time: float = 0
status: Status = Status.FETCHING
status_msg: str = ""
paused: bool = False
##################
# PYGAME INIT
@ -92,7 +97,6 @@ board = []
pencil_marks = []
sudoku = None
start_time = [0]
game_state = GameState()
data_lock = threading.Lock()
@ -110,7 +114,6 @@ 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
@ -180,12 +183,20 @@ def new_button(id, label, color, x, y, w, h):
return Button(id,
btn_font.render(label, True, color),
Rect(x, y, w, h))
pause_btns = [
new_button("pause", "Pause", "black", screen.get_width() - 10 - 150, 90, 150, 30),
new_button("pause", "Resume", "black", screen.get_width() - 10 - 150, 90, 150, 30),
]
buttons = [
new_button("check", "Check", "black", screen.get_width() - 10 - 100, 10, 100, 30),
new_button("load", "Load new", "black", screen.get_width() - 10 - 150, 50, 150, 30),
pause_btns[0],
]
def draw_buttons():
# God this is hacky
buttons[2] = pause_btns[1] if game_state.paused else pause_btns[0]
for btn in buttons:
pg.draw.rect(screen, "gray", btn.rect)
pg.draw.rect(screen, "black", btn.rect, width=2)
@ -198,6 +209,19 @@ def draw_numbers():
for col in range(9):
idx = row * 9 + col
cell = board[idx]
if game_state.paused:
if row == 4 and col > 1 and col < 7:
paused = "PAUSE"
letter = filled_num_font.render(paused[col - 2], True, "dark blue")
pos = (GRID_X + CELL_SIZE * col + CELL_SIZE // 2 - letter.get_width() // 2,
# Here we divide the height by 2.3 because there seems to be some extra
# padding at the bottom of the surface
GRID_Y + CELL_SIZE * row + CELL_SIZE // 2 - letter.get_height() // 2.3)
screen.blit(letter, pos)
continue
if cell.value > 0:
color = "black" if cell.given else "dark blue"
digit = filled_num_font.render(str(cell.value), True, color)
@ -221,6 +245,9 @@ def draw_hud():
screen.blit(status_txt, (screen.get_width() // 2 - status_txt.get_width() // 2,
screen.get_height() - status_txt.get_height() - 70))
def toggle_pause():
game_state.paused = not game_state.paused
def check_board():
for i,cell in enumerate(board):
if cell.value != 0 and cell.value != sudoku.solution[i]:
@ -230,7 +257,7 @@ def check_board():
if cell.value == 0:
game_state.status_msg = "You still have to finish the puzzle!"
return
completion_time = time.time() - game_state.start_time
completion_time = game_state.elapsed_time
mins = int(completion_time // 60)
secs = int(completion_time % 60)
game_state.status_msg = f"You finished it in {mins} min {secs} sec!"
@ -240,6 +267,7 @@ def main():
running = True
input_method = InputMethod.FILL
while running:
dt = clock.tick(60) / 1000
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
@ -262,6 +290,8 @@ def main():
case "check":
if game_state.status == Status.SOLVING:
check_board()
case "pause":
toggle_pause()
case "load":
pass
@ -311,6 +341,10 @@ def main():
input_method = InputMethod.CENTER
case pg.K_b:
input_method = InputMethod.BORDER
case pg.K_p:
toggle_pause()
case pg.K_RETURN:
check_board()
##############
# Debug stuff
@ -332,6 +366,8 @@ def main():
with data_lock:
if sudoku is not None:
draw_numbers()
if not game_state.paused:
game_state.elapsed_time += dt
draw_cursor()
draw_hud()
draw_buttons()
@ -339,7 +375,6 @@ def main():
pg.display.flip()
dt = clock.tick(60) / 1000
pg.quit()

View File

@ -20,7 +20,8 @@
* DONE Pass difficulty through command line
* DONE Toast on valid solve
* DONE Show the time it took
* TODO Pausing functionality
* DONE Keybinding to check so you don't have to use the mouse
* DONE Pausing functionality
* TODO Show current input method
* TODO Show keybindings somehow
* TODO Show errors
@ -28,7 +29,7 @@
* TODO Choose difficulty buttons
* TODO Save current game
* TODO Create a pip file
* TODO Support undo?
* TODO Support undo
* TODO Sudoku generator
* TODO Sudoku solver
* TODO Sudoku difficulty generator