Files
tetri5/tetris/util.py
2021-07-07 14:49:04 -04:00

104 lines
4.2 KiB
Python

import yaml
import pygame
from typing import KeysView, Tuple
"""
TODO description
"""
class ConfigurationManager:
CONFIG_FILE_LOCATION = "config.yaml"
_configuration = []
@classmethod
def init(cls) -> None:
with open(cls.CONFIG_FILE_LOCATION, "r") as yaml_file:
cls._configuration = yaml.safe_load(yaml_file)
@classmethod
def get(cls, key: str, sub_key: str = None):
if sub_key:
return cls._configuration[key][sub_key]
else:
return cls._configuration[key]
"""
TODO description
"""
class TextGenerator:
_sheet = None
_glyph_size = (-1, -1)
_characters = { }
@classmethod
def init(cls, file: str, glyph_size: Tuple) -> None:
cls._sheet = pygame.image.load(file)
cls._glyph_size = glyph_size
# load character positions in bitmap into the characters dictionary
# letters
cls._characters["A"] = (9 * glyph_size[0], 2 * glyph_size[1])
cls._characters["B"] = (10 * glyph_size[0], 2 * glyph_size[1])
cls._characters["C"] = (11 * glyph_size[0], 2 * glyph_size[1])
cls._characters["D"] = (0 * glyph_size[0], 3 * glyph_size[1])
cls._characters["E"] = (1 * glyph_size[0], 3 * glyph_size[1])
cls._characters["F"] = (2 * glyph_size[0], 3 * glyph_size[1])
cls._characters["G"] = (3 * glyph_size[0], 3 * glyph_size[1])
cls._characters["H"] = (4 * glyph_size[0], 3 * glyph_size[1])
cls._characters["I"] = (5 * glyph_size[0], 3 * glyph_size[1])
cls._characters["J"] = (6 * glyph_size[0], 3 * glyph_size[1])
cls._characters["K"] = (7 * glyph_size[0], 3 * glyph_size[1])
cls._characters["L"] = (8 * glyph_size[0], 3 * glyph_size[1])
cls._characters["M"] = (9 * glyph_size[0], 3 * glyph_size[1])
cls._characters["N"] = (10 * glyph_size[0], 3 * glyph_size[1])
cls._characters["O"] = (11 * glyph_size[0], 3 * glyph_size[1])
cls._characters["P"] = (0 * glyph_size[0], 4 * glyph_size[1])
cls._characters["Q"] = (1 * glyph_size[0], 4 * glyph_size[1])
cls._characters["R"] = (2 * glyph_size[0], 4 * glyph_size[1])
cls._characters["S"] = (3 * glyph_size[0], 4 * glyph_size[1])
cls._characters["T"] = (4 * glyph_size[0], 4 * glyph_size[1])
cls._characters["U"] = (5 * glyph_size[0], 4 * glyph_size[1])
cls._characters["V"] = (6 * glyph_size[0], 4 * glyph_size[1])
cls._characters["W"] = (7 * glyph_size[0], 4 * glyph_size[1])
cls._characters["X"] = (8 * glyph_size[0], 4 * glyph_size[1])
cls._characters["Y"] = (9 * glyph_size[0], 4 * glyph_size[1])
cls._characters["Z"] = (10 * glyph_size[0], 4 * glyph_size[1])
# numbers
cls._characters["0"] = (4 * glyph_size[0], 1 * glyph_size[1])
cls._characters["1"] = (5 * glyph_size[0], 1 * glyph_size[1])
cls._characters["2"] = (6 * glyph_size[0], 1 * glyph_size[1])
cls._characters["3"] = (7 * glyph_size[0], 1 * glyph_size[1])
cls._characters["4"] = (8 * glyph_size[0], 1 * glyph_size[1])
cls._characters["5"] = (9 * glyph_size[0], 1 * glyph_size[1])
cls._characters["6"] = (10 * glyph_size[0], 1 * glyph_size[1])
cls._characters["7"] = (11 * glyph_size[0], 1 * glyph_size[1])
cls._characters["8"] = (0 * glyph_size[0], 2 * glyph_size[1])
cls._characters["9"] = (1 * glyph_size[0], 2 * glyph_size[1])
@classmethod
def draw(cls, text: str, position: Tuple, surface: pygame.Surface) -> None:
x_position = 0
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]
"""
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]