pydoku/pydoku.py
2023-10-28 17:22:25 +07:00

82 lines
2.6 KiB
Python

import pygame as pg
from pygame import Rect, Vector2 as v2
pg.init()
screen = pg.display.set_mode((900, 900))
pg.display.set_caption("Pydoku")
pg.key.set_repeat(200, 35)
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_pos = v2(5, 5)
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_pos.x - 1) + GRID_X - pad_diff,
CELL_SIZE * (cursor_pos.y - 1) + GRID_Y - pad_diff,
CURSOR_SIZE, CURSOR_SIZE)
screen.blit(cursorSurface, rect)
while running:
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
if event.type == pg.KEYDOWN:
keys = pg.key.get_pressed()
if keys[pg.K_w] and cursor_pos.y > 1:
cursor_pos.y -= 1
if keys[pg.K_s] and cursor_pos.y < 9:
cursor_pos.y += 1
if keys[pg.K_a] and cursor_pos.x > 1:
cursor_pos.x -= 1
if keys[pg.K_d] and cursor_pos.x < 9:
cursor_pos.x += 1
# playerPos += dir * (speed * dt)
screen.fill('cornflower blue')
draw_grid()
draw_cursor()
pg.display.flip()
dt = clock.tick(60) / 1000
pg.quit()