From 6d128b840f8acf5c1f253f2a6a6ca7a6681c6ea4 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sat, 28 Oct 2023 17:01:59 +0700 Subject: [PATCH] Draw grid with a cursor --- pydoku.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 pydoku.py diff --git a/pydoku.py b/pydoku.py new file mode 100644 index 0000000..4ee0309 --- /dev/null +++ b/pydoku.py @@ -0,0 +1,79 @@ +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") +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 = 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.x - 1) + GRID_X - pad_diff, + CELL_SIZE * (cursor.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 + keys = pg.key.get_pressed() + + if keys[pg.K_w]: + cursor = -1 + if keys[pg.K_s]: + dir.y = 1 + if keys[pg.K_a]: + dir.x = -1 + if keys[pg.K_d]: + dir.x = 1 + + # playerPos += dir * (speed * dt) + + screen.fill('cornflower blue') + + draw_grid() + draw_cursor() + + + pg.display.flip() + + dt = clock.tick(60) / 1000 + +pg.quit()