Add a default puzzle to avoid init http request. WASD. Clean up a bit.

This commit is contained in:
Joseph Ferano 2023-10-29 14:59:45 +07:00
parent ffc9b69906
commit b7d96b8e53
2 changed files with 50 additions and 13 deletions

View File

@ -2,13 +2,17 @@ import pygame as pg
from pygame import Rect, Vector2 as v2 from pygame import Rect, Vector2 as v2
from dataclasses import dataclass from dataclasses import dataclass
import os import os
from websudoku import get_sudoku_puzzle, Difficulty from websudoku import *
@dataclass @dataclass
class Cursor: class Cursor:
row: int = 5 row: int = 5
col: int = 5 col: int = 5
##################
# PYGAME INIT
##################
oswinx, oswiny = 1940,50 oswinx, oswiny = 1940,50
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (oswinx,oswiny) os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (oswinx,oswiny)
pg.init() pg.init()
@ -16,19 +20,28 @@ pg.init()
screen = pg.display.set_mode((900, 900)) screen = pg.display.set_mode((900, 900))
pg.display.set_caption("Pydoku") pg.display.set_caption("Pydoku")
pg.key.set_repeat(200, 35) 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() clock = pg.time.Clock()
running = True 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 CELL_SIZE = 60
CURSOR_SIZE = CELL_SIZE + 6 CURSOR_SIZE = CELL_SIZE + 6
GRID_X = screen.get_width() / 2 - 9 * CELL_SIZE // 2 GRID_X = screen.get_width() / 2 - 9 * CELL_SIZE // 2
GRID_Y = screen.get_height() / 2 - 9 * CELL_SIZE // 2 GRID_Y = screen.get_height() / 2 - 9 * CELL_SIZE // 2
cursorSurface = pg.Surface((CURSOR_SIZE, CURSOR_SIZE), pg.SRCALPHA) cursorSurface = pg.Surface((CURSOR_SIZE, CURSOR_SIZE), pg.SRCALPHA)
# Not used for now
padding = 0
cursor = Cursor() cursor = Cursor()
@ -45,11 +58,11 @@ for i in range(81):
def draw_grid(): def draw_grid():
for row in range(9): for row in range(9):
for col in range(9): for col in range(9):
rect = Rect(CELL_SIZE * col + padding * col + GRID_X, rect = Rect(CELL_SIZE * col + GRID_X,
CELL_SIZE * row + padding * row + GRID_Y, CELL_SIZE * row + GRID_Y,
CELL_SIZE, CELL_SIZE) CELL_SIZE, CELL_SIZE)
rect = Rect(CELL_SIZE * col + padding * col + GRID_X, rect = Rect(CELL_SIZE * col + GRID_X,
CELL_SIZE * row + padding * row + GRID_Y, CELL_SIZE * row + GRID_Y,
CELL_SIZE, CELL_SIZE) CELL_SIZE, CELL_SIZE)
pg.draw.rect(screen, "white", rect) pg.draw.rect(screen, "white", rect)
@ -107,15 +120,16 @@ while running:
and my <= grid_max_y): and my <= grid_max_y):
cursor.row = int((my - GRID_Y) // CELL_SIZE + 1) cursor.row = int((my - GRID_Y) // CELL_SIZE + 1)
cursor.col = int((mx - GRID_X) // CELL_SIZE + 1) cursor.col = int((mx - GRID_X) // CELL_SIZE + 1)
if event.type == pg.KEYDOWN: if event.type == pg.KEYDOWN:
# keys = pg.key.get_pressed() # 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 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 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 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 cursor.col += 1
num = pg.key.name(event.key) num = pg.key.name(event.key)

View File

@ -17,6 +17,29 @@ class Difficulty(Enum):
host = "five.websudoku.com" 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: def get_sudoku_puzzle(difficulty: Difficulty) -> Sudoku:
"""Makes a web request to websudoku.com to grab a sudoku puzzle """Makes a web request to websudoku.com to grab a sudoku puzzle