From bf0688408e11e942029d20704322aba71960f943 Mon Sep 17 00:00:00 2001 From: Stephan Halder <2121472@stud.hs-mannheim.de> Date: Sun, 26 Mar 2023 15:14:06 +0200 Subject: [PATCH] countinued working on level loading --- project/data/levels/1-1.csv | 2 +- project/data/levels/levels.json | 2 +- project/data/sprites/sprites.json | 29 +++++++++++++ project/level/LevelElementSymbols.py | 20 ++++++--- project/level/LevelManager.py | 2 - project/level/elements/BlockElement.py | 10 ++--- project/level/elements/InteractableElement.py | 10 ++--- project/level/elements/LevelElement.py | 9 ---- project/level/elements/LoadedLevel.py | 38 ++++++++-------- project/level/elements/StaticLevelElement.py | 7 +++ project/main.py | 43 ++++++++++++++++--- project/sprite/PositionScale.py | 2 +- 12 files changed, 118 insertions(+), 56 deletions(-) delete mode 100644 project/level/elements/LevelElement.py create mode 100644 project/level/elements/StaticLevelElement.py diff --git a/project/data/levels/1-1.csv b/project/data/levels/1-1.csv index 6fe1a88..da5f96c 100644 --- a/project/data/levels/1-1.csv +++ b/project/data/levels/1-1.csv @@ -26,7 +26,7 @@ ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, -,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,X,X,X,X,X,X,X,X,X,,,,,,,,,,,,,,,,,D,,, +,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,x,x,x,x,x,x,x,x,x,,,,,,,,,,,,,,,,,D,,, #,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,,,,,,,,,,,,,,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,# #,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,,,,,,,,,,,,,,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,# #,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,,,,,,,,,,,,,,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,# diff --git a/project/data/levels/levels.json b/project/data/levels/levels.json index 159da51..f67d65b 100644 --- a/project/data/levels/levels.json +++ b/project/data/levels/levels.json @@ -1,7 +1,7 @@ [ { "name": "1-1", - "theme": "ghost", + "theme": "tutorial", "abilities": [ "dash" ], diff --git a/project/data/sprites/sprites.json b/project/data/sprites/sprites.json index 1882b19..850fb5b 100644 --- a/project/data/sprites/sprites.json +++ b/project/data/sprites/sprites.json @@ -88,5 +88,34 @@ "height": 36 } ] + }, + { + "id": "tutorial_block_full", + "subsheets": [ + { + "id": "1", + "delays": [ + 1 + ], + "width": 16, + "height": 16 + }, + { + "id": "2", + "delays": [ + 1 + ], + "width": 16, + "height": 16 + }, + { + "id": "3", + "delays": [ + 1 + ], + "width": 16, + "height": 16 + } + ] } ] \ No newline at end of file diff --git a/project/level/LevelElementSymbols.py b/project/level/LevelElementSymbols.py index f00a2b4..901b542 100644 --- a/project/level/LevelElementSymbols.py +++ b/project/level/LevelElementSymbols.py @@ -5,9 +5,12 @@ DYNAMIC = 'dynamic' class LevelElementSymbols: SOLID_BLOCK = { - 'symbol': '#', 'type': STATIC, - 'sprite_id': '_block' + 'sprite_id': '_block_full', + 'themed': True + } + SOLID_BLOCK_TOP = { + } SOLID_BLOCK_LEFT = { 'type': STATIC, @@ -26,7 +29,7 @@ class LevelElementSymbols: 'sprite_id': '_gate' } GOAL_DOOR = { - 'type':STATIC, + 'type': STATIC, 'sprite_id': '_goal_door' } SMALL_SPIKE = { @@ -34,7 +37,7 @@ class LevelElementSymbols: 'sprite_id': '_small_spike' } LEVER = { - 'type':STATIC, + 'type': STATIC, 'sprite_id': '_lever' } PLAYER = { @@ -44,13 +47,16 @@ class LevelElementSymbols: dict = { '#': SOLID_BLOCK, + '+': SOLID_BLOCK, 'l': SOLID_BLOCK_RIGHT, 'r': SOLID_BLOCK_LEFT, '': AIR, - 'g': GATE, - 'd': GOAL_DOOR, + 'G': GATE, + 'D': GOAL_DOOR, 'x': SMALL_SPIKE, - 'h': LEVER, + 'L': LEVER, 'P': PLAYER } + BLOCKS_LIST = [SOLID_BLOCK, SOLID_BLOCK_RIGHT, SOLID_BLOCK_LEFT] + diff --git a/project/level/LevelManager.py b/project/level/LevelManager.py index 6cf6a30..0c1b878 100644 --- a/project/level/LevelManager.py +++ b/project/level/LevelManager.py @@ -1,5 +1,3 @@ -import sys -import os import json from level.Level import Level diff --git a/project/level/elements/BlockElement.py b/project/level/elements/BlockElement.py index e1c346d..6058886 100644 --- a/project/level/elements/BlockElement.py +++ b/project/level/elements/BlockElement.py @@ -1,7 +1,7 @@ -from level.elements.LevelElement import LevelElement -from sprite.StaticSprite import StaticSprite +from level.elements.StaticLevelElement import StaticLevelElement +from sprite.Spritesheet import Spritesheet -class BlockElement(LevelElement): - def __init__(self, x_position : int, y_position : int, sprite : StaticSprite): - super().__init__(x_position, y_position, sprite) +class BlockElement(StaticLevelElement): + def __init__(self, spritesheet: Spritesheet): + super().__init__(spritesheet) diff --git a/project/level/elements/InteractableElement.py b/project/level/elements/InteractableElement.py index 125d4fb..5d6fc5f 100644 --- a/project/level/elements/InteractableElement.py +++ b/project/level/elements/InteractableElement.py @@ -1,9 +1,9 @@ -from LevelElement import LevelElement -from sprite.StaticSprite import StaticSprite +from StaticLevelElement import StaticLevelElement +from sprite.Spritesheet import Spritesheet -class InteractableElement(LevelElement): - def __init__(self, x_position : int, y_position : int, sprite : StaticSprite, block_id : str, requires : list[str]): - super().__init__(x_position, y_position, sprite) +class InteractableElement(StaticLevelElement): + def __init__(self, spritesheet: Spritesheet, block_id: str, requires: list[str]): + super().__init__(spritesheet) self.block_id = block_id self.requires = requires diff --git a/project/level/elements/LevelElement.py b/project/level/elements/LevelElement.py deleted file mode 100644 index 951cd3a..0000000 --- a/project/level/elements/LevelElement.py +++ /dev/null @@ -1,9 +0,0 @@ -from sprite.StaticSprite import StaticSprite - -class LevelElement: - def __init__(self,x_position : int, y_position : int, sprite : StaticSprite): - self.x_position = x_position - self.y_position = y_position - self.sprite = sprite - - diff --git a/project/level/elements/LoadedLevel.py b/project/level/elements/LoadedLevel.py index e8ca47c..e42351b 100644 --- a/project/level/elements/LoadedLevel.py +++ b/project/level/elements/LoadedLevel.py @@ -1,12 +1,12 @@ from level.Level import Level from level.LevelElementSymbols import LevelElementSymbols from level.elements.BlockElement import BlockElement -from level.elements.InteractableElement import InteractableElement from physics.SpriteManager import SpriteManager, DrawLayers from sprite.SpritesheetManager import SpritesheetManager -from sprite.StaticSprite import StaticSprite BLOCK_SIZE = (12, 12) + + class LoadedLevel: def __init__(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager): self.sprite_manager = sprite_manager @@ -15,25 +15,25 @@ class LoadedLevel: def load_level(self, level: Level): for row_number, row in enumerate(level.tiles): - self.blocks.append([]) for tile_number, tile in enumerate(row): - x_position = row_number * BLOCK_SIZE[0] - y_position = tile_number * BLOCK_SIZE[1] - if LevelElementSymbols.dict[tile] != LevelElementSymbols.PLAYER and LevelElementSymbols.dict[tile] != LevelElementSymbols.AIR: - sprite = StaticSprite(self.spritesheet_manager.get_sheet(level.theme + LevelElementSymbols.dict[tile]['sprite_id'])) - sprite.position_scale.position = (x_position, y_position) + position = (tile_number * BLOCK_SIZE[0], row_number * BLOCK_SIZE[1]) + + tile_element = LevelElementSymbols.dict[tile['name']] + + sprite = None + + if tile_element in LevelElementSymbols.BLOCKS_LIST: + spritesheet = self.spritesheet_manager.get_sheet(level.theme + tile_element['sprite_id']) + sprite = BlockElement(spritesheet) sprite.set_animation_state('1') + sprite.position_scale.position = position + + # tile['id'], tile['requirements'] + + if sprite is not None: self.sprite_manager.add_ui_element(DrawLayers.OBJECTS, sprite) - if(tile['id'] is None): - self.blocks[row_number].append(BlockElement(x_position, y_position, sprite)) - else: - self.blocks[row_number].append(InteractableElement(x_position, y_position, sprite,tile['id'], tile['requirements'])) - - - - - - def destroy_level(self): - del self.blocks \ No newline at end of file + for block in self.blocks: + self.sprite_manager.remove_ui_element(block) + del self.blocks diff --git a/project/level/elements/StaticLevelElement.py b/project/level/elements/StaticLevelElement.py new file mode 100644 index 0000000..8b412c6 --- /dev/null +++ b/project/level/elements/StaticLevelElement.py @@ -0,0 +1,7 @@ +from sprite.Spritesheet import Spritesheet +from sprite.StaticSprite import StaticSprite + + +class StaticLevelElement(StaticSprite): + def __init__(self, spritesheet: Spritesheet): + super().__init__(spritesheet) diff --git a/project/main.py b/project/main.py index 59a8437..8bbac7a 100644 --- a/project/main.py +++ b/project/main.py @@ -3,6 +3,7 @@ import random import pygame from level.LevelManager import LevelManager +from level.elements.LoadedLevel import LoadedLevel from physics.SpriteManager import SpriteManager, DrawLayers from physics.TickData import TickData from physics.controllers.PlayerSprite import PlayerSprite @@ -14,7 +15,7 @@ from ui_elements.ClickEvent import ClickEvent from ui_elements.KeyManager import KeyManager from ui_elements.TextLabel import TextLabel -what_to_run = 'physics' +what_to_run = 'level' def apply_frame_rate(number: float): @@ -28,11 +29,41 @@ def apply_frame_rate(number: float): if what_to_run == 'level': - csv_parse_test = LevelManager('data/levels') - csv_parse_test.load_from_config('data/levels/levels.json') - print(csv_parse_test.levels[0]) - for row in csv_parse_test.levels[0].tiles: - print(row) + screen_transform = PositionScale((0, 0), (1.5, 1.5)) + + pygame.init() + screen = pygame.display.set_mode((12 * 71 * 1.5, 12 * 40 * 1.5)) + pygame.display.set_caption("PM GAME") + clock = pygame.time.Clock() + frame_rate = 30 + + spritesheet_manager = SpritesheetManager("data/sprites", "data/sprites/sprites.json") + sprite_manager = SpriteManager() + key_manager = KeyManager() + + parsed_levels_manager = LevelManager('data/levels') + parsed_levels_manager.load_from_config('data/levels/levels.json') + + generated_level = LoadedLevel(sprite_manager, spritesheet_manager) + generated_level.load_level(parsed_levels_manager.levels[0]) + + while True: + clock.tick(frame_rate) + + pygame_events: list[pygame.event.Event] = pygame.event.get() + key_manager.update_key_events(pygame_events) + click_events: list[ClickEvent] = ClickEvent.create_events(pygame_events, screen_transform) + + for event in pygame_events: + if event.type == pygame.QUIT: + pygame.quit() + quit() + + screen.fill((0, 0, 0)) + + sprite_manager.tick(TickData(apply_frame_rate(1), pygame_events, key_manager, click_events, screen_transform)) + sprite_manager.draw(screen, screen_transform) + pygame.display.update() elif what_to_run == 'physics': diff --git a/project/sprite/PositionScale.py b/project/sprite/PositionScale.py index 1666c63..d3f6be2 100644 --- a/project/sprite/PositionScale.py +++ b/project/sprite/PositionScale.py @@ -1,5 +1,5 @@ class PositionScale: - def __init__(self, position: tuple[int, int] = (0, 0), scale: tuple[int, int] = (1, 1)): + def __init__(self, position: tuple[float, float] = (0, 0), scale: tuple[float, float] = (1, 1)): self.position = position self.scale = scale