Source code for game.utils.asset_loader

"""Asset loading utilities."""

import pygame
import os
from .constants import PACKAGE_DIR

[docs] class AssetLoader: """Handles loading and caching of game assets.""" def __init__(self):
[docs] self.loaded_images = {}
[docs] self.loaded_sounds = {}
[docs] def load_map_image(self, path, scale=None): """Load an image with optional scaling.""" if path in self.loaded_images: return self.loaded_images[path] try: full_path = os.path.join('', path) image = pygame.image.load(full_path).convert_alpha() if scale: image = pygame.transform.scale(image, scale) self.loaded_images[path] = image return image except pygame.error: print(f"Error loading image: {path}") # Return placeholder if image not found return self._create_placeholder_image(32, 32, (255, 0, 255))
[docs] def load_image(self, path, scale=None): """Load an image with optional scaling.""" if path in self.loaded_images: return self.loaded_images[path] try: full_path = PACKAGE_DIR / "assets" / "images" / path image = pygame.image.load(str(full_path)).convert_alpha() if scale: image = pygame.transform.scale(image, scale) self.loaded_images[path] = image return image except pygame.error: print(f"Error loading image: {path}") # Return placeholder if image not found return self._create_placeholder_image(32, 32, (255, 0, 255))
[docs] def load_sound(self, path): """Load a sound file.""" if path in self.loaded_sounds: return self.loaded_sounds[path] try: full_path = PACKAGE_DIR / "assets" / "sounds" / path sound = pygame.mixer.Sound(str(full_path)) self.loaded_sounds[path] = sound return sound except pygame.error: return None
def _create_placeholder_image(self, width, height, color): """Create a placeholder image.""" surface = pygame.Surface((width, height)) surface.fill(color) return surface