From cd8fa05c41bb5b54ed001671e0cf1063e9ca52b6 Mon Sep 17 00:00:00 2001 From: Yan Wittmann Date: Sun, 26 Mar 2023 15:00:29 +0200 Subject: [PATCH] Added files for testing level loading and controller --- project/data/sprites/ghost_character.png | Bin 0 -> 190 bytes project/data/sprites/goal_door.png | Bin 0 -> 155 bytes project/data/sprites/small_spike.png | Bin 0 -> 143 bytes project/data/sprites/sprites.json | 57 +++++++++++++++--- project/data/sprites/tutorial_block_full.png | Bin 0 -> 148 bytes project/physics/TickData.py | 10 +-- .../physics/controllers/ElementController.py | 10 --- .../physics/controllers/PlayerController.py | 5 -- project/physics/controllers/PlayerSprite.py | 24 ++++++++ project/sprite/DynamicSprite.py | 1 + project/sprite/Sprite.py | 8 ++- project/ui_elements/KeyEvent.py | 31 ---------- project/ui_elements/KeyManager.py | 31 ++++++++++ project/ui_elements/TextLabel.py | 2 +- 14 files changed, 115 insertions(+), 64 deletions(-) create mode 100644 project/data/sprites/ghost_character.png create mode 100644 project/data/sprites/goal_door.png create mode 100644 project/data/sprites/small_spike.png create mode 100644 project/data/sprites/tutorial_block_full.png delete mode 100644 project/physics/controllers/ElementController.py delete mode 100644 project/physics/controllers/PlayerController.py create mode 100644 project/physics/controllers/PlayerSprite.py delete mode 100644 project/ui_elements/KeyEvent.py create mode 100644 project/ui_elements/KeyManager.py diff --git a/project/data/sprites/ghost_character.png b/project/data/sprites/ghost_character.png new file mode 100644 index 0000000000000000000000000000000000000000..98c298e2e8a01098d1fa66d533c797d1d68a0216 GIT binary patch literal 190 zcmeAS@N?(olHy`uVBq!ia0vp^20%Q4g&9chG58ta|ZZ?xB}__|NkF3a^%|om3$z<0*}aI1_r)EAj~ML;ne^XwDWXv4DmRg ze1KDewb6mW$w-s2k*P)IpU{%#iyNePSK}lWiiPp;FbGDz7jz4kD*&3r;OXk;vd$@? F2>`n7H|_uc literal 0 HcmV?d00001 diff --git a/project/data/sprites/goal_door.png b/project/data/sprites/goal_door.png new file mode 100644 index 0000000000000000000000000000000000000000..609e9cba60416ed526e897abe28e2eb90a34d845 GIT binary patch literal 155 zcmeAS@N?(olHy`uVBq!ia0vp^50}4S to+bwc21WL^X6c??xmR+PujcabFr3vk44ofy`glX(f`u%tWsIx;Y9 z?C1WI$O`0h7I;J!GcfQS0%1l`4X*~Epo*u9V~9p@vI3g{)5rh+|0~L@X_z>1BIl_i j+-{Nn{1`*#dk*T!Hle|NocXoPQU{;w TickData: + return TickData(0, [], KeyManager(), [], PositionScale()) def render_sprite_image(self) -> pygame.Surface: return self.image diff --git a/project/ui_elements/KeyEvent.py b/project/ui_elements/KeyEvent.py deleted file mode 100644 index ad031d2..0000000 --- a/project/ui_elements/KeyEvent.py +++ /dev/null @@ -1,31 +0,0 @@ -import pygame -from pygame.event import Event - - -class KeyEvent: - KEY_LEFT: list[int] = [pygame.K_LEFT, pygame.K_a] - KEY_RIGHT: list[int] = [pygame.K_RIGHT, pygame.K_d] - KEY_UP: list[int] = [pygame.K_UP, pygame.K_w] - KEY_DOWN: list[int] = [pygame.K_DOWN, pygame.K_s] - KEY_SPACE: list[int] = [pygame.K_SPACE] - KEY_ESCAPE: list[int] = [pygame.K_ESCAPE] - KEY_ENTER: list[int] = [pygame.K_RETURN, pygame.K_KP_ENTER] - - def __init__(self, event: Event): - self.event = event - - def is_key_down(self, key: int) -> bool: - return self.event.type == pygame.KEYDOWN and self.event.key == key - - def is_key_up(self, key: int) -> bool: - return self.event.type == pygame.KEYUP and self.event.key == key - - def is_keymap_down(self, keys: list[int]) -> bool: - return self.event.type == pygame.KEYDOWN and self.event.key in keys - - def is_keymap_up(self, keys: list[int]) -> bool: - return self.event.type == pygame.KEYUP and self.event.key in keys - - @staticmethod - def create_events(event: list[Event]) -> list['KeyEvent']: - return [KeyEvent(e) for e in event if e.type == pygame.KEYDOWN or e.type == pygame.KEYUP] diff --git a/project/ui_elements/KeyManager.py b/project/ui_elements/KeyManager.py new file mode 100644 index 0000000..dc4a2c6 --- /dev/null +++ b/project/ui_elements/KeyManager.py @@ -0,0 +1,31 @@ +import pygame +from pygame.event import Event + + +class KeyManager: + KEY_LEFT: list[int] = [pygame.K_LEFT, pygame.K_a] + KEY_RIGHT: list[int] = [pygame.K_RIGHT, pygame.K_d] + KEY_UP: list[int] = [pygame.K_UP, pygame.K_w] + KEY_DOWN: list[int] = [pygame.K_DOWN, pygame.K_s] + KEY_SPACE: list[int] = [pygame.K_SPACE] + KEY_ESCAPE: list[int] = [pygame.K_ESCAPE] + KEY_ENTER: list[int] = [pygame.K_RETURN, pygame.K_KP_ENTER] + + def __init__(self): + self.down = [] + + def update_key_events(self, events: list[Event]): + for event in events: + if event.type == pygame.KEYDOWN: + self.down.append(event.key) + elif event.type == pygame.KEYUP: + self.down.remove(event.key) + + def is_key_down(self, key: int) -> bool: + return key in self.down + + def is_keymap_down(self, keys: list[int]) -> bool: + for key in keys: + if key in self.down: + return True + return False diff --git a/project/ui_elements/TextLabel.py b/project/ui_elements/TextLabel.py index 74af2e1..d46b948 100644 --- a/project/ui_elements/TextLabel.py +++ b/project/ui_elements/TextLabel.py @@ -12,7 +12,7 @@ class TextLabel(UiElement): def tick(self, tick_data: TickData): pass - def __init__(self, text: str, x_position: float, y_position: float, font_size: int, alignment: str = "left"): + def __init__(self, text: str, x_position: float, y_position: float, font_size: int = 50, alignment: str = "left"): super().__init__() self.text = text