/
ap1k
/
PyGame_Practice
Обзор
Документация
Войти
/
ap1k
/
PyGame_Practice
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/utils/resource_loader.py
142 строки
5 KB
Roman
The all game
27 май 2025, 15:26
27 май 2025, 15:26
4ea7de9
Код
Авторство
О чём код?
""" ResourceLoader class - handles loading and managing game resources Implements the Singleton pattern to ensure only one loader instance exists """ import pygame import os class ResourceLoader: """ Resource loader class implementing Singleton pattern Responsible for loading and storing game resources """ _instance = None def __new__(cls): if cls._instance is None: cls._instance = super(ResourceLoader, cls).__new__(cls) cls._instance._initialized = False return cls._instance def __init__(self): if self._initialized: return # Resource dictionaries self.images = {} self.sounds = {} self.fonts = {} # Base paths self.image_path = os.path.join("src", "assets", "images") self.sound_path = os.path.join("src", "assets", "sounds") self._initialized = True def load_all(self): """Load all game resources""" self._load_images() self._load_sounds() self._load_fonts() def _load_images(self): """Load all game images""" # Define the images to load image_files = { "player": "player_ship.png", "enemy_basic": "enemy_basic.png", "enemy_fast": "enemy_fast.png", "enemy_tank": "enemy_tank.png", "bullet_basic": "bullet_basic.png", "bullet_rapid": "bullet_rapid.png", "bullet_power": "bullet_power.png", "background": "background.png", } # Create placeholder images if the files don't exist for key, filename in image_files.items(): full_path = os.path.join(self.image_path, filename) # Check if the image file exists if os.path.exists(full_path): # Load the image self.images[key] = pygame.image.load(full_path).convert_alpha() else: # Create a placeholder image if key == "background": # Create a black background with some stars self.images[key] = self._create_background() elif "enemy" in key: # Create a red rectangle for enemies self.images[key] = self._create_placeholder(30, 30, (255, 0, 0)) elif "bullet" in key: # Create a small yellow rectangle for bullets self.images[key] = self._create_placeholder(5, 10, (255, 255, 0)) else: # Create a blue rectangle for the player self.images[key] = self._create_placeholder(40, 40, (0, 0, 255)) def _load_sounds(self): """Load all game sounds""" # Define the sounds to load sound_files = { "shoot": "shoot.wav", "explosion": "explosion.wav", "powerup": "powerup.wav", } # Try to load the sounds, but don't crash if they don't exist for key, filename in sound_files.items(): full_path = os.path.join(self.sound_path, filename) # Check if the sound file exists if os.path.exists(full_path): # Load the sound self.sounds[key] = pygame.mixer.Sound(full_path) else: # Create a dummy sound object self.sounds[key] = pygame.mixer.Sound(buffer=bytes(0)) def _load_fonts(self): """Load all game fonts""" # Use the default pygame font for now self.fonts["small"] = pygame.font.Font(None, 24) self.fonts["medium"] = pygame.font.Font(None, 36) self.fonts["large"] = pygame.font.Font(None, 48) def _create_placeholder(self, width, height, color): """Create a placeholder surface with the given dimensions and color""" surface = pygame.Surface((width, height), pygame.SRCALPHA) surface.fill(color) return surface def _create_background(self): """Create a simple starfield background""" import random # Create a black background surface = pygame.Surface((800, 600)) surface.fill((0, 0, 0)) # Add some stars for _ in range(100): x = random.randint(0, 799) y = random.randint(0, 599) radius = random.randint(1, 2) brightness = random.randint(100, 255) color = (brightness, brightness, brightness) pygame.draw.circle(surface, color, (x, y), radius) return surface def get_image(self, key): """Get an image by key""" return self.images.get(key) def get_sound(self, key): """Get a sound by key""" return self.sounds.get(key) def get_font(self, key): """Get a font by key""" return self.fonts.get(key)