Keyboard movement

This commit is contained in:
Joseph Ferano 2023-10-28 17:22:25 +07:00
parent 6d128b840f
commit 2dc9f89431

View File

@ -5,6 +5,7 @@ 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)
clock = pg.time.Clock() clock = pg.time.Clock()
running = True 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) cursorSurface = pg.Surface((CURSOR_SIZE, CURSOR_SIZE), pg.SRCALPHA)
# Not used for now # Not used for now
padding = 0 padding = 0
cursor = v2(5, 5) cursor_pos = v2(5, 5)
def draw_grid(): def draw_grid():
for row in range(9): for row in range(9):
@ -44,8 +45,8 @@ def draw_grid():
def draw_cursor(): def draw_cursor():
pad_diff = (CURSOR_SIZE - CELL_SIZE) // 2 pad_diff = (CURSOR_SIZE - CELL_SIZE) // 2
pg.draw.rect(cursorSurface, (100, 0, 155, 100), [0, 0, CURSOR_SIZE, CURSOR_SIZE]) 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, rect = Rect(CELL_SIZE * (cursor_pos.x - 1) + GRID_X - pad_diff,
CELL_SIZE * (cursor.y - 1) + GRID_Y - pad_diff, CELL_SIZE * (cursor_pos.y - 1) + GRID_Y - pad_diff,
CURSOR_SIZE, CURSOR_SIZE) CURSOR_SIZE, CURSOR_SIZE)
screen.blit(cursorSurface, rect) screen.blit(cursorSurface, rect)
@ -53,16 +54,17 @@ while running:
for event in pg.event.get(): for event in pg.event.get():
if event.type == pg.QUIT: if event.type == pg.QUIT:
running = False running = False
keys = pg.key.get_pressed() if event.type == pg.KEYDOWN:
keys = pg.key.get_pressed()
if keys[pg.K_w]: if keys[pg.K_w] and cursor_pos.y > 1:
cursor = -1 cursor_pos.y -= 1
if keys[pg.K_s]: if keys[pg.K_s] and cursor_pos.y < 9:
dir.y = 1 cursor_pos.y += 1
if keys[pg.K_a]: if keys[pg.K_a] and cursor_pos.x > 1:
dir.x = -1 cursor_pos.x -= 1
if keys[pg.K_d]: if keys[pg.K_d] and cursor_pos.x < 9:
dir.x = 1 cursor_pos.x += 1
# playerPos += dir * (speed * dt) # playerPos += dir * (speed * dt)