refactor: improve keyboard input logic

This commit is contained in:
Giovani Rodriguez
2021-06-14 13:17:18 -04:00
parent f52f218a5f
commit bc8721e077
3 changed files with 49 additions and 40 deletions

View File

@@ -1,6 +1,6 @@
import yaml
import pygame
from typing import Tuple
from typing import KeysView, Tuple
"""
TODO description
@@ -83,4 +83,23 @@ class TextGenerator:
for char_ in text:
if not char_.isspace():
surface.blit(cls.sheet, (position[0] + x_position, position[1]), pygame.Rect(cls.characters[char_.upper()], (cls.glyph_size[0], cls.glyph_size[1])))
x_position += cls.glyph_size[0]
x_position += cls.glyph_size[0]
"""
TODO description
"""
class Controller:
keys_pressed = {}
@classmethod
def key_down(cls, key: int) -> bool:
prior_pressed_state = False if key not in cls.keys_pressed else cls.keys_pressed[key]
cls.keys_pressed[key] = pygame.key.get_pressed()[key]
return cls.keys_pressed[key] and not prior_pressed_state
@classmethod
def key_pressed(cls, key: int) -> bool:
return pygame.key.get_pressed()[key]