sep-pm-platformer/project/ui_elements/KeyManager.py

32 lines
1000 B
Python

import pygame
from pygame.event import Event
class KeyManager:
KEY_LEFT: list[int] = [pygame.K_LEFT, pygame.K_a]
KEY_RIGHT: list[int] = [pygame.K_RIGHT, pygame.K_d]
KEY_UP: list[int] = [pygame.K_UP, pygame.K_w, pygame.K_SPACE]
KEY_DOWN: list[int] = [pygame.K_DOWN, pygame.K_s]
KEY_SPACE: list[int] = [pygame.K_SPACE]
KEY_ESCAPE: list[int] = [pygame.K_ESCAPE]
KEY_ENTER: list[int] = [pygame.K_RETURN, pygame.K_KP_ENTER]
def __init__(self):
self.down = []
def update_key_events(self, events: list[Event]):
for event in events:
if event.type == pygame.KEYDOWN:
self.down.append(event.key)
elif event.type == pygame.KEYUP:
self.down.remove(event.key)
def is_key_down(self, key: int) -> bool:
return key in self.down
def is_keymap_down(self, keys: list[int]) -> bool:
for key in keys:
if key in self.down:
return True
return False