From e9e36c1d508369da4ac325f16d75ea0dc78da373 Mon Sep 17 00:00:00 2001 From: Skyball2000 Date: Wed, 29 Mar 2023 15:40:15 +0200 Subject: [PATCH] Added feature for colliding sprites messaging the main level logic --- project/level/elements/LevelElement.py | 4 +++- project/level/elements/LoadedLevel.py | 3 ++- .../elements/dynamic/BouncingBallLevelElement.py | 4 ++-- .../level/elements/dynamic/DynamicLevelElement.py | 4 ++-- .../elements/dynamic/PushableBoxLevelElement.py | 4 ++-- .../level/elements/dynamic/PushableLevelElement.py | 4 ++-- .../elements/static/ButtonInputLevelElement.py | 4 ++-- .../elements/static/GateReceiverLevelElement.py | 4 ++-- .../elements/static/GoalDoorReceiverLevelElement.py | 4 ++-- project/level/elements/static/InputLevelElement.py | 4 ++-- .../level/elements/static/LeverInputLevelElement.py | 4 ++-- .../level/elements/static/ReceiverLevelElement.py | 4 ++-- .../elements/static/SimpleBlockLevelElement.py | 4 ++-- project/level/elements/static/SpikeLevelElement.py | 13 ++++++++++--- project/level/elements/static/StaticLevelElement.py | 4 ++-- .../level/elements/static/VacuumCleanerElement.py | 4 ++-- project/level/selection/LevelScreenManager.py | 4 ++++ project/main.py | 3 +++ 18 files changed, 48 insertions(+), 31 deletions(-) diff --git a/project/level/elements/LevelElement.py b/project/level/elements/LevelElement.py index 6b88284..bda8798 100644 --- a/project/level/elements/LevelElement.py +++ b/project/level/elements/LevelElement.py @@ -1,5 +1,6 @@ import uuid from abc import abstractmethod +from typing import Optional from level.Level import Level from physics.SpriteManager import SpriteManager @@ -7,9 +8,10 @@ from sprite.SpritesheetManager import SpritesheetManager class LevelElement: - def __init__(self, tile: dict, loaded_level): + def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'): self.tile = tile self.loaded_level = loaded_level + self.level_screen_manager: 'LevelScreenManager' = level_screen_manager if 'id' in tile: self.id = tile['id'] diff --git a/project/level/elements/LoadedLevel.py b/project/level/elements/LoadedLevel.py index 36bb4a8..76d2685 100644 --- a/project/level/elements/LoadedLevel.py +++ b/project/level/elements/LoadedLevel.py @@ -52,6 +52,7 @@ class LoadedLevel: self.spritesheet_manager = spritesheet_manager self.blocks = [] self.elements_by_id: dict[str, LevelElement] = {} + self.level_screen_manager = None def load_level(self, level: Level): for row_number, row in enumerate(level.tiles): @@ -67,7 +68,7 @@ class LoadedLevel: layer = DrawLayers.OBJECTS if name in TILES: - element = TILES[name](tile, self) + element = TILES[name](tile, self, self.level_screen_manager) elif name == 'C': ghost_character = PlayerSprite(self.spritesheet_manager.get_sheet("ghost_character")) diff --git a/project/level/elements/dynamic/BouncingBallLevelElement.py b/project/level/elements/dynamic/BouncingBallLevelElement.py index db5a3e0..9412408 100644 --- a/project/level/elements/dynamic/BouncingBallLevelElement.py +++ b/project/level/elements/dynamic/BouncingBallLevelElement.py @@ -5,8 +5,8 @@ from sprite.SpritesheetManager import SpritesheetManager class BouncingBallLevelElement(PushableLevelElement): - def __init__(self, tile: dict, loaded_level): - super().__init__(tile, loaded_level) + def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'): + super().__init__(tile, loaded_level, level_screen_manager) self.bounce_factor = -1 diff --git a/project/level/elements/dynamic/DynamicLevelElement.py b/project/level/elements/dynamic/DynamicLevelElement.py index d6f2db0..372eac4 100644 --- a/project/level/elements/dynamic/DynamicLevelElement.py +++ b/project/level/elements/dynamic/DynamicLevelElement.py @@ -5,6 +5,6 @@ from sprite.DynamicSprite import DynamicSprite class DynamicLevelElement(DynamicSprite, LevelElement, ABC): - def __init__(self, tile: dict, loaded_level): + def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'): DynamicSprite.__init__(self) - LevelElement.__init__(self, tile, loaded_level) + LevelElement.__init__(self, tile, loaded_level, level_screen_manager) diff --git a/project/level/elements/dynamic/PushableBoxLevelElement.py b/project/level/elements/dynamic/PushableBoxLevelElement.py index 4d3ae00..16d2dc5 100644 --- a/project/level/elements/dynamic/PushableBoxLevelElement.py +++ b/project/level/elements/dynamic/PushableBoxLevelElement.py @@ -5,8 +5,8 @@ from sprite.SpritesheetManager import SpritesheetManager class PushableBoxLevelElement(PushableLevelElement): - def __init__(self, tile: dict, loaded_level): - super().__init__(tile, loaded_level) + def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'): + super().__init__(tile, loaded_level, level_screen_manager) if 'size' in self.tile: self.position_scale.scale = (float(self.tile['size']), diff --git a/project/level/elements/dynamic/PushableLevelElement.py b/project/level/elements/dynamic/PushableLevelElement.py index ac21915..c572ad2 100644 --- a/project/level/elements/dynamic/PushableLevelElement.py +++ b/project/level/elements/dynamic/PushableLevelElement.py @@ -6,8 +6,8 @@ from physics.TickData import TickData class PushableLevelElement(DynamicLevelElement, ABC): - def __init__(self, tile: dict, loaded_level): - super().__init__(tile, loaded_level) + def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'): + super().__init__(tile, loaded_level, level_screen_manager) if 'weight' in self.tile: self.weight = float(self.tile['weight']) diff --git a/project/level/elements/static/ButtonInputLevelElement.py b/project/level/elements/static/ButtonInputLevelElement.py index 4142b02..26ae1ba 100644 --- a/project/level/elements/static/ButtonInputLevelElement.py +++ b/project/level/elements/static/ButtonInputLevelElement.py @@ -8,8 +8,8 @@ from sprite.SpritesheetManager import SpritesheetManager class ButtonInputLevelElement(InputLevelElement): - def __init__(self, tile: dict, level: Level): - super().__init__(tile, level) + def __init__(self, tile: dict, level: Level, level_screen_manager: 'LevelScreenManager'): + super().__init__(tile, level, level_screen_manager) self.is_currently_stood_on = False self.is_collider = False diff --git a/project/level/elements/static/GateReceiverLevelElement.py b/project/level/elements/static/GateReceiverLevelElement.py index 1ab39fd..f8a489d 100644 --- a/project/level/elements/static/GateReceiverLevelElement.py +++ b/project/level/elements/static/GateReceiverLevelElement.py @@ -5,8 +5,8 @@ from sprite.SpritesheetManager import SpritesheetManager class GateReceiverLevelElement(ReceiverLevelElement): - def __init__(self, tile: dict, level: Level): - super().__init__(tile, level) + def __init__(self, tile: dict, level: Level, level_screen_manager: 'LevelScreenManager'): + super().__init__(tile, level, level_screen_manager) def load(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager, level: Level): self.spritesheet = spritesheet_manager.get_sheet('gate') diff --git a/project/level/elements/static/GoalDoorReceiverLevelElement.py b/project/level/elements/static/GoalDoorReceiverLevelElement.py index 6068013..2353fa5 100644 --- a/project/level/elements/static/GoalDoorReceiverLevelElement.py +++ b/project/level/elements/static/GoalDoorReceiverLevelElement.py @@ -5,8 +5,8 @@ from sprite.SpritesheetManager import SpritesheetManager class GoalDoorReceiverLevelElement(ReceiverLevelElement): - def __init__(self, tile: dict, level: Level): - super().__init__(tile, level) + def __init__(self, tile: dict, level: Level, level_screen_manager: 'LevelScreenManager'): + super().__init__(tile, level, level_screen_manager) def load(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager, level: Level): self.spritesheet = spritesheet_manager.get_sheet('goal_door') diff --git a/project/level/elements/static/InputLevelElement.py b/project/level/elements/static/InputLevelElement.py index 8db3e52..9386370 100644 --- a/project/level/elements/static/InputLevelElement.py +++ b/project/level/elements/static/InputLevelElement.py @@ -5,8 +5,8 @@ from level.elements.static.StaticLevelElement import StaticLevelElement class InputLevelElement(StaticLevelElement, ABC): - def __init__(self, tile: dict, loaded_level): - super().__init__(tile, loaded_level) + def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'): + super().__init__(tile, loaded_level, level_screen_manager) if 'emitter_state' in tile: self.emitter_state = tile['emitter_state'] == 'true' diff --git a/project/level/elements/static/LeverInputLevelElement.py b/project/level/elements/static/LeverInputLevelElement.py index 91454aa..f286049 100644 --- a/project/level/elements/static/LeverInputLevelElement.py +++ b/project/level/elements/static/LeverInputLevelElement.py @@ -8,8 +8,8 @@ from sprite.SpritesheetManager import SpritesheetManager class LeverInputLevelElement(InputLevelElement): - def __init__(self, tile: dict, level: Level): - super().__init__(tile, level) + def __init__(self, tile: dict, level: Level, level_screen_manager: 'LevelScreenManager'): + super().__init__(tile, level, level_screen_manager) self.is_currently_stood_on = False self.is_collider = False diff --git a/project/level/elements/static/ReceiverLevelElement.py b/project/level/elements/static/ReceiverLevelElement.py index d3efeeb..f747205 100644 --- a/project/level/elements/static/ReceiverLevelElement.py +++ b/project/level/elements/static/ReceiverLevelElement.py @@ -5,8 +5,8 @@ from physics.TickData import TickData class ReceiverLevelElement(StaticLevelElement, ABC): - def __init__(self, tile: dict, loaded_level): - super().__init__(tile, loaded_level) + def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'): + super().__init__(tile, loaded_level, level_screen_manager) self.requires = { } diff --git a/project/level/elements/static/SimpleBlockLevelElement.py b/project/level/elements/static/SimpleBlockLevelElement.py index 0dfa8b7..5a75863 100644 --- a/project/level/elements/static/SimpleBlockLevelElement.py +++ b/project/level/elements/static/SimpleBlockLevelElement.py @@ -17,8 +17,8 @@ BLOCK_TYPES = { class SimpleBlockLevelElement(StaticLevelElement): - def __init__(self, tile: dict, loaded_level): - super().__init__(tile, loaded_level) + def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'): + super().__init__(tile, loaded_level, level_screen_manager) def load(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager, level: Level): if self.tile['name'] in BLOCK_TYPES: diff --git a/project/level/elements/static/SpikeLevelElement.py b/project/level/elements/static/SpikeLevelElement.py index f1e886f..672c56f 100644 --- a/project/level/elements/static/SpikeLevelElement.py +++ b/project/level/elements/static/SpikeLevelElement.py @@ -1,6 +1,7 @@ from level.Level import Level from level.elements.static.StaticLevelElement import StaticLevelElement from physics.SpriteManager import SpriteManager +from physics.TickData import TickData from sprite.SpritesheetManager import SpritesheetManager BLOCK_TYPES = { @@ -12,12 +13,18 @@ BLOCK_TYPES = { class SpikeLevelElement(StaticLevelElement): - def __init__(self, tile: dict, loaded_level): - super().__init__(tile, loaded_level) + def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'): + super().__init__(tile, loaded_level, level_screen_manager) def load(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager, level: Level): block_type = BLOCK_TYPES[self.tile['name']] self.spritesheet = spritesheet_manager.get_sheet(block_type) self.set_animation_state('1') - self.position_scale.position = self.tile['position'] \ No newline at end of file + self.position_scale.position = self.tile['position'] + + def tick(self, tick_data: TickData): + super().tick(tick_data) + for collision in self.get_collides_with(): + if collision.secondary_sprite.id == 'player': + self.level_screen_manager.player_death() \ No newline at end of file diff --git a/project/level/elements/static/StaticLevelElement.py b/project/level/elements/static/StaticLevelElement.py index 9f42d5b..a533e6d 100644 --- a/project/level/elements/static/StaticLevelElement.py +++ b/project/level/elements/static/StaticLevelElement.py @@ -5,6 +5,6 @@ from sprite.StaticSprite import StaticSprite class StaticLevelElement(StaticSprite, LevelElement, ABC): - def __init__(self, tile: dict, loaded_level): + def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'): StaticSprite.__init__(self) - LevelElement.__init__(self, tile, loaded_level) + LevelElement.__init__(self, tile, loaded_level, level_screen_manager) diff --git a/project/level/elements/static/VacuumCleanerElement.py b/project/level/elements/static/VacuumCleanerElement.py index b0dc97e..948d221 100644 --- a/project/level/elements/static/VacuumCleanerElement.py +++ b/project/level/elements/static/VacuumCleanerElement.py @@ -9,8 +9,8 @@ BLOCK_TYPES = { class VacuumCleanerElement(StaticLevelElement): - def __init__(self, tile: dict, loaded_level): - super().__init__(tile, loaded_level) + def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'): + super().__init__(tile, loaded_level, level_screen_manager) def load(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager, level: Level): block_type = BLOCK_TYPES[self.tile['name']] diff --git a/project/level/selection/LevelScreenManager.py b/project/level/selection/LevelScreenManager.py index 2c473f7..9b1af17 100644 --- a/project/level/selection/LevelScreenManager.py +++ b/project/level/selection/LevelScreenManager.py @@ -16,6 +16,7 @@ class LevelScreenManager(ScreenManager): self.level = level self.loaded_level = LoadedLevel(sprite_manager, spritesheet_manager) + self.loaded_level.level_screen_manager = self self.loaded_level.load_level(self.level) def initialize(self): @@ -28,3 +29,6 @@ class LevelScreenManager(ScreenManager): def tick(self, tick_data: TickData): if tick_data.key_manager.is_keymap_down(KeyManager.KEY_ESCAPE): self.main_loop.select_level_selection(self.level.theme) + + def player_death(self): + self.main_loop.player_death(self.level) diff --git a/project/main.py b/project/main.py index d786c99..00083b6 100644 --- a/project/main.py +++ b/project/main.py @@ -98,6 +98,9 @@ class MainLoop: if self.screen_manager is not None and isinstance(self.screen_manager, LevelSelectionScreenManager): self.screen_manager.select_theme(theme) + def player_death(self, level: Level): + self.select_level(level) + def select_main_menu(self): self.set_game_state(self.GAME_STATE_MENU)