Generalize button code to add a new button

This commit is contained in:
Joseph Ferano 2023-10-31 11:22:24 +07:00
parent dc03796275
commit d4e5be4a7d
2 changed files with 30 additions and 11 deletions

View File

@ -24,6 +24,12 @@ class PencilMark:
center: Set[int] = field(default_factory=set)
border: Set[int] = field(default_factory=set)
@dataclass
class Button:
id: str
txt: pg.Surface
rect: Rect
class InputMethod(Enum):
"""
This enum helps representing Snyder notation in the cells
@ -148,14 +154,22 @@ def draw_pm_center(row, col, idx):
GRID_Y + CELL_SIZE * row + CELL_SIZE // 2 - digits.get_height() // 2.3)
screen.blit(digits, pos)
done_txt = btn_font.render("Check", True, "black")
done_rect = Rect(screen.get_width() - 10 - 100, 10, 100, 30)
def new_button(id, label, color, x, y, w, h):
return Button(id,
btn_font.render(label, True, color),
Rect(x, y, w, h))
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),
]
def draw_buttons():
pg.draw.rect(screen, "gray", done_rect)
pg.draw.rect(screen, "black", done_rect, width=2)
screen.blit(done_txt,
(done_rect.x + done_rect.w // 2 - done_txt.get_width() // 2,
done_rect.y + done_rect.h // 2 - done_txt.get_height() // 2))
for btn in buttons:
pg.draw.rect(screen, "gray", btn.rect)
pg.draw.rect(screen, "black", btn.rect, width=2)
screen.blit(btn.txt,
(btn.rect.x + btn.rect.w // 2 - btn.txt.get_width() // 2,
btn.rect.y + btn.rect.h // 2 - btn.txt.get_height() // 2))
def draw_numbers():
for row in range(9):
@ -209,8 +223,13 @@ while running:
cursor.row = int((my - GRID_Y) // CELL_SIZE + 1)
cursor.col = int((mx - GRID_X) // CELL_SIZE + 1)
if done_rect.collidepoint(event.pos):
check_board()
for btn in buttons:
if btn_rect.collidepoint(event.pos):
match btn_rect.id:
case "check":
check_board()
case "load":
pass
if event.type == pg.KEYDOWN:

View File

@ -14,11 +14,11 @@
* DONE Check if puzzle is complete
* DONE Improve pencil mark colors
* DONE Async http request
* DONE Generalized button code
* TODO Button to load a new puzzle
* TODO Show errors
* TODO Generalized button code
* TODO Pass difficulty through command line
* TODO Choose difficulty buttons
* TODO Button to load a new puzzle
* TODO Delete center pencil marks 1 num at a time
* TODO Toast on valid solve
* TODO Show the time it took