diff --git a/project/data/sprites/ghost_character.png b/project/data/sprites/ghost_character.png new file mode 100644 index 0000000..98c298e Binary files /dev/null and b/project/data/sprites/ghost_character.png differ diff --git a/project/data/sprites/goal_door.png b/project/data/sprites/goal_door.png new file mode 100644 index 0000000..609e9cb Binary files /dev/null and b/project/data/sprites/goal_door.png differ diff --git a/project/data/sprites/small_spike.png b/project/data/sprites/small_spike.png new file mode 100644 index 0000000..46a3648 Binary files /dev/null and b/project/data/sprites/small_spike.png differ diff --git a/project/data/sprites/sprites.json b/project/data/sprites/sprites.json index f7e3e93..1882b19 100644 --- a/project/data/sprites/sprites.json +++ b/project/data/sprites/sprites.json @@ -10,8 +10,8 @@ 3, 1 ], - "height": 16, - "width": 16 + "width": 16, + "height": 16 }, { "id": "idle", @@ -22,8 +22,8 @@ 1, 1 ], - "height": 16, - "width": 16 + "width": 16, + "height": 16 }, { "id": "walk_r", @@ -33,8 +33,8 @@ 1, 1 ], - "height": 16, - "width": 16 + "width": 16, + "height": 16 }, { "id": "other_test", @@ -43,8 +43,49 @@ 1, 1 ], - "height": 16, - "width": 16 + "width": 16, + "height": 16 + } + ] + }, + { + "id": "ghost_character", + "subsheets": [ + { + "id": "idle", + "delays": [ + 40, + 10 + ], + "width": 24, + "height": 36 + }, + { + "id": "walk_l", + "delays": [ + 1, + 1 + ], + "width": 24, + "height": 36 + }, + { + "id": "walk_r", + "delays": [ + 1, + 1 + ], + "width": 24, + "height": 36 + }, + { + "id": "jump", + "delays": [ + 1, + 1 + ], + "width": 24, + "height": 36 } ] } diff --git a/project/data/sprites/tutorial_block_full.png b/project/data/sprites/tutorial_block_full.png new file mode 100644 index 0000000..5d9f5fe Binary files /dev/null and b/project/data/sprites/tutorial_block_full.png differ diff --git a/project/physics/TickData.py b/project/physics/TickData.py index 32643e2..7c87d69 100644 --- a/project/physics/TickData.py +++ b/project/physics/TickData.py @@ -2,18 +2,14 @@ import pygame from sprite.PositionScale import PositionScale from ui_elements.ClickEvent import ClickEvent -from ui_elements.KeyEvent import KeyEvent +from ui_elements.KeyManager import KeyManager class TickData: - def __init__(self, dt: float, pygame_events: list[pygame.event.Event], key_events: list[KeyEvent], + def __init__(self, dt: float, pygame_events: list[pygame.event.Event], key_manager: KeyManager, click_events: list[ClickEvent], screen_transform: PositionScale): self.dt = dt self.pygame_events = pygame_events - self.key_events = key_events + self.key_manager = key_manager self.click_events = click_events self.screen_transform = screen_transform - - @staticmethod - def empty(): - pass diff --git a/project/physics/controllers/ElementController.py b/project/physics/controllers/ElementController.py deleted file mode 100644 index 270be20..0000000 --- a/project/physics/controllers/ElementController.py +++ /dev/null @@ -1,10 +0,0 @@ -from ui_elements.ClickEvent import ClickEvent -from ui_elements.KeyEvent import KeyEvent - - -class ElementController: - def __init__(self): - pass - - def handle_input(self, dt: float, key_events: list[KeyEvent], click_events: list[ClickEvent]): - pass diff --git a/project/physics/controllers/PlayerController.py b/project/physics/controllers/PlayerController.py deleted file mode 100644 index c948746..0000000 --- a/project/physics/controllers/PlayerController.py +++ /dev/null @@ -1,5 +0,0 @@ -from physics.controllers.ElementController import ElementController - - -class PlayerController(ElementController): - pass diff --git a/project/physics/controllers/PlayerSprite.py b/project/physics/controllers/PlayerSprite.py new file mode 100644 index 0000000..5828166 --- /dev/null +++ b/project/physics/controllers/PlayerSprite.py @@ -0,0 +1,24 @@ +from physics.TickData import TickData +from sprite.DynamicSprite import DynamicSprite +from sprite.Spritesheet import Spritesheet +from ui_elements.KeyManager import KeyManager +from ui_elements.TextLabel import TextLabel + + +class PlayerSprite(DynamicSprite): + def __init__(self, spritesheet: Spritesheet): + super().__init__(spritesheet) + self.jump_time = -1 + self.allowed_jump_time = 20 + self.debug_label = TextLabel('', -1, -1) + + def tick(self, tick_data: TickData): + super().tick(tick_data) + + if tick_data.key_manager.is_keymap_down(KeyManager.KEY_RIGHT): + self.motion = (self.motion[0] + 2, self.motion[1]) + + if tick_data.key_manager.is_keymap_down(KeyManager.KEY_LEFT): + self.motion = (self.motion[0] - 2, self.motion[1]) + + self.debug_label.set_text(f'jump: {self.jump_time}, x: {round(self.motion[0], 2)}, y: {round(self.motion[1], 2)}, touches: {self.touches_bounding}') diff --git a/project/sprite/DynamicSprite.py b/project/sprite/DynamicSprite.py index 2570a23..65dc1c2 100644 --- a/project/sprite/DynamicSprite.py +++ b/project/sprite/DynamicSprite.py @@ -1,3 +1,4 @@ +from sprite.BoundingBox import BoundingBox from sprite.Spritesheet import Spritesheet from sprite.StaticSprite import StaticSprite from physics.TickData import TickData diff --git a/project/sprite/Sprite.py b/project/sprite/Sprite.py index 8c4a9d6..f6a5eb6 100644 --- a/project/sprite/Sprite.py +++ b/project/sprite/Sprite.py @@ -4,6 +4,7 @@ from physics.TickData import TickData from sprite.BoundingBox import BoundingBox from sprite.PositionScale import PositionScale from sprite.Spritesheet import Spritesheet +from ui_elements.KeyManager import KeyManager from ui_elements.UiElement import UiElement @@ -38,11 +39,14 @@ class Sprite(UiElement): if state in self.spritesheet.animations: self.animation_state = state self.animation_delay = 0 - self.tick(TickData(0, [], [], [], PositionScale())) + self.tick(self.empty_tick_data()) def set_animation_frame(self, frame: int): self.animation_frame = frame - self.tick(TickData(0, [], [], [], PositionScale())) + self.tick(self.empty_tick_data()) + + def empty_tick_data(self) -> 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