"""Rasterize palette-key grids to cached pygame Surfaces. ``get_sprite(name)`` returns the native-size cached Surface; the renderer scales it to ``TILE`` via ``get_tile_sprite``. All art is procedural (pygame.draw / set_at from in-code palettes); no external image files are loaded. """ from __future__ import annotations from typing import Any import pygame from data import sprites as SPRITE_DATA from data.constants import PALETTE, TILE _NATIVE: dict[str, pygame.Surface] = {} _SCALED: dict[str, pygame.Surface] = {} def _color(ch: str, tint: tuple[int, int, int] | None) -> tuple[int, int, int] | None: if ch == "a": return tint if tint is not None else (200, 200, 200) return PALETTE.get(ch) def _rasterize(grid: list[str], tint: tuple[int, int, int] | None) -> pygame.Surface: h = len(grid) w = max((len(row) for row in grid), default=1) w = max(1, w) surf = pygame.Surface((w, h), pygame.SRCALPHA) for y, row in enumerate(grid): for x, ch in enumerate(row): c = _color(ch, tint) if c is not None: surf.set_at((x, y), (c[0], c[1], c[2], 255)) return surf def _convert(surf: pygame.Surface) -> pygame.Surface: try: return surf.convert_alpha() except Exception: return surf # no display yet (e.g. constructed before set_mode): use raw def _make_dragon() -> pygame.Surface: """Procedural 2x2 boss dragon: ONE connected silhouette (wings+body+head+tail drawn in the same red with no internal outlines, so it reads as a single creature, not separate blobs), then details layered on top.""" size = TILE * 2 s = pygame.Surface((size, size), pygame.SRCALPHA) K = (16, 14, 20); n = (140, 42, 42); r = (208, 66, 54); H = (172, 54, 54) h = (235, 235, 235); y = (246, 206, 86); amber = (222, 156, 60) def poly(col, pts): pygame.draw.polygon(s, col, pts) def ell(col, cx, cy, rw, rh): pygame.draw.ellipse(s, col, pygame.Rect(cx - rw, cy - rh, rw * 2, rh * 2)) # ---- single connected red silhouette: wings sweep up-out, body+head capsule ---- poly(r, [(20, 16), (5, 7), (2, 15), (7, 20), (13, 17), (20, 21)]) # left wing poly(r, [(20, 16), (35, 7), (38, 15), (33, 20), (27, 17), (20, 21)]) # right wing ell(r, 20, 22, 7, 13) # body + head capsule poly(r, [(17, 34), (23, 34), (27, 39), (13, 39)]) # tail # wing membrane shading (darker), inside the wings only poly(n, [(16, 15), (6, 9), (4, 14), (9, 18), (13, 16)]) poly(n, [(24, 15), (34, 9), (36, 14), (31, 18), (27, 16)]) pygame.draw.line(s, H, (20, 17), (6, 9), 1) # finger bones pygame.draw.line(s, H, (20, 17), (13, 16), 1) pygame.draw.line(s, H, (20, 17), (34, 9), 1) pygame.draw.line(s, H, (20, 17), (27, 16), 1) # ---- details ---- for sx in (16, 20, 24): # dorsal spines poly(H, [(sx - 1, 10), (sx + 1, 10), (sx, 5)]) ell(y, 20, 28, 4, 5) # fiery belly for by in (26, 29, 32): pygame.draw.line(s, amber, (17, by), (23, by), 1) poly(h, [(15, 10), (10, 2), (13, 2), (17, 9)]) # horns poly(h, [(25, 10), (30, 2), (27, 2), (23, 9)]) pygame.draw.line(s, H, (14, 14), (17, 14), 1) # brows pygame.draw.line(s, H, (23, 14), (26, 14), 1) pygame.draw.circle(s, y, (17, 16), 2) # eyes pygame.draw.circle(s, y, (23, 16), 2) pygame.draw.line(s, K, (17, 15), (17, 17), 1) pygame.draw.line(s, K, (23, 15), (23, 17), 1) pygame.draw.circle(s, K, (19, 19), 1) # nostrils pygame.draw.circle(s, K, (21, 19), 1) poly(h, [(18, 18), (17, 21), (19, 19)]) # fangs poly(h, [(22, 18), (23, 21), (21, 19)]) for fx in (16, 24): # claws pygame.draw.line(s, h, (fx - 2, 35), (fx - 2, 38), 1) pygame.draw.line(s, h, (fx, 35), (fx, 38), 1) pygame.draw.line(s, h, (fx + 2, 35), (fx + 2, 38), 1) return s def get_sprite(name: str) -> pygame.Surface: """Native-size cached sprite for ``name`` (transparent background).""" if name in _NATIVE: return _NATIVE[name] if name == "ancient_dragon": surf = _convert(_make_dragon()) _NATIVE[name] = surf return surf grid = SPRITE_DATA.GRIDS.get(name) if grid is None: grid = SPRITE_DATA.GRIDS["player"] # safe fallback tint = SPRITE_DATA.TINTS.get(name) surf = _convert(_rasterize(grid, tint)) _NATIVE[name] = surf return surf def get_tile_sprite(name: str, size: int = TILE) -> pygame.Surface: """Sprite scaled to ``size`` (cached per name+size).""" key = f"{name}@{size}" if key in _SCALED: return _SCALED[key] native = get_sprite(name) scaled = pygame.transform.scale(native, (size, size)) _SCALED[key] = scaled return scaled def clear_cache() -> None: _NATIVE.clear() _SCALED.clear() if __name__ == "__main__": pygame.init() pygame.display.set_mode((16, 16)) s = get_sprite("stairs") opaque = yellow = 0 for yy in range(s.get_height()): for xx in range(s.get_width()): c = s.get_at((xx, yy)) if c.a == 0: continue opaque += 1 if c.r > 120 and c.g > 100 and c.b < 110: yellow += 1 print(f"stairs {s.get_size()} opaque={opaque} yellow={yellow}") assert opaque > 0 and yellow > 0, "stairs sprite not yellow/opaque" for n in ("player", "rat", "goblin", "skeleton_archer", "orc_warrior", "wraith", "green_slime", "ancient_dragon", "healing_potion", "war_hammer"): g = get_sprite(n) assert g.get_width() >= 4 and g.get_height() >= 4, n print("sprites self-check: OK") pygame.quit()