sep-pm-platformer/project/sprite/Spritesheet.py

34 lines
1.2 KiB
Python

import pygame
class Spritesheet:
def __init__(self, sprites_dir, sprite_data):
self.sprite_id = sprite_data["id"]
self.subsheets = sprite_data["subsheets"]
self.sheet_file = sprites_dir + "/" + self.sprite_id + ".png"
self.sheet = pygame.image.load(self.sheet_file).convert_alpha()
self.animations = {}
self.load_animations()
def load_animations(self):
subsheet_y = 0
for subsheet_data in self.subsheets:
subsheet_id = subsheet_data["id"]
delays = subsheet_data["delays"]
subsheet_width = subsheet_data["width"]
subsheet_height = subsheet_data["height"]
subsheet_x = 0
subsheet_images = []
for i in range(len(delays)):
subsheet_image = pygame.Surface((subsheet_width, subsheet_height), pygame.SRCALPHA)
subsheet_image.blit(self.sheet, (0, 0), (subsheet_x, subsheet_y, subsheet_width, subsheet_height))
subsheet_images.append(subsheet_image)
subsheet_x += subsheet_width
subsheet_y += subsheet_height
self.animations[subsheet_id] = {"images": subsheet_images, "delays": delays}