From 78776f5ced1d5282ba97ca47fc5fe3a4da9e506f Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 28 Mar 2023 16:59:29 +0200 Subject: [PATCH 1/4] add background to LevelMenu --- project/main.py | 2 ++ project/ui_elements/Background.py | 31 +++++++++++++++++++++++++++++++ project/ui_elements/LevelMenu.py | 5 ++++- 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 project/ui_elements/Background.py diff --git a/project/main.py b/project/main.py index 3427e0c..d6348cd 100644 --- a/project/main.py +++ b/project/main.py @@ -76,6 +76,7 @@ if what_to_run == 'menu': global level_text_label for level_text_label in level_menu.level_text_labels: sprite_manager.remove_ui_element(level_text_label) + sprite_manager.remove_ui_element(level_menu.background) level_menu.level_select_listener = lambda selected_level: load_next_level(selected_level) @@ -103,6 +104,7 @@ if what_to_run == 'menu': destroy_menu() else: show_menu = True + sprite_manager.add_ui_element(DrawLayers.UI, level_menu.background) for level_text_label in level_menu.level_text_labels: sprite_manager.add_ui_element(DrawLayers.UI, level_text_label) diff --git a/project/ui_elements/Background.py b/project/ui_elements/Background.py new file mode 100644 index 0000000..17deaaf --- /dev/null +++ b/project/ui_elements/Background.py @@ -0,0 +1,31 @@ +from typing import Optional + +import pygame +from pygame import Surface + +from physics.TickData import TickData +from sprite.BoundingBox import BoundingBox +from ui_elements.UiElement import UiElement + + +class Background(UiElement): + + def __init__(self, x_pos: float, y_pos: float, width: float, height: float, color: tuple): + super().__init__() + self.x_pos = x_pos + self.y_pos = y_pos + self.width = width + self.height = height + self.color = color + + def tick(self, tick_data: TickData): + pass + + def render_sprite_image(self) -> Optional[Surface]: + surface = pygame.Surface((self.width, self.height), pygame.SRCALPHA) + background_rect = pygame.Rect(self.x_pos, self.y_pos, self.width, self.height) + pygame.draw.rect(surface, self.color, background_rect) + return surface + + def get_bounding_box(self) -> BoundingBox: + return BoundingBox(-1, -1, 0, 0) diff --git a/project/ui_elements/LevelMenu.py b/project/ui_elements/LevelMenu.py index 17c9196..b1de9f8 100644 --- a/project/ui_elements/LevelMenu.py +++ b/project/ui_elements/LevelMenu.py @@ -1,13 +1,15 @@ import pygame from level.Level import Level +from ui_elements.Background import Background from ui_elements.ClickEvent import ClickEvent from ui_elements.TextLabel import TextLabel class LevelMenu: - def __init__(self, levels: [Level], x_pos: int, y_pos: int, width: float, height: float, column_count: int): + def __init__(self, levels: [Level], x_pos: float, y_pos: float, width: float, height: float, column_count: int): + super().__init__() self.levels = levels self.x_pos = x_pos self.y_pos = y_pos @@ -18,6 +20,7 @@ class LevelMenu: self.column_count = column_count self.create_level_text_labels() + self.background = Background(self.x_pos, self.y_pos, self.width, self.height, (100, 100, 100, 180)) def create_level_text_labels(self): current_text_label_x = self.x_pos From 447ebdd5a88bc57438864ff8647c87cba5e5531c Mon Sep 17 00:00:00 2001 From: Yan Wittmann Date: Tue, 28 Mar 2023 17:17:51 +0200 Subject: [PATCH 2/4] Another attempt at optimization --- project/physics/PhysicsElementsHandler.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/project/physics/PhysicsElementsHandler.py b/project/physics/PhysicsElementsHandler.py index dc13ca1..0b565c9 100644 --- a/project/physics/PhysicsElementsHandler.py +++ b/project/physics/PhysicsElementsHandler.py @@ -167,8 +167,7 @@ class PhysicsElementsHandler: for collider in colliders: if sprite is not collider: distance = self.calculate_basic_distance(sprite, collider) - if distance > max(self.get_sprite_size_for_distance(sprite, screen_transform), - self.get_sprite_size_for_distance(collider, screen_transform)): + if distance > 50: continue if sprite.collides_with(collider, TOLERANCE): collides_with.append(collider) From 5381bb2c20e401fcfe6270e0a76653d8ca535966 Mon Sep 17 00:00:00 2001 From: Stephan Halder <2121472@stud.hs-mannheim.de> Date: Tue, 28 Mar 2023 17:26:47 +0200 Subject: [PATCH 3/4] spikeElements implemented --- project/data/levels/level-01.csv | 16 ++++++------ project/level/elements/LoadedLevel.py | 6 +++++ .../elements/static/SpikeLevelElement.py | 25 +++++++++++++++++++ 3 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 project/level/elements/static/SpikeLevelElement.py diff --git a/project/data/levels/level-01.csv b/project/data/levels/level-01.csv index 546a1f7..99426a6 100644 --- a/project/data/levels/level-01.csv +++ b/project/data/levels/level-01.csv @@ -2,14 +2,14 @@ #,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,# #,#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #,#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# -#,#,S,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# -#,#,S,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# -#,#,S,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# -#,#,S,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,C,,,# -#,#,S,,,,,,,,,,,,,,,,,,,+,+,+,+,+,+,+,+,+,+,+,+,+,+,+,+,+,+,+,+,+,S,S,S,S,S,S,S,S,+,+,+,+,+,+,+,+,+,# -#,#,S,,,,,,,,,,,,,,,,,,,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,# -#,#,S,,,,,,,,,,,,,,,,,,,,,#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# -#,#,S,,,,,,,,,,,,,,,,,,,,,#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# +#,#,>,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# +#,#,>,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# +#,#,>,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# +#,#,>,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,C,,,# +#,#,>,,,,,,,,,,,,,,,,,,,+,+,+,+,+,+,+,+,+,+,+,+,+,+,+,+,+,+,+,+,+,S,S,S,S,S,S,S,S,+,+,+,+,+,+,+,+,+,# +#,#,>,,,,,,,,,,,,,,,,,,,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,# +#,#,>,,,,,,,,,,,,,,,,,,,,,#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# +#,#,>,,,,,,,,,,,,,,,,,,,,,#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #,#,,,,,,,,,,,,,,,,,,,,,,G,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# diff --git a/project/level/elements/LoadedLevel.py b/project/level/elements/LoadedLevel.py index 3d08e3a..5dfbebe 100644 --- a/project/level/elements/LoadedLevel.py +++ b/project/level/elements/LoadedLevel.py @@ -3,6 +3,7 @@ from typing import Optional from level.Level import Level from level.elements.dynamic.BouncingBallLevelElement import BouncingBallLevelElement from level.elements.dynamic.PushableBoxLevelElement import PushableBoxLevelElement +from level.elements.static.SpikeLevelElement import SpikeLevelElement from level.elements.static.ButtonInputLevelElement import ButtonInputLevelElement from level.elements.static.GateReceiverLevelElement import GateReceiverLevelElement from level.elements.LevelElement import LevelElement @@ -29,6 +30,11 @@ TILES = { 'M': PushableBoxLevelElement, 'q': BouncingBallLevelElement, + + 'S': SpikeLevelElement, + 's': SpikeLevelElement, + '<': SpikeLevelElement, + '>': SpikeLevelElement } diff --git a/project/level/elements/static/SpikeLevelElement.py b/project/level/elements/static/SpikeLevelElement.py new file mode 100644 index 0000000..94ca474 --- /dev/null +++ b/project/level/elements/static/SpikeLevelElement.py @@ -0,0 +1,25 @@ +import random + +from level.Level import Level +from level.elements.static.StaticLevelElement import StaticLevelElement +from physics.SpriteManager import SpriteManager +from sprite.SpritesheetManager import SpritesheetManager + +BLOCK_TYPES = { + 'S': 'small_spike_up', + 's': 'small_spike_down', + '<': 'small_spike_left', + '>': 'small_spike_right' +} + +class SpikeLevelElement(StaticLevelElement): + + def __init__(self, tile: dict, loaded_level): + super().__init__(tile, loaded_level) + + 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 From 8aba74aa70f14d28508605bf7a774abb37e7c193 Mon Sep 17 00:00:00 2001 From: Stephan Halder <2121472@stud.hs-mannheim.de> Date: Tue, 28 Mar 2023 17:28:54 +0200 Subject: [PATCH 4/4] removed unused import --- project/level/elements/static/SpikeLevelElement.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/project/level/elements/static/SpikeLevelElement.py b/project/level/elements/static/SpikeLevelElement.py index 94ca474..f1e886f 100644 --- a/project/level/elements/static/SpikeLevelElement.py +++ b/project/level/elements/static/SpikeLevelElement.py @@ -1,5 +1,3 @@ -import random - from level.Level import Level from level.elements.static.StaticLevelElement import StaticLevelElement from physics.SpriteManager import SpriteManager