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

32 lines
1.1 KiB
Python

import pygame
from pygame.event import Event
class KeyEvent:
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]
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, event: Event):
self.event = event
def is_key_down(self, key: int) -> bool:
return self.event.type == pygame.KEYDOWN and self.event.key == key
def is_key_up(self, key: int) -> bool:
return self.event.type == pygame.KEYUP and self.event.key == key
def is_keymap_down(self, keys: list[int]) -> bool:
return self.event.type == pygame.KEYDOWN and self.event.key in keys
def is_keymap_up(self, keys: list[int]) -> bool:
return self.event.type == pygame.KEYUP and self.event.key in keys
@staticmethod
def create_events(event: list[Event]) -> list['KeyEvent']:
return [KeyEvent(e) for e in event if e.type == pygame.KEYDOWN or e.type == pygame.KEYUP]