sep-pm-platformer/project/SpritesheetManager.py

27 lines
920 B
Python

import pygame
import json
from Spritesheet import Spritesheet
# This class is used to load named sprite sheets from the img folder.
class SpritesheetManager:
def __init__(self, sprites_dir, config_file):
self.sprites_dir = sprites_dir
self.spritesheets = {}
self.load_from_config(config_file)
def load_from_config(self, config_file):
print("Loading sprites from sprite sheet config file", config_file)
config = json.load(open(config_file))
for sprite_data in config:
sprite_sheet = Spritesheet(self.sprites_dir, sprite_data)
self.spritesheets[sprite_sheet.sprite_id] = sprite_sheet
print("Loaded", len(self.spritesheets), "sprite sheet(s)")
def get_sheet(self, sheet):
if sheet not in self.spritesheets:
raise ValueError("Invalid/Missing sprite sheet " + sheet)
return self.spritesheets[sheet]