From a58865eba826a80fc46711a3076f5df102b2497f Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Thu, 18 Apr 2024 18:24:41 +0200 Subject: [PATCH] Pixelized it (And made it way faster) --- engine.py | 73 +++++++++++++++-------------- model_viewer.py | 22 +++++---- models/model_properties.py | 7 --- {models => sprites}/coffee_cup.png | Bin sprites/sprite_properties.json | 14 ++++++ 5 files changed, 65 insertions(+), 51 deletions(-) delete mode 100644 models/model_properties.py rename {models => sprites}/coffee_cup.png (100%) create mode 100644 sprites/sprite_properties.json diff --git a/engine.py b/engine.py index c4cda76..de2eec3 100644 --- a/engine.py +++ b/engine.py @@ -1,48 +1,44 @@ #!/usr/bin/python3 -import tools.pg -from tools.pg import crop_surface import pygame +from dataclasses import dataclass +from wobbl_tools import pg +from wobbl_tools import data_file class Object: - def __init__(self, model_path: str, model_properties_path: str=None, sprite_spacing: int=1, size_multiplier: int=1): - self.model_path = model_path - - if model_properties_path is None: - self.model_properties_path = self.get_directory(model_path) + "/model_properties" - print(self.model_properties_path) - - else: - self.model_properties_path = model_properties_path - + def __init__(self, sprite_path: str="sprites/coffee_cup.png", sprite_spacing: int=1, rotation: int=45, size_multiplier: int=16): + self.sprite_path = sprite_path self.sprite_spacing = sprite_spacing + self.rotation = rotation self.size_multiplier = size_multiplier - self.rotation = 0 - self.model_name = self.get_filename(model_path) - self.model_properties = __import__(self.model_properties_path.replace("/", "."), fromlist=self.get_directory(model_path).replace("/", ".")).properties[self.model_name] - self.sprite_size = self.model_properties["sprite_size"] + self.sprite_size = tuple(get_json(get_dir(sprite_path) + "/sprite_properties.json")[get_filename(sprite_path)]["size"]) - self.sprites = self.generate_sprites() - self.original_sprites = self.sprites + self.original_sprites = self.get_sprites() + self.sprites = self.rotate_sprites() self.surface = self.generate_surface() - def get_directory(self, file_path): - return file_path.rsplit("/", 1)[0] + print(self.sprite_size) - def get_filename(self, file_path): - return file_path.rsplit("/", 1)[1] - - def generate_sprites(self): - model = pygame.image.load(self.model_path) + 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 = tools.pg.crop_surface(model, (x, 0), (sw, sh)) - sprite = pygame.transform.scale(sprite, (sprite.get_width() * self.size_multiplier, sprite.get_height() * self.size_multiplier)) + sprite = pg.crop_surface(model, (x, 0), (sw, sh)) + sprites.append(sprite) + + return sprites + + def rotate_sprites(self): + sprites = [] + + for sprite in self.original_sprites: + sprite = pygame.transform.rotate(sprite, self.rotation) + sprites.append(sprite) return sprites @@ -58,21 +54,26 @@ class Object: 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)) + return surface - def rotate_model(self): - sprites = [] - for sprite in self.original_sprites: - sprite = pygame.transform.rotate(sprite, self.rotation) +def get_dir(path: str): + return path.rsplit("/", 1)[0] - sprites.append(sprite) - return sprites +def get_filename(path: str): + return path.rsplit("/", 1)[1] - def blit(self, surface, position): - surface.blit(self.surface, position) + +def get_json(path: str): + file = open(path, "r") + data = eval(file.read()) + file.close() + + return data if __name__ == "__main__": - pass + bla = Object() diff --git a/model_viewer.py b/model_viewer.py index aad049c..ce138fa 100644 --- a/model_viewer.py +++ b/model_viewer.py @@ -15,7 +15,8 @@ class ModelViewer: self.window_size = window_size self.fps = fps self.rotating = True - self.model = engine.Object("models/coffee_cup.png", size_multiplier=30) + self.model = engine.Object(size_multiplier=16) + self.rotation_cooldown = 5 self.clock = pygame.time.Clock() @@ -30,19 +31,24 @@ class ModelViewer: return if self.rotating: - if self.model.rotation < 360: - self.model.rotation += 1 + if self.rotation_cooldown == 0: + if self.model.rotation < 360: + self.model.rotation += 1 + + else: + self.model.rotation = 0 + + self.model.sprites = self.model.rotate_sprites() + self.model.surface = self.model.generate_surface() + self.rotation_cooldown = 1 else: - self.model.rotation = 0 - - self.model.sprites = self.model.rotate_model() - self.model.surface = self.model.generate_surface() + self.rotation_cooldown -= 1 w, h = self.screen.get_size() mw, mh = self.model.surface.get_size() - self.model.blit(self.screen, (w / 2 - mw / 2, h / 2 - mh / 2)) + self.screen.blit(self.model.surface, (w // 2 - mw // 2, h // 2 - mh // 2)) pygame.display.update() self.clock.tick(self.fps) diff --git a/models/model_properties.py b/models/model_properties.py deleted file mode 100644 index 7db84ef..0000000 --- a/models/model_properties.py +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/python3 - -properties = { - "coffee_cup.png": { - "sprite_size": (7, 10) - } -} diff --git a/models/coffee_cup.png b/sprites/coffee_cup.png similarity index 100% rename from models/coffee_cup.png rename to sprites/coffee_cup.png diff --git a/sprites/sprite_properties.json b/sprites/sprite_properties.json new file mode 100644 index 0000000..1d033d4 --- /dev/null +++ b/sprites/sprite_properties.json @@ -0,0 +1,14 @@ +{ + "coffee_cup.png": { + "size": [7, 10] + }, + "creeper.png": { + "size": [10, 16] + }, + "plane_01.png": { + "size": [70, 78] + }, + "robot_01.png": { + "size": [38, 16] + } +}