sprite_stacking_engine/model_viewer.py

95 lines
2.8 KiB
Python

#!/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)
pygame.display.set_caption("Sprite Stacking Model Viewer")
self.gray = (20, 20, 20)
self.window_size = window_size
self.fps = fps
self.rotating = True
self.model = engine.Model(size_multiplier=4, sprite_spacing=1)
self.rotation_cooldown = 5
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.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.rotation_cooldown -= 1
w, h = self.screen.get_size()
mw, mh = self.model.surface.get_size()
self.screen.blit(self.model.surface, (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!")