#!/usr/bin/python3 import pygame from wobbl_tools import pg class Model: def __init__(self, sprite_path: str="sprites/coffee_cup.png", size_multiplier: int=16, sprite_spacing: int=1, rotation: int=45): self.sprite_path = sprite_path self.sprite_spacing = sprite_spacing self.rotation = rotation self.size_multiplier = size_multiplier self.cache = {} self.sprite_size = tuple(get_json(get_dir(sprite_path) + "/sprite_properties.json")[get_filename(sprite_path)]["size"]) self.original_sprites = self.get_sprites() self.sprites = self.rotate_sprites() self.surface = self.generate_surface() print(self.sprite_size) def get_sprites(self): model = pygame.image.load(self.sprite_path) sw, sh = self.sprite_size mw, mh = model.get_size() sprites = [] for x in range(0, mw, sw): sprite = pg.crop_surface(model, (x, 0), (sw, sh)) sprites.append(sprite) return sprites def rotate_sprites(self): if self.rotation in self.cache: return self.original_sprites sprites = [] for sprite in self.original_sprites: sprite = pygame.transform.rotate(sprite, self.rotation) sprites.append(sprite) return sprites def generate_surface(self): if self.rotation in self.cache: return self.cache[self.rotation] sw, sh = self.sprites[0].get_size() surface = pygame.Surface((sw, sh + self.sprite_spacing * len(self.sprites)), flags=pygame.SRCALPHA) y = surface.get_height() - sh for sprite in self.sprites: surface.blit(sprite, (0, y)) y -= self.sprite_spacing surface = pygame.transform.scale(surface, (surface.get_width() * self.size_multiplier, surface.get_height() * self.size_multiplier)) self.cache[self.rotation] = surface return surface def update(self): return self.rotate_sprites(), self.generate_surface() class Object: def __init__(self, model: Model, position: tuple, rotation: int=45): self.model = model self.position = position self.rotation = rotation def update(self): self.model.update() def draw(self, surface): surface.blit(self.model.cache[self.rotation]) def get_dir(path: str): return path.rsplit("/", 1)[0] def get_filename(path: str): return path.rsplit("/", 1)[1] def get_json(path: str): file = open(path, "r") data = eval(file.read()) file.close() return data