Got it working.

This commit is contained in:
The Wobbler 2024-02-03 17:08:51 +01:00
parent 42f92f0295
commit 5b5594ae40
4 changed files with 173 additions and 0 deletions

78
engine.py Normal file
View file

@ -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

88
model_viewer.py Normal file
View file

@ -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!")

BIN
models/coffee_cup.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 281 B

View file

@ -0,0 +1,7 @@
#!/usr/bin/python3
properties = {
"coffee_cup.png": {
"sprite_size": (7, 10)
}
}