sprite_stacking_engine/model_viewer.py

96 lines
2.8 KiB
Python
Raw Permalink Normal View History

2024-02-03 17:08:51 +01:00
#!/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)
2024-02-03 17:16:02 +01:00
pygame.display.set_caption("Sprite Stacking Model Viewer")
2024-02-03 17:08:51 +01:00
self.gray = (20, 20, 20)
self.window_size = window_size
self.fps = fps
self.rotating = True
2024-04-19 17:20:28 +02:00
self.model = engine.Model(size_multiplier=4, sprite_spacing=1)
2024-04-18 18:24:41 +02:00
self.rotation_cooldown = 5
2024-02-03 17:08:51 +01:00
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:
2024-04-18 18:24:41 +02:00
if self.rotation_cooldown == 0:
if self.model.rotation < 360:
self.model.rotation += 1
2024-02-03 17:08:51 +01:00
2024-04-18 18:24:41 +02:00
else:
self.model.rotation = 0
self.model.sprites = self.model.rotate_sprites()
self.model.surface = self.model.generate_surface()
self.rotation_cooldown = 1
2024-02-03 17:08:51 +01:00
2024-04-18 18:24:41 +02:00
else:
self.rotation_cooldown -= 1
2024-02-03 17:08:51 +01:00
w, h = self.screen.get_size()
mw, mh = self.model.surface.get_size()
2024-04-18 18:24:41 +02:00
self.screen.blit(self.model.surface, (w // 2 - mw // 2, h // 2 - mh // 2))
2024-02-03 17:08:51 +01:00
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!")