From 85a9518faf1595b33d96624b78a13b06699a2da9 Mon Sep 17 00:00:00 2001 From: Wobbl Date: Fri, 19 Jul 2024 18:24:45 +0200 Subject: [PATCH] Created the main file and integrated the engine into it. --- .../animated/sprite_properties.json | 2 +- assets/stacked_sprites/sprite_properties.json | 4 +- moss.py | 44 +++++++++++++++++++ sprite_stacking_engine/cache.py | 3 +- sprite_stacking_engine/engine.py | 44 ++++++------------- sprite_stacking_engine/player.py | 9 ++-- sprite_stacking_engine/scene.py | 13 +++--- sprite_stacking_engine/settings.py | 4 +- sprite_stacking_engine/stacked_sprite.py | 28 ++++++------ 9 files changed, 91 insertions(+), 60 deletions(-) create mode 100644 moss.py diff --git a/assets/stacked_sprites/animated/sprite_properties.json b/assets/stacked_sprites/animated/sprite_properties.json index c6bbb75..de51c50 100644 --- a/assets/stacked_sprites/animated/sprite_properties.json +++ b/assets/stacked_sprites/animated/sprite_properties.json @@ -1,6 +1,6 @@ { "Bla": { - "path": "../assets/stacked_sprites/animated/bla/", + "path": "assets/stacked_sprites/animated/bla/", "layers": 3, "scale": 16 } diff --git a/assets/stacked_sprites/sprite_properties.json b/assets/stacked_sprites/sprite_properties.json index 8f573ea..9456879 100644 --- a/assets/stacked_sprites/sprite_properties.json +++ b/assets/stacked_sprites/sprite_properties.json @@ -1,11 +1,11 @@ { "Robot": { - "path": "../assets/stacked_sprites/Robot.png", + "path": "assets/stacked_sprites/Robot.png", "layers": 16, "scale": 8 }, "Building": { - "path": "../assets/stacked_sprites/Building.png", + "path": "assets/stacked_sprites/Building.png", "layers": 80, "scale": 4 } diff --git a/moss.py b/moss.py new file mode 100644 index 0000000..20ce0af --- /dev/null +++ b/moss.py @@ -0,0 +1,44 @@ +#!/usr/bin/python3 + +import pygame +from wobbl_tools import pg +from sprite_stacking_engine.engine import Engine + + +class Moss: + def __init__(self): + self.screen = pygame.display.set_mode((1400, 800)) + self.clock = pygame.time.Clock() + self.engine = Engine(self) + self.time = 0 + self.delta_time = 0.01 + + self.running = True + + def update(self): + pygame.display.set_caption(f"{self.clock.get_fps(): .1f}") + self.delta_time = self.clock.tick(60) + self.engine.update() + + def draw(self): + self.engine.draw() + + self.screen.blit(self.engine.surface, (0, 0)) + + pygame.display.flip() + + def get_time(self): + self.time = pygame.time.get_ticks() * 0.001 + + def tick(self): + self.engine.check_events() + self.get_time() + self.update() + self.draw() + + +if __name__ == "__main__": + game = Moss() + + while game.running: + game.tick() diff --git a/sprite_stacking_engine/cache.py b/sprite_stacking_engine/cache.py index 3bc0b5c..8508fad 100644 --- a/sprite_stacking_engine/cache.py +++ b/sprite_stacking_engine/cache.py @@ -1,8 +1,7 @@ #!/usr/bin/python3 import os -from random import randrange -from settings import * +from sprite_stacking_engine.settings import * class Cache: diff --git a/sprite_stacking_engine/engine.py b/sprite_stacking_engine/engine.py index 737633e..2c44075 100755 --- a/sprite_stacking_engine/engine.py +++ b/sprite_stacking_engine/engine.py @@ -1,19 +1,16 @@ #!/usr/bin/python3 import sys -from settings import * -from stacked_sprite import StackedSprite -from cache import Cache -from player import Player -from scene import Scene +from sprite_stacking_engine.settings import * +from sprite_stacking_engine.cache import Cache +from sprite_stacking_engine.player import Player +from sprite_stacking_engine.scene import Scene -class App: - def __init__(self): - self.screen = pygame.display.set_mode(RES) - self.clock = pygame.time.Clock() - self.time = 0 - self.delta_time = 0.01 +class Engine: + def __init__(self, app): + self.app = app + self.surface = pygame.Surface(self.app.screen.get_size()) # surface to draw the sprites on # groups self.main_group = pygame.sprite.LayeredUpdates() # game objects @@ -23,13 +20,10 @@ class App: def update(self): self.main_group.update() - pygame.display.set_caption(f"{self.clock.get_fps(): .1f}") - self.delta_time = self.clock.tick(60) def draw(self): - self.screen.fill(BG_COLOR) - self.main_group.draw(self.screen) - pygame.display.flip() + self.surface.fill(BG_COLOR) + self.main_group.draw(self.surface) def check_events(self): for event in pygame.event.get(): @@ -37,17 +31,7 @@ class App: pygame.quit() sys.exit() - def get_time(self): - self.time = pygame.time.get_ticks() * 0.001 - - def run(self): - while True: - self.check_events() - self.get_time() - self.update() - self.draw() - - -if __name__ == "__main__": - app = App() - app.run() + def tick(self): + self.check_events() + self.update() + self.draw() diff --git a/sprite_stacking_engine/player.py b/sprite_stacking_engine/player.py index 870d3ad..679019e 100644 --- a/sprite_stacking_engine/player.py +++ b/sprite_stacking_engine/player.py @@ -1,13 +1,14 @@ #!/usr/bin/python3 -from settings import * +from sprite_stacking_engine.settings import * import math class Player(pygame.sprite.Sprite): - def __init__(self, app): - self.app = app - self.group = app.main_group + def __init__(self, engine): + self.engine = engine + self.app = engine.app + self.group = self.engine.main_group super().__init__(self.group) self.group.change_layer(self, CENTER.y) # simpy set the layer to the y center of the screen because that is - diff --git a/sprite_stacking_engine/scene.py b/sprite_stacking_engine/scene.py index 18684a2..cf485d2 100644 --- a/sprite_stacking_engine/scene.py +++ b/sprite_stacking_engine/scene.py @@ -1,6 +1,6 @@ #!/usr/bin/python3 -from stacked_sprite import * +from sprite_stacking_engine.stacked_sprite import * from random import uniform P = "player" @@ -23,8 +23,9 @@ MAP_CENTER = MAP_SIZE / 2 class Scene: - def __init__(self, app): - self.app = app + def __init__(self, engine): + self.engine = engine + self.app = engine.app self.load_scene() def load_scene(self): @@ -36,11 +37,11 @@ class Scene: pos = vec2(i, j) + vec2(0.5) if name == "player": - self.app.player.offset = pos * TILE_SIZE + self.engine.player.offset = pos * TILE_SIZE elif name: if name == "Bla": - AnimatedStackedSprite(self.app, name=name, pos=rand_pos(pos), rot=rand_rot()) + AnimatedStackedSprite(self.engine, name=name, pos=rand_pos(pos), rot=rand_rot()) else: - StackedSprite(self.app, name=name, pos=rand_pos(pos), rot=rand_rot()) + StackedSprite(self.engine, name=name, pos=rand_pos(pos), rot=rand_rot()) diff --git a/sprite_stacking_engine/settings.py b/sprite_stacking_engine/settings.py index fed4ed1..f78ae0c 100644 --- a/sprite_stacking_engine/settings.py +++ b/sprite_stacking_engine/settings.py @@ -27,8 +27,8 @@ NUM_ANGLES = 180 # multiple of 360 TRANSP_COLOR = (45, 54, 76) OUTLINE = True -SPRITE_ATTRS = load_json("../assets/stacked_sprites/sprite_properties.json") -ANIMATED_SPRITE_ATTRS = load_json("../assets/stacked_sprites/animated/sprite_properties.json") +SPRITE_ATTRS = load_json("assets/stacked_sprites/sprite_properties.json") +ANIMATED_SPRITE_ATTRS = load_json("assets/stacked_sprites/animated/sprite_properties.json") # SPRITE_ATTRS = { # "Robot": { diff --git a/sprite_stacking_engine/stacked_sprite.py b/sprite_stacking_engine/stacked_sprite.py index 3e2e905..098105d 100644 --- a/sprite_stacking_engine/stacked_sprite.py +++ b/sprite_stacking_engine/stacked_sprite.py @@ -1,17 +1,18 @@ #!/usr/bin/python3 import pygame -from settings import * +from sprite_stacking_engine.settings import * import math class StackedSprite(pygame.sprite.Sprite): - def __init__(self, app, name, pos, rot=0): - self.app = app + def __init__(self, engine, name, pos, rot=0): + self.engine = engine + self.app = engine.app self.name = name self.pos = vec2(pos) * TILE_SIZE - self.player = self.app.player - self.group = self.app.main_group + self.player = engine.player + self.group = engine.main_group super().__init__(self.group) self.attrs = SPRITE_ATTRS[name] # get attributes @@ -21,8 +22,8 @@ class StackedSprite(pygame.sprite.Sprite): self.y_offset = vec2(0, -self.attrs["layers"] / 2 * self.attrs["scale"]) # calculate the y offset because the - # images position in pygame is the position of the top left corner of the image and we use the bottom position - self.cache = self.app.cache.stacked_sprite_cache - self.viewing_angle = app.cache.viewing_angle + self.cache = engine.cache.stacked_sprite_cache + self.viewing_angle = engine.cache.viewing_angle self.rotated_sprites = self.cache[name]["rotated_sprites"] self.angle = 0 self.screen_position = vec2(0) @@ -52,12 +53,13 @@ class StackedSprite(pygame.sprite.Sprite): class AnimatedStackedSprite(pygame.sprite.Sprite): # stacked sprite class for animated sprites, works nearly the same - def __init__(self, app, name, pos, rot=0): - self.app = app + def __init__(self, engine, name, pos, rot=0): + self.engine = engine + self.app = engine.app self.name = name self.pos = vec2(pos) * TILE_SIZE - self.player = self.app.player - self.group = self.app.main_group + self.player = engine.player + self.group = engine.main_group super().__init__(self.group) self.attrs = ANIMATED_SPRITE_ATTRS[name] @@ -67,8 +69,8 @@ class AnimatedStackedSprite(pygame.sprite.Sprite): # stacked sprite class for a self.attrs["animated"] = True # set the animated attribute to true so that the cache knows that this sprite - self.y_offset = vec2(0, -self.attrs["layers"] / 2 * self.attrs["scale"]) # is animated - self.cache = self.app.cache.stacked_sprite_cache - self.viewing_angle = app.cache.viewing_angle + self.cache = engine.cache.stacked_sprite_cache + self.viewing_angle = engine.cache.viewing_angle self.frame = list(self.cache[name]["frames"].keys())[0] # set the frame to the first frame self.rotated_sprites = self.cache[name]["frames"][self.frame]["rotated_sprites"] self.angle = 0