From 1a24f7c091e4883607928ce45d374187584f64fe Mon Sep 17 00:00:00 2001 From: Yan Wittmann Date: Mon, 27 Mar 2023 18:52:49 +0200 Subject: [PATCH] Fixed issue in box collision --- project/data/levels/0-tutorial.csv | 4 ++-- project/data/sprites/debug_1.png | Bin 0 -> 985 bytes project/data/sprites/sprites.json | 13 +++++++++++++ project/level/elements/LoadedLevel.py | 2 ++ .../dynamic/BouncingBallLevelElement.py | 17 +++++++++++++++++ project/main.py | 2 +- project/physics/PhysicsElementsHandler.py | 9 +++++---- project/sprite/DynamicSprite.py | 2 ++ 8 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 project/data/sprites/debug_1.png create mode 100644 project/level/elements/dynamic/BouncingBallLevelElement.py diff --git a/project/data/levels/0-tutorial.csv b/project/data/levels/0-tutorial.csv index 2c30b16..71d0e78 100644 --- a/project/data/levels/0-tutorial.csv +++ b/project/data/levels/0-tutorial.csv @@ -10,7 +10,7 @@ ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, -,,,,,,,,,,,,,,,,,,,,C,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, +,,,,,,,,,,,,,,,,,M,,,C,,,,,,,,,,,,,,,,,,,,,,,,,,,,M,,,,,,,,,,,, ,,,,,,,,,,,,,,,,+,+,+,+,+,+,+,+,+,,,,,,,,,,,,,,,,,,,,,+,+,+,+,+,,,,,,,,,,, ,,,,,,,,,,,,,,,,#,#,#,#,#,#,#,#,#,,,,,,,,,,,,,,,,,,,,,#,#,#,#,#,+,+,+,+,,,,,,, ,,,,,,,,,,,,,,,,#,#,#,#,#,#,#,#,#,,,,,,,+,+,+,+,+,+,+,,,,,,,,#,#,#,#,#,#,#,#,#,,,,,,, @@ -43,4 +43,4 @@ AQ,18,requires_xor=lever-1;button-1,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, AU,32,requires_and=lever-1;button-1,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, AJ,33,id=lever-1,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, Q,34,id=button-1,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, -F,23,size=7,,weight=0.5,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, \ No newline at end of file +F,23,size=8,,weight=0.5,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, \ No newline at end of file diff --git a/project/data/sprites/debug_1.png b/project/data/sprites/debug_1.png new file mode 100644 index 0000000000000000000000000000000000000000..9d307814b52ce0990405b79d4c105fdfdaf20011 GIT binary patch literal 985 zcmV;~119{5P)4Tx04UFukvmHRK@^3*7$xEZ3oS$~7JOh4K@>!=kqD-V7D)u7Hp!ZYup5`% z4H9ex8;gL2XydE!A80GsDhPrih}hZtY9y?8j1UnmoMHBUoY`}kIRiBjLpPn;K``^K z9Sw)H*~FX{c*IX90lFDW>W(!Pn~d}4c)P}@>eYbi#s5a*V%pKcS1&xGTed4aE*x2L zEyWwcZF(-bD7+^ewB>-p=Soji_(5^D!Y_*LcyvPUrh01GZ=CwZdd@b5`-OWw%}wP+Hb9-o&1#ivT8CJcshdS1hig4!+a&HuYC@0AigS3bOGBt&?o=5 zEPMb)^=K=VqP@`o000SaNLh0L04^c`04^c{s^Z;}0006ANklv|-@~1Z@ZP<|d1d1$_fl^Q$B30-HDnZA45__UCjL-mZJceQi zqVp&mL)!@SqMA*t0B}yH`_NFs5S@2u9YGk(qeqtw;Cu_k5!9`7!ZAnMhljTUJ##w$ z4{~NDjQ~35caSynj{%YE8IcOM`*m2^M&3jc*w*x|$l#~4wZL>ub5BeF&fMD2$Arik z&d*PuHnhj^Qf>d$Apk& z0Lv9~?n+A-?8`ABWEg-qGypfWTO4Cj3}CLmhdC|X;20ARTr@*H)Y=}`7@GNT1sJXA zN*6fBR2hJ;Vt{e$$?A+_Oql`BRlUjFmAXVxVPB33p~L_MRd2%UKYyVc5nx}(lTqlw zb>^Q%QMH>S??o2y476Jbe`6o^4WUTi<}I2+aSipe?T0t?a#rLS&tYv;*|?$1In&~p z`4^N%fCv9`y^7*Mwf@W+oFB5{onCFotJa^$8|TDZfkCfi7qZgT0j6cs@0@rmFz60x z$lCZeurG5Sa89fh_|g9rD-d7YB;@~WDMS2hDZsJgvqj_=GP;O;Z+O7&00000NkvXX Hu0mjfehtJt literal 0 HcmV?d00001 diff --git a/project/data/sprites/sprites.json b/project/data/sprites/sprites.json index c85982f..80e1bd7 100644 --- a/project/data/sprites/sprites.json +++ b/project/data/sprites/sprites.json @@ -353,6 +353,19 @@ } ] }, + { + "id": "debug_1", + "subsheets": [ + { + "id": "1", + "delays": [ + 1 + ], + "width": 24, + "height": 24 + } + ] + }, { "id": "test_1", "subsheets": [ diff --git a/project/level/elements/LoadedLevel.py b/project/level/elements/LoadedLevel.py index 10109af..3d08e3a 100644 --- a/project/level/elements/LoadedLevel.py +++ b/project/level/elements/LoadedLevel.py @@ -1,6 +1,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.ButtonInputLevelElement import ButtonInputLevelElement from level.elements.static.GateReceiverLevelElement import GateReceiverLevelElement @@ -27,6 +28,7 @@ TILES = { 'G': GateReceiverLevelElement, 'M': PushableBoxLevelElement, + 'q': BouncingBallLevelElement, } diff --git a/project/level/elements/dynamic/BouncingBallLevelElement.py b/project/level/elements/dynamic/BouncingBallLevelElement.py new file mode 100644 index 0000000..db5a3e0 --- /dev/null +++ b/project/level/elements/dynamic/BouncingBallLevelElement.py @@ -0,0 +1,17 @@ +from level.Level import Level +from level.elements.dynamic.PushableLevelElement import PushableLevelElement +from physics.SpriteManager import SpriteManager +from sprite.SpritesheetManager import SpritesheetManager + + +class BouncingBallLevelElement(PushableLevelElement): + def __init__(self, tile: dict, loaded_level): + super().__init__(tile, loaded_level) + + self.bounce_factor = -1 + + def load(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager, level: Level): + self.spritesheet = spritesheet_manager.get_sheet('debug_1') + self.set_animation_state('1') + self.position_scale.position = self.tile['position'] + self.position_scale.scale = (0.5, 0.5) diff --git a/project/main.py b/project/main.py index 3e41d03..bf65967 100644 --- a/project/main.py +++ b/project/main.py @@ -18,7 +18,7 @@ from ui_elements.KeyManager import KeyManager from ui_elements.LevelMenu import LevelMenu from ui_elements.TextLabel import TextLabel -what_to_run = 'menu' +what_to_run = 'level' def apply_frame_rate(number: float): diff --git a/project/physics/PhysicsElementsHandler.py b/project/physics/PhysicsElementsHandler.py index 057911f..dc13ca1 100644 --- a/project/physics/PhysicsElementsHandler.py +++ b/project/physics/PhysicsElementsHandler.py @@ -109,7 +109,7 @@ class PhysicsElementsHandler: sprite.position_scale.position[1] ) - sprite.motion = (0, sprite.motion[1]) + sprite.motion = (sprite.motion[0] * sprite.bounce_factor, sprite.motion[1]) collides_with_last = collider collided[0] = True @@ -153,14 +153,15 @@ class PhysicsElementsHandler: sprite.position_scale.position[1] - motion_step[1] ) - sprite.motion = (sprite.motion[0], 0) + sprite.motion = (sprite.motion[0], sprite.motion[1] * sprite.bounce_factor) collides_with_last = collider collided[1] = True return collides_with_last - def check_collides(self, sprite: StaticSprite, colliders: list[StaticSprite], screen_transform: PositionScale) -> list[StaticSprite]: + def check_collides(self, sprite: StaticSprite, colliders: list[StaticSprite], screen_transform: PositionScale) -> \ + list[StaticSprite]: collides_with = [] for collider in colliders: @@ -171,7 +172,7 @@ class PhysicsElementsHandler: continue if sprite.collides_with(collider, TOLERANCE): collides_with.append(collider) - if len(collides_with) >= 2: + if len(collides_with) > 5: break return collides_with diff --git a/project/sprite/DynamicSprite.py b/project/sprite/DynamicSprite.py index 2271396..13c76a3 100644 --- a/project/sprite/DynamicSprite.py +++ b/project/sprite/DynamicSprite.py @@ -17,6 +17,8 @@ class DynamicSprite(StaticSprite): self.deceleration_horizontal_ground = 0.3 self.gravity = 9.81 / 10 + self.bounce_factor = 0 + def tick(self, tick_data: TickData): super().tick(tick_data)