From 5b5594ae40a3cd9a73b15ab6f0ae4746b137c48f Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Sat, 3 Feb 2024 17:08:51 +0100 Subject: [PATCH] Got it working. --- engine.py | 78 ++++++++++++++++++++++++++++++++ model_viewer.py | 88 +++++++++++++++++++++++++++++++++++++ models/coffee_cup.png | Bin 0 -> 281 bytes models/model_properties.py | 7 +++ 4 files changed, 173 insertions(+) create mode 100644 engine.py create mode 100644 model_viewer.py create mode 100644 models/coffee_cup.png create mode 100644 models/model_properties.py diff --git a/engine.py b/engine.py new file mode 100644 index 0000000..c4cda76 --- /dev/null +++ b/engine.py @@ -0,0 +1,78 @@ +#!/usr/bin/python3 +import tools.pg +from tools.pg import crop_surface +import pygame + + +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 + + self.sprite_spacing = sprite_spacing + 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.sprites = self.generate_sprites() + self.original_sprites = self.sprites + self.surface = self.generate_surface() + + def get_directory(self, file_path): + return file_path.rsplit("/", 1)[0] + + def get_filename(self, file_path): + return file_path.rsplit("/", 1)[1] + + def generate_sprites(self): + model = pygame.image.load(self.model_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)) + sprites.append(sprite) + + return sprites + + def generate_surface(self): + 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 + + return surface + + def rotate_model(self): + sprites = [] + + for sprite in self.original_sprites: + sprite = pygame.transform.rotate(sprite, self.rotation) + + sprites.append(sprite) + + return sprites + + def blit(self, surface, position): + surface.blit(self.surface, position) + + +if __name__ == "__main__": + pass diff --git a/model_viewer.py b/model_viewer.py new file mode 100644 index 0000000..1b597cb --- /dev/null +++ b/model_viewer.py @@ -0,0 +1,88 @@ +#!/usr/bin/python3 + +import pygame +import engine + + +class ModelViewer: + def __init__(self, window_size: tuple=(1200, 800), fps: int=60): + pygame.init() + self.screen = pygame.display.set_mode(window_size, flags=pygame.RESIZABLE) + + self.gray = (20, 20, 20) + + self.window_size = window_size + self.fps = fps + self.rotating = True + self.model = engine.Object("models/coffee_cup.png", size_multiplier=20) + + self.clock = pygame.time.Clock() + + self.running = True + + def loop(self): + self.screen.fill(self.gray) + + self.get_events() + + if not self.running: + return + + if self.rotating: + if self.model.rotation < 360: + self.model.rotation += 1 + + else: + self.model.rotation = 0 + + self.model.sprites = self.model.rotate_model() + self.model.surface = self.model.generate_surface() + + 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)) + + pygame.display.update() + self.clock.tick(self.fps) + + def get_events(self): + for event in pygame.event.get(): + if event.type == pygame.QUIT: + self.quit() + + elif event.type == pygame.KEYDOWN: + if event.key == pygame.K_UP: + self.model.sprite_spacing += 1 + self.model.surface = self.model.generate_surface() + + elif event.key == pygame.K_DOWN: + self.model.sprite_spacing -= 1 + self.model.surface = self.model.generate_surface() + + elif event.key == pygame.K_RIGHT: + if not self.rotating: + self.model.rotation += 10 + self.model.sprites = self.model.rotate_model() + self.model.surface = self.model.generate_surface() + + elif event.key == pygame.K_LEFT: + if not self.rotating: + self.model.rotation -= 10 + self.model.sprites = self.model.rotate_model() + self.model.surface = self.model.generate_surface() + + elif event.key == pygame.K_r: + self.rotating = not self.rotating + + def quit(self): + self.running = False + + +if __name__ == "__main__": + model_viewer = ModelViewer() + + while model_viewer.running: + model_viewer.loop() + + print("Bye!") diff --git a/models/coffee_cup.png b/models/coffee_cup.png new file mode 100644 index 0000000000000000000000000000000000000000..0cca32d7ebaab076910566c524390702381f804c GIT binary patch literal 281 zcmV+!0p|XRP)j1$fm4t}j`G|w;r_(XK zky2_p3n3T`&Z_ygKrvmAQtDfJbw;a-bS{rgX=XDhW_IB=rV%2}%0slONYN^V3+HH+ zLft4ckB#JMU^oRp*8qR;lf&E}&;DFDYV&*E<`@1*7;pex;bFl0_*=ZOkpQ|;5kfE; zD4+Q$+w3%4(ySzBajF}lJoK3yvQS6