feat: Collisions Dataclass eingefuehrt und in PhysicsEntity verwendet

- Neue @dataclass Collisions mit Feldern top/right/bottom/left
- collisions-Dict in PhysicsEntity durch Collisions-Instanz ersetzt
- Attribut-Zugriff (collisions.top) statt Dict-Indexierung
- Typisierung von Feldern und Methoden in PhysicsEntity und Player
This commit is contained in:
binaerverkehr 2026-05-11 10:37:28 +02:00
parent da973dd968
commit d796b47c46

View file

@ -1,26 +1,37 @@
import pygame
from dataclasses import dataclass
from scripts.animation import Animation
from scripts.tilemap import Tilemap
@dataclass
class Collisions:
top: bool = False
right: bool = False
bottom: bool = False
left: bool = False
class PhysicsEntity:
def __init__(self, game, e_type, pos: tuple, size):
def __init__(self, game, e_type: str, pos: tuple[float, float], size: tuple[int, int]):
self.game = game
self.e_type = e_type
self.pos = list(pos)
self.size = size
self.velocity = [0, 0]
self.collisions = {"top": False, "bottom": False, "left": False, "right": False}
self.speed = 3
self.e_type: str = e_type
self.pos: list[float] = list(pos)
self.size: tuple[int, int] = size
self.velocity: list[float] = [0, 0]
self.collisions: Collisions = Collisions()
self.speed: int = 3
# Animationen
self.action = ""
self.flip = False
self.animation_offset = (-3, -3)
self.action: str = ""
self.flip: bool = False
self.animation_offset: tuple[int, int] = (-3, -3)
self.animation: Animation
self.set_action("idle")
def update(self, tilemap: Tilemap, movement: tuple = (0, 0)):
self.collisions = {"top": False, "bottom": False, "left": False, "right": False}
frame_movement = (
def update(self, tilemap: Tilemap, movement: tuple[float, float] = (0, 0)) -> None:
self.collisions = Collisions()
frame_movement: tuple[float, float] = (
(movement[0] + self.velocity[0]) * self.speed,
movement[1] + self.velocity[1],
)
@ -31,12 +42,12 @@ class PhysicsEntity:
if entity_rect.colliderect(recto):
if frame_movement[0] > 0:
entity_rect.right = recto.left
self.collisions["right"] = True
self.collisions.right = True
self.game.isJumping = False
if frame_movement[0] < 0:
entity_rect.left = recto.right
self.collisions["left"] = True
self.collisions.left = True
self.game.isJumping = False
self.pos[0] = entity_rect.x
@ -48,12 +59,12 @@ class PhysicsEntity:
if entity_rect.colliderect(rectolino):
if frame_movement[1] > 0:
entity_rect.bottom = rectolino.top
self.collisions["bottom"] = True
self.collisions.bottom = True
self.game.isJumping = False
if frame_movement[1] < 0:
entity_rect.top = rectolino.bottom
self.collisions["top"] = True
self.collisions.top = True
self.pos[1] = entity_rect.y
@ -62,14 +73,14 @@ class PhysicsEntity:
if movement[0] < 0:
self.flip = True
if self.collisions["bottom"] or self.collisions["top"]:
if self.collisions.bottom or self.collisions.top:
self.velocity[1] = 0
self.velocity[1] = min(5, self.velocity[1] + 0.1)
self.animation.update()
def render(self, surface: pygame.Surface, offset: tuple = (0, 0)):
def render(self, surface: pygame.Surface, offset: tuple[int, int] = (0, 0)) -> None:
surface.blit(
pygame.transform.flip(self.animation.img(), self.flip, False),
(
@ -78,10 +89,10 @@ class PhysicsEntity:
),
)
def rect(self):
def rect(self) -> pygame.Rect:
return pygame.Rect(self.pos[0], self.pos[1], self.size[0], self.size[1])
def set_action(self, action):
def set_action(self, action: str) -> None:
if action != self.action:
self.action = action
image = self.e_type + "/" + self.action
@ -89,16 +100,16 @@ class PhysicsEntity:
class Player(PhysicsEntity):
def __init__(self, game, pos, size):
def __init__(self, game, pos: tuple[float, float], size: tuple[int, int]):
super().__init__(game, "player", pos, size)
self.air_time = 0 # Neu! Zählt, wie lange wir fallen/springe
self.air_time: int = 0 # Neu! Zählt, wie lange wir fallen/springe
def update(self, tilemap, movement=(0, 0)):
def update(self, tilemap: Tilemap, movement: tuple[float, float] = (0, 0)) -> None:
super().update(tilemap, movement)
self.air_time += 1
if self.collisions["bottom"]:
if self.collisions.bottom:
self.air_time = 0
# Wenn wir länger als 4 Frames in der Luft sind -> Springen!
if self.air_time > 4: