From b7d96b8e5320104f77e812ba376ce2d11a7f9829 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sun, 29 Oct 2023 14:59:45 +0700 Subject: [PATCH] Add a default puzzle to avoid init http request. WASD. Clean up a bit. --- pydoku.py | 40 +++++++++++++++++++++++++++------------- websudoku.py | 23 +++++++++++++++++++++++ 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/pydoku.py b/pydoku.py index 565cfaa..425fec6 100644 --- a/pydoku.py +++ b/pydoku.py @@ -2,13 +2,17 @@ import pygame as pg from pygame import Rect, Vector2 as v2 from dataclasses import dataclass import os -from websudoku import get_sudoku_puzzle, Difficulty +from websudoku import * @dataclass class Cursor: row: int = 5 col: int = 5 + +################## +# PYGAME INIT +################## oswinx, oswiny = 1940,50 os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (oswinx,oswiny) pg.init() @@ -16,19 +20,28 @@ pg.init() screen = pg.display.set_mode((900, 900)) pg.display.set_caption("Pydoku") pg.key.set_repeat(200, 35) -title_font = pg.font.Font(None, 64) -filled_num_font = pg.font.Font(None, 48) + clock = pg.time.Clock() running = True +################## +# FONTS +################## +title_font = pg.font.Font(None, 64) +filled_num_font = pg.font.Font(None, 60) +pm_border_font = pg.font.Font(None, 18) +pm_center_font = pg.font.Font(None, 32) +title = title_font.render("Pydoku", True, "black") + +################## +# GAME +################## CELL_SIZE = 60 CURSOR_SIZE = CELL_SIZE + 6 GRID_X = screen.get_width() / 2 - 9 * CELL_SIZE // 2 GRID_Y = screen.get_height() / 2 - 9 * CELL_SIZE // 2 cursorSurface = pg.Surface((CURSOR_SIZE, CURSOR_SIZE), pg.SRCALPHA) -# Not used for now -padding = 0 cursor = Cursor() @@ -45,11 +58,11 @@ for i in range(81): def draw_grid(): for row in range(9): for col in range(9): - rect = Rect(CELL_SIZE * col + padding * col + GRID_X, - CELL_SIZE * row + padding * row + GRID_Y, + rect = Rect(CELL_SIZE * col + GRID_X, + CELL_SIZE * row + GRID_Y, CELL_SIZE, CELL_SIZE) - rect = Rect(CELL_SIZE * col + padding * col + GRID_X, - CELL_SIZE * row + padding * row + GRID_Y, + rect = Rect(CELL_SIZE * col + GRID_X, + CELL_SIZE * row + GRID_Y, CELL_SIZE, CELL_SIZE) pg.draw.rect(screen, "white", rect) @@ -107,15 +120,16 @@ while running: and my <= grid_max_y): cursor.row = int((my - GRID_Y) // CELL_SIZE + 1) cursor.col = int((mx - GRID_X) // CELL_SIZE + 1) + if event.type == pg.KEYDOWN: # keys = pg.key.get_pressed() - if event.key == pg.K_h and cursor.col > 1: + if (event.key == pg.K_h or event.key == pg.K_a) and cursor.col > 1: cursor.col -= 1 - if event.key == pg.K_k and cursor.row > 1: + if (event.key == pg.K_k or event.key == pg.K_w) and cursor.row > 1: cursor.row -= 1 - if event.key == pg.K_j and cursor.row < 9: + if (event.key == pg.K_j or event.key == pg.K_s) and cursor.row < 9: cursor.row += 1 - if event.key == pg.K_l and cursor.col < 9: + if (event.key == pg.K_l or event.key == pg.K_d) and cursor.col < 9: cursor.col += 1 num = pg.key.name(event.key) diff --git a/websudoku.py b/websudoku.py index 008a170..96238d3 100644 --- a/websudoku.py +++ b/websudoku.py @@ -17,6 +17,29 @@ class Difficulty(Enum): host = "five.websudoku.com" +def get_sudoku_puzzle_test(): + cheat = [ 1,7,6,5,9,4,3,2,8, + 3,5,8,6,7,2,9,1,4, + 2,9,4,8,1,3,6,5,7, + 7,3,1,9,2,6,8,4,5, + 6,4,5,1,3,8,7,9,2, + 9,8,2,4,5,7,1,3,6, + 5,6,3,2,8,9,4,7,1, + 8,2,9,7,4,1,5,6,3, + 4,1,7,3,6,5,2,8,9 ] + editmask = [ 1,0,1,0,0,1,0,1,1, + 1,0,1,1,0,1,1,0,1, + 0,1,1,1,1,1,0,1,0, + 1,1,1,1,1,0,1,1,1, + 0,1,1,0,1,0,1,1,0, + 1,1,1,0,1,1,1,1,1, + 0,1,0,1,1,1,1,1,0, + 1,0,1,1,0,1,1,0,1, + 1,1,0,1,0,0,1,0,1 ] + + return Sudoku(cheat, editmask) + + def get_sudoku_puzzle(difficulty: Difficulty) -> Sudoku: """Makes a web request to websudoku.com to grab a sudoku puzzle