from __future__ import annotations import os import pygame from data.constants import FPS, GAME_TITLE, SCREEN_H, SCREEN_W from engine.audio import SoundEffects from engine.flow import GameFlow from render.renderer import Renderer _KEY_INTENTS: dict[int, str] = { pygame.K_UP: "up", pygame.K_k: "up", pygame.K_DOWN: "down", pygame.K_j: "down", pygame.K_LEFT: "left", pygame.K_h: "left", pygame.K_RIGHT: "right", pygame.K_l: "right", pygame.K_RETURN: "confirm", pygame.K_KP_ENTER: "confirm", pygame.K_ESCAPE: "cancel", } _CHAR_INTENTS: dict[str, str] = { "g": "pickup", "f": "descend", "i": "inventory", "e": "equipment", "q": "quaff", "r": "read", "S": "save", "?": "help", "m": "mute", } def _intent_for(event: pygame.event.Event) -> str | None: if event.key in _KEY_INTENTS: return _KEY_INTENTS[event.key] char = event.unicode return _CHAR_INTENTS.get(char, char if char and char.isprintable() else None) def main(max_frames: int | None = None) -> None: pygame.init() screen = pygame.display.set_mode((SCREEN_W, SCREEN_H)) pygame.display.set_caption(GAME_TITLE) clock = pygame.time.Clock() renderer = Renderer(screen) flow = GameFlow(audio=SoundEffects()) frames = 0 if max_frames is None and os.environ.get("UNDERDEPTHS_MAX_FRAMES"): max_frames = int(os.environ["UNDERDEPTHS_MAX_FRAMES"]) try: while flow.running and (max_frames is None or frames < max_frames): dt = clock.tick(FPS) / 1000.0 for event in pygame.event.get(): if event.type == pygame.QUIT: flow.running = False elif event.type == pygame.KEYDOWN: intent = _intent_for(event) if intent is not None: flow.handle_key(intent) renderer.draw(flow, dt) frames += 1 finally: pygame.quit() if __name__ == "__main__": main()