From 2dc9f894315c131a14d912940a4f401c7572e1ab Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sat, 28 Oct 2023 17:22:25 +0700 Subject: [PATCH] Keyboard movement --- pydoku.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/pydoku.py b/pydoku.py index 4ee0309..af41d2d 100644 --- a/pydoku.py +++ b/pydoku.py @@ -5,6 +5,7 @@ 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 @@ -16,7 +17,7 @@ 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) +cursor_pos = v2(5, 5) def draw_grid(): for row in range(9): @@ -44,8 +45,8 @@ def draw_grid(): 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, + 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) @@ -53,16 +54,17 @@ while running: for event in pg.event.get(): if event.type == pg.QUIT: running = False - keys = pg.key.get_pressed() + if event.type == pg.KEYDOWN: + 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 + 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)