Added files for testing level loading and controller

main
Yan Wittmann 2023-03-26 15:00:29 +02:00
parent a3a09f2c11
commit cd8fa05c41
14 changed files with 115 additions and 64 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 190 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 155 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 143 B

View File

@ -10,8 +10,8 @@
3, 3,
1 1
], ],
"height": 16, "width": 16,
"width": 16 "height": 16
}, },
{ {
"id": "idle", "id": "idle",
@ -22,8 +22,8 @@
1, 1,
1 1
], ],
"height": 16, "width": 16,
"width": 16 "height": 16
}, },
{ {
"id": "walk_r", "id": "walk_r",
@ -33,8 +33,8 @@
1, 1,
1 1
], ],
"height": 16, "width": 16,
"width": 16 "height": 16
}, },
{ {
"id": "other_test", "id": "other_test",
@ -43,8 +43,49 @@
1, 1,
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
} }
] ]
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 148 B

View File

@ -2,18 +2,14 @@ import pygame
from sprite.PositionScale import PositionScale from sprite.PositionScale import PositionScale
from ui_elements.ClickEvent import ClickEvent from ui_elements.ClickEvent import ClickEvent
from ui_elements.KeyEvent import KeyEvent from ui_elements.KeyManager import KeyManager
class TickData: 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): click_events: list[ClickEvent], screen_transform: PositionScale):
self.dt = dt self.dt = dt
self.pygame_events = pygame_events self.pygame_events = pygame_events
self.key_events = key_events self.key_manager = key_manager
self.click_events = click_events self.click_events = click_events
self.screen_transform = screen_transform self.screen_transform = screen_transform
@staticmethod
def empty():
pass

View File

@ -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

View File

@ -1,5 +0,0 @@
from physics.controllers.ElementController import ElementController
class PlayerController(ElementController):
pass

View File

@ -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}')

View File

@ -1,3 +1,4 @@
from sprite.BoundingBox import BoundingBox
from sprite.Spritesheet import Spritesheet from sprite.Spritesheet import Spritesheet
from sprite.StaticSprite import StaticSprite from sprite.StaticSprite import StaticSprite
from physics.TickData import TickData from physics.TickData import TickData

View File

@ -4,6 +4,7 @@ from physics.TickData import TickData
from sprite.BoundingBox import BoundingBox from sprite.BoundingBox import BoundingBox
from sprite.PositionScale import PositionScale from sprite.PositionScale import PositionScale
from sprite.Spritesheet import Spritesheet from sprite.Spritesheet import Spritesheet
from ui_elements.KeyManager import KeyManager
from ui_elements.UiElement import UiElement from ui_elements.UiElement import UiElement
@ -38,11 +39,14 @@ class Sprite(UiElement):
if state in self.spritesheet.animations: if state in self.spritesheet.animations:
self.animation_state = state self.animation_state = state
self.animation_delay = 0 self.animation_delay = 0
self.tick(TickData(0, [], [], [], PositionScale())) self.tick(self.empty_tick_data())
def set_animation_frame(self, frame: int): def set_animation_frame(self, frame: int):
self.animation_frame = frame 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: def render_sprite_image(self) -> pygame.Surface:
return self.image return self.image

View File

@ -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]

View File

@ -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

View File

@ -12,7 +12,7 @@ class TextLabel(UiElement):
def tick(self, tick_data: TickData): def tick(self, tick_data: TickData):
pass 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__() super().__init__()
self.text = text self.text = text