pydoku/pydoku.py

150 lines
4.9 KiB
Python

import pygame as pg
from pygame import Rect, Vector2 as v2
from dataclasses import dataclass
import os
from websudoku import get_sudoku_puzzle, Difficulty
@dataclass
class Cursor:
row: int = 5
col: int = 5
oswinx, oswiny = 1940,50
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (oswinx,oswiny)
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
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()
sudoku = get_sudoku_puzzle(Difficulty.EVIL)
board = []
for i in range(81):
if sudoku.editmask[i] == 0:
board.append((sudoku.cheat[i], True))
else:
board.append((0, False))
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,
CELL_SIZE, CELL_SIZE)
rect = Rect(CELL_SIZE * col + padding * col + GRID_X,
CELL_SIZE * row + padding * row + GRID_Y,
CELL_SIZE, CELL_SIZE)
pg.draw.rect(screen, "white", rect)
pg.draw.rect(screen, "gray", rect, width=1)
for l in range(4):
pg.draw.line(screen, "black",
start_pos=(GRID_X, GRID_Y + CELL_SIZE * 3 * l),
end_pos =(GRID_X + CELL_SIZE * 9, GRID_Y + CELL_SIZE * 3 * l),
width=2)
for l in range(4):
pg.draw.line(screen, "black",
start_pos=(GRID_X + CELL_SIZE * 3 * l, GRID_Y),
end_pos =(GRID_X + CELL_SIZE * 3 * l, GRID_Y + CELL_SIZE * 9),
width=2)
def draw_cursor():
pad_diff = (CURSOR_SIZE - CELL_SIZE) // 2
pg.draw.rect(cursorSurface, (100, 0, 155, 100), [0, 0, CURSOR_SIZE, CURSOR_SIZE])
rect = Rect(CELL_SIZE * (cursor.col - 1) + GRID_X - pad_diff,
CELL_SIZE * (cursor.row - 1) + GRID_Y - pad_diff,
CURSOR_SIZE, CURSOR_SIZE)
screen.blit(cursorSurface, rect)
filled_num_font = pg.font.Font(None, 60)
def draw_numbers():
for row in range(9):
for col in range(9):
num = board[row * 9 + col][0]
if num == 0:
continue
digit = filled_num_font.render(str(num), True, "black")
pos = (GRID_X + CELL_SIZE * col + CELL_SIZE // 2 - digit.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 - digit.get_height() // 2.3)
screen.blit(digit, pos)
title = title_font.render("Pydoku", True, "black")
def draw_hud():
screen.blit(title, (screen.get_width() // 2 - title.get_width() // 2, 70))
while running:
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
if event.type == pg.MOUSEBUTTONDOWN:
if event.button == 1:
grid_max_x = GRID_X + CELL_SIZE * 9
grid_max_y = GRID_Y + CELL_SIZE * 9
mx = event.pos[0]
my = event.pos[1]
if (mx >= GRID_X
and mx <= grid_max_x
and my >= GRID_Y
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:
cursor.col -= 1
if event.key == pg.K_k and cursor.row > 1:
cursor.row -= 1
if event.key == pg.K_j and cursor.row < 9:
cursor.row += 1
if event.key == pg.K_l and cursor.col < 9:
cursor.col += 1
num = pg.key.name(event.key)
if num.isdigit() and num != '0':
num = int(num)
idx = (cursor.row - 1) * 9 + cursor.col - 1
if not board[idx][1]:
board[idx] = (num, False)
if event.key == pg.K_BACKSPACE:
idx = (cursor.row - 1) * 9 + cursor.col - 1
if not board[idx][1]:
board[idx] = (0, False)
if event.key == pg.K_F1:
print(sudoku)
screen.fill('cornflower blue')
draw_grid()
draw_numbers()
draw_cursor()
draw_hud()
pg.display.flip()
dt = clock.tick(60) / 1000
pg.quit()