Pixelized it (And made it way faster)

This commit is contained in:
The Wobbler 2024-04-18 18:24:41 +02:00
parent c79224305e
commit a58865eba8
5 changed files with 65 additions and 51 deletions

View file

@ -1,48 +1,44 @@
#!/usr/bin/python3 #!/usr/bin/python3
import tools.pg
from tools.pg import crop_surface
import pygame import pygame
from dataclasses import dataclass
from wobbl_tools import pg
from wobbl_tools import data_file
class Object: class Object:
def __init__(self, model_path: str, model_properties_path: str=None, sprite_spacing: int=1, size_multiplier: int=1): def __init__(self, sprite_path: str="sprites/coffee_cup.png", sprite_spacing: int=1, rotation: int=45, size_multiplier: int=16):
self.model_path = model_path self.sprite_path = sprite_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.sprite_spacing = sprite_spacing
self.rotation = rotation
self.size_multiplier = size_multiplier self.size_multiplier = size_multiplier
self.rotation = 0 self.sprite_size = tuple(get_json(get_dir(sprite_path) + "/sprite_properties.json")[get_filename(sprite_path)]["size"])
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.get_sprites()
self.original_sprites = self.sprites self.sprites = self.rotate_sprites()
self.surface = self.generate_surface() self.surface = self.generate_surface()
def get_directory(self, file_path): print(self.sprite_size)
return file_path.rsplit("/", 1)[0]
def get_filename(self, file_path): def get_sprites(self):
return file_path.rsplit("/", 1)[1] model = pygame.image.load(self.sprite_path)
def generate_sprites(self):
model = pygame.image.load(self.model_path)
sw, sh = self.sprite_size sw, sh = self.sprite_size
mw, mh = model.get_size() mw, mh = model.get_size()
sprites = [] sprites = []
for x in range(0, mw, sw): for x in range(0, mw, sw):
sprite = tools.pg.crop_surface(model, (x, 0), (sw, sh)) sprite = 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 rotate_sprites(self):
sprites = []
for sprite in self.original_sprites:
sprite = pygame.transform.rotate(sprite, self.rotation)
sprites.append(sprite) sprites.append(sprite)
return sprites return sprites
@ -58,21 +54,26 @@ class Object:
surface.blit(sprite, (0, y)) surface.blit(sprite, (0, y))
y -= self.sprite_spacing y -= self.sprite_spacing
surface = pygame.transform.scale(surface, (surface.get_width() * self.size_multiplier, surface.get_height() * self.size_multiplier))
return surface return surface
def rotate_model(self):
sprites = []
for sprite in self.original_sprites: def get_dir(path: str):
sprite = pygame.transform.rotate(sprite, self.rotation) 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__": if __name__ == "__main__":
pass bla = Object()

View file

@ -15,7 +15,8 @@ class ModelViewer:
self.window_size = window_size self.window_size = window_size
self.fps = fps self.fps = fps
self.rotating = True 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() self.clock = pygame.time.Clock()
@ -30,19 +31,24 @@ class ModelViewer:
return return
if self.rotating: if self.rotating:
if self.rotation_cooldown == 0:
if self.model.rotation < 360: if self.model.rotation < 360:
self.model.rotation += 1 self.model.rotation += 1
else: else:
self.model.rotation = 0 self.model.rotation = 0
self.model.sprites = self.model.rotate_model() self.model.sprites = self.model.rotate_sprites()
self.model.surface = self.model.generate_surface() self.model.surface = self.model.generate_surface()
self.rotation_cooldown = 1
else:
self.rotation_cooldown -= 1
w, h = self.screen.get_size() w, h = self.screen.get_size()
mw, mh = self.model.surface.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() pygame.display.update()
self.clock.tick(self.fps) self.clock.tick(self.fps)

View file

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

View file

Before

Width:  |  Height:  |  Size: 281 B

After

Width:  |  Height:  |  Size: 281 B

View file

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