Created the main file and integrated the engine into it.

This commit is contained in:
The Wobbler 2024-07-19 18:24:45 +02:00
parent 9b1d91934b
commit 85a9518faf
9 changed files with 91 additions and 60 deletions

View file

@ -1,6 +1,6 @@
{ {
"Bla": { "Bla": {
"path": "../assets/stacked_sprites/animated/bla/", "path": "assets/stacked_sprites/animated/bla/",
"layers": 3, "layers": 3,
"scale": 16 "scale": 16
} }

View file

@ -1,11 +1,11 @@
{ {
"Robot": { "Robot": {
"path": "../assets/stacked_sprites/Robot.png", "path": "assets/stacked_sprites/Robot.png",
"layers": 16, "layers": 16,
"scale": 8 "scale": 8
}, },
"Building": { "Building": {
"path": "../assets/stacked_sprites/Building.png", "path": "assets/stacked_sprites/Building.png",
"layers": 80, "layers": 80,
"scale": 4 "scale": 4
} }

44
moss.py Normal file
View file

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

View file

@ -1,8 +1,7 @@
#!/usr/bin/python3 #!/usr/bin/python3
import os import os
from random import randrange from sprite_stacking_engine.settings import *
from settings import *
class Cache: class Cache:

View file

@ -1,19 +1,16 @@
#!/usr/bin/python3 #!/usr/bin/python3
import sys import sys
from settings import * from sprite_stacking_engine.settings import *
from stacked_sprite import StackedSprite from sprite_stacking_engine.cache import Cache
from cache import Cache from sprite_stacking_engine.player import Player
from player import Player from sprite_stacking_engine.scene import Scene
from scene import Scene
class App: class Engine:
def __init__(self): def __init__(self, app):
self.screen = pygame.display.set_mode(RES) self.app = app
self.clock = pygame.time.Clock() self.surface = pygame.Surface(self.app.screen.get_size()) # surface to draw the sprites on
self.time = 0
self.delta_time = 0.01
# groups # groups
self.main_group = pygame.sprite.LayeredUpdates() self.main_group = pygame.sprite.LayeredUpdates()
# game objects # game objects
@ -23,13 +20,10 @@ class App:
def update(self): def update(self):
self.main_group.update() self.main_group.update()
pygame.display.set_caption(f"{self.clock.get_fps(): .1f}")
self.delta_time = self.clock.tick(60)
def draw(self): def draw(self):
self.screen.fill(BG_COLOR) self.surface.fill(BG_COLOR)
self.main_group.draw(self.screen) self.main_group.draw(self.surface)
pygame.display.flip()
def check_events(self): def check_events(self):
for event in pygame.event.get(): for event in pygame.event.get():
@ -37,17 +31,7 @@ class App:
pygame.quit() pygame.quit()
sys.exit() sys.exit()
def get_time(self): def tick(self):
self.time = pygame.time.get_ticks() * 0.001 self.check_events()
self.update()
def run(self): self.draw()
while True:
self.check_events()
self.get_time()
self.update()
self.draw()
if __name__ == "__main__":
app = App()
app.run()

View file

@ -1,13 +1,14 @@
#!/usr/bin/python3 #!/usr/bin/python3
from settings import * from sprite_stacking_engine.settings import *
import math import math
class Player(pygame.sprite.Sprite): class Player(pygame.sprite.Sprite):
def __init__(self, app): def __init__(self, engine):
self.app = app self.engine = engine
self.group = app.main_group self.app = engine.app
self.group = self.engine.main_group
super().__init__(self.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 - self.group.change_layer(self, CENTER.y) # simpy set the layer to the y center of the screen because that is -

View file

@ -1,6 +1,6 @@
#!/usr/bin/python3 #!/usr/bin/python3
from stacked_sprite import * from sprite_stacking_engine.stacked_sprite import *
from random import uniform from random import uniform
P = "player" P = "player"
@ -23,8 +23,9 @@ MAP_CENTER = MAP_SIZE / 2
class Scene: class Scene:
def __init__(self, app): def __init__(self, engine):
self.app = app self.engine = engine
self.app = engine.app
self.load_scene() self.load_scene()
def load_scene(self): def load_scene(self):
@ -36,11 +37,11 @@ class Scene:
pos = vec2(i, j) + vec2(0.5) pos = vec2(i, j) + vec2(0.5)
if name == "player": if name == "player":
self.app.player.offset = pos * TILE_SIZE self.engine.player.offset = pos * TILE_SIZE
elif name: elif name:
if name == "Bla": 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: 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())

View file

@ -27,8 +27,8 @@ NUM_ANGLES = 180 # multiple of 360
TRANSP_COLOR = (45, 54, 76) TRANSP_COLOR = (45, 54, 76)
OUTLINE = True OUTLINE = True
SPRITE_ATTRS = load_json("../assets/stacked_sprites/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") ANIMATED_SPRITE_ATTRS = load_json("assets/stacked_sprites/animated/sprite_properties.json")
# SPRITE_ATTRS = { # SPRITE_ATTRS = {
# "Robot": { # "Robot": {

View file

@ -1,17 +1,18 @@
#!/usr/bin/python3 #!/usr/bin/python3
import pygame import pygame
from settings import * from sprite_stacking_engine.settings import *
import math import math
class StackedSprite(pygame.sprite.Sprite): class StackedSprite(pygame.sprite.Sprite):
def __init__(self, app, name, pos, rot=0): def __init__(self, engine, name, pos, rot=0):
self.app = app self.engine = engine
self.app = engine.app
self.name = name self.name = name
self.pos = vec2(pos) * TILE_SIZE self.pos = vec2(pos) * TILE_SIZE
self.player = self.app.player self.player = engine.player
self.group = self.app.main_group self.group = engine.main_group
super().__init__(self.group) super().__init__(self.group)
self.attrs = SPRITE_ATTRS[name] # get attributes 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 - 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 # 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.cache = engine.cache.stacked_sprite_cache
self.viewing_angle = app.cache.viewing_angle self.viewing_angle = engine.cache.viewing_angle
self.rotated_sprites = self.cache[name]["rotated_sprites"] self.rotated_sprites = self.cache[name]["rotated_sprites"]
self.angle = 0 self.angle = 0
self.screen_position = vec2(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 class AnimatedStackedSprite(pygame.sprite.Sprite): # stacked sprite class for animated sprites, works nearly the same
def __init__(self, app, name, pos, rot=0): def __init__(self, engine, name, pos, rot=0):
self.app = app self.engine = engine
self.app = engine.app
self.name = name self.name = name
self.pos = vec2(pos) * TILE_SIZE self.pos = vec2(pos) * TILE_SIZE
self.player = self.app.player self.player = engine.player
self.group = self.app.main_group self.group = engine.main_group
super().__init__(self.group) super().__init__(self.group)
self.attrs = ANIMATED_SPRITE_ATTRS[name] 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.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.y_offset = vec2(0, -self.attrs["layers"] / 2 * self.attrs["scale"]) # is animated
self.cache = self.app.cache.stacked_sprite_cache self.cache = engine.cache.stacked_sprite_cache
self.viewing_angle = app.cache.viewing_angle self.viewing_angle = engine.cache.viewing_angle
self.frame = list(self.cache[name]["frames"].keys())[0] # set the frame to the first frame 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.rotated_sprites = self.cache[name]["frames"][self.frame]["rotated_sprites"]
self.angle = 0 self.angle = 0