refactor: Typisierung in Hauptdateien und Skripten ergaenzt

- Felder und Methoden in Game und Editor typisiert
- Tilemap: Felder, render, tiles_around, physics_rects_around, autotile
- Animation: Konstruktor-Parameter, Felder, copy und update
- Clouds: Cloud und Clouds vollstaendig typisiert
- utils: load_image und load_images mit Rueckgabe-Typ
This commit is contained in:
binaerverkehr 2026-05-11 10:37:37 +02:00
parent d796b47c46
commit 4449656a69
6 changed files with 83 additions and 82 deletions

View file

@ -3,7 +3,7 @@ import sys
from scripts.tilemap import Tilemap
from scripts.utils import load_images
RENDER_SCALE = 2.0
RENDER_SCALE: float = 2.0
class better_list(list):
@ -16,45 +16,45 @@ class Editor:
pygame.init()
pygame.display.set_caption("Editor")
self.screen = pygame.display.set_mode((640, 480))
self.display = pygame.Surface((320, 240))
self.screen: pygame.Surface = pygame.display.set_mode((640, 480))
self.display: pygame.Surface = pygame.Surface((320, 240))
self.clock = pygame.time.Clock()
self.running = True
self.clock: pygame.time.Clock = pygame.time.Clock()
self.running: bool = True
self.collision_area = pygame.Rect(50, 50, 300, 50)
self.movement = [False, False, False, False]
self.collision_area: pygame.Rect = pygame.Rect(50, 50, 300, 50)
self.movement: list[bool] = [False, False, False, False]
self.assets = {
self.assets: dict[str, list[pygame.Surface]] = {
"decor": load_images("tiles/decor"),
"grass": load_images("tiles/grass"),
"large_decor": load_images("tiles/large_decor"),
"stone": load_images("tiles/stone"),
}
self.tilemap = Tilemap(self, 16)
self.tilemap: Tilemap = Tilemap(self, 16)
try:
self.tilemap.load("map.json")
except FileNotFoundError:
pass
self.isJumping = False
self.scroll = [0, 0]
self.tile_list = better_list(self.assets)
self.tile_group = 0
self.tile_variant = 0
self.isJumping: bool = False
self.scroll: list[float] = [0, 0]
self.tile_list: better_list = better_list(self.assets)
self.tile_group: int = 0
self.tile_variant: int = 0
self.clicking = False
self.right_clicking = False
self.shifting = False
self.clicking: bool = False
self.right_clicking: bool = False
self.shifting: bool = False
self.sekunde = 60
self.zaehler = 0
self.sekunde: int = 60
self.zaehler: int = 0
self.on_grid = True
self.on_grid: bool = True
def run(self):
def run(self) -> None:
while self.running:
self.display.fill((0, 0, 0))

26
game.py
View file

@ -13,16 +13,16 @@ class Game:
pygame.init()
pygame.display.set_caption("Jump and run spiel")
self.screen = pygame.display.set_mode((640, 480))
self.display = pygame.Surface((320, 240))
self.screen: pygame.Surface = pygame.display.set_mode((640, 480))
self.display: pygame.Surface = pygame.Surface((320, 240))
self.clock = pygame.time.Clock()
self.running = True
self.clock: pygame.time.Clock = pygame.time.Clock()
self.running: bool = True
self.collision_area = pygame.Rect(50, 50, 300, 50)
self.movement = [False, False]
self.collision_area: pygame.Rect = pygame.Rect(50, 50, 300, 50)
self.movement: list[bool] = [False, False]
self.assets = {
self.assets: dict = {
# Andere Dinge
"background": load_image("background.png"),
"decor": load_images("tiles/decor"),
@ -41,18 +41,18 @@ class Game:
"player/slide": Animation(load_images("entities/player/slide")),
"player/wall_slide": Animation(load_images("entities/player/wall_slide")),
}
self.player = Player(self, (50, 50), (8, 15))
self.player: Player = Player(self, (50, 50), (8, 15))
self.tilemap = Tilemap(self, 16)
self.tilemap: Tilemap = Tilemap(self, 16)
try:
self.tilemap.load("map.json")
except FileNotFoundError:
pass
self.clouds = Clouds(self.assets["clouds"], count=16)
self.isJumping = False
self.scroll = [0, 0]
self.clouds: Clouds = Clouds(self.assets["clouds"], count=16)
self.isJumping: bool = False
self.scroll: list[float] = [0, 0]
def run(self):
def run(self) -> None:
while self.running:
self.display.blit(self.assets["background"], (0, 0))

View file

@ -1,5 +1,5 @@
class Animation:
def __init__(self, images, image_duration=5, loop=True):
def __init__(self, images: list, image_duration: int = 5, loop: bool = True):
"""
Initialisiert eine Animation mit einer Liste von Bildern.
@ -11,13 +11,13 @@ class Animation:
- done startet bei False
"""
# TODO: Implementieren
self.images = images
self.image_duration = image_duration
self.loop = loop
self.frame = 0
self.done = False
self.images: list = images
self.image_duration: int = image_duration
self.loop: bool = loop
self.frame: int = 0
self.done: bool = False
def copy(self):
def copy(self) -> "Animation":
"""
Erstellt eine Kopie der Animation.
@ -29,7 +29,8 @@ class Animation:
# TODO: Implementieren
copy_animation = Animation(self.images, self.image_duration, self.loop)
return copy_animation
def update(self):
def update(self) -> None:
"""
Aktualisiert die Animation um einen Frame weiter.
@ -50,6 +51,7 @@ class Animation:
self.frame = min(self.frame + 1, len(self.images) * self.image_duration -1)
if self.frame >= self.image_duration * (len(self.images) - 1):
self.done = True
def img(self):
"""
Gibt das aktuelle Bild der Animation zurück.

View file

@ -1,45 +1,40 @@
import pygame
import random
class Cloud:
def __init__(self, pos:tuple, img, speed:float, depth):
self.pos = list(pos)
self.image = img
self.speed = speed
self.depth = depth
def update(self):
class Cloud:
def __init__(self, pos: tuple[float, float], img: pygame.Surface, speed: float, depth: float):
self.pos: list[float] = list(pos)
self.image: pygame.Surface = img
self.speed: float = speed
self.depth: float = depth
def update(self) -> None:
self.pos[0] += self.speed
def render(self, surface:pygame.Surface,offset:tuple=(0,0)):
def render(self, surface: pygame.Surface, offset: tuple[int, int] = (0, 0)) -> None:
render_pos = (self.pos[0] - offset[0] * self.depth, self.pos[1] - offset[1] * self.depth)
surface.blit(self.image,
(render_pos[0] % (surface.get_width() + self.image.get_width()) - self.image.get_width(),
surface.blit(self.image,
(render_pos[0] % (surface.get_width() + self.image.get_width()) - self.image.get_width(),
render_pos[1] % (surface.get_height() + self.image.get_height()) - self.image.get_height())
)
class Clouds:
def __init__(self, cloud_images, count=16):
self.clouds = []
def __init__(self, cloud_images: list[pygame.Surface], count: int = 16):
self.clouds: list[Cloud] = []
for _ in range(count):
self.clouds.append(Cloud((random.random() * 99999, random.random() * 99999),
random.choice(cloud_images),
(random.random() * 0.05 + 0.05) * 1,
(random.random() * 0.05 + 0.05) * 1,
random.random() * 0.6 + 0.2))
self.clouds.sort(key=lambda x: x.depth)
"""
self.clouds = []
# Erstelle die Wolken mit zufälligen Eigenschaften
for i in range(count):
self.clouds.append(Cloud((random.random() * 99999, random.random() * 99999),
random.choice(cloud_images),
random.random() * 0.05 + 0.05,
random.random() * 0.6 + 0.2)
)
self.clouds.sort(key=lambda x: x.depth)"""
def update(self):
def update(self) -> None:
for cloud in self.clouds:
cloud.update()
def render(self, surface:pygame.Surface, offset:tuple=(0,0)):
def render(self, surface: pygame.Surface, offset: tuple[int, int] = (0, 0)) -> None:
for cloud in self.clouds:
cloud.render(surface, offset)

View file

@ -32,9 +32,9 @@ class Tilemap:
def __init__(self, game, tile_size: int = 16):
self.game = game
self.tile_size = tile_size
self.tilemap = {}
self.offgrid_tiles = []
self.tile_size: int = tile_size
self.tilemap: dict[str, dict] = {}
self.offgrid_tiles: list[dict] = []
for i in range(10):
self.tilemap[str(3 + i) + ";10"] = {
"type": "grass",
@ -47,7 +47,7 @@ class Tilemap:
"pos": (10, i + 5),
}
def render(self, surface: pg.Surface, offset: tuple = (0, 0)):
def render(self, surface: pg.Surface, offset: tuple[int, int] = (0, 0)) -> None:
for tile in self.offgrid_tiles:
surface.blit(
self.game.assets[tile["type"]][tile["variant"]],
@ -72,8 +72,8 @@ class Tilemap:
),
)
def tiles_around(self, pos: list):
tiles = []
def tiles_around(self, pos: list[float]) -> list[dict]:
tiles: list[dict] = []
tile_location = (int(pos[0] // self.tile_size), int(pos[1] // self.tile_size))
for offset in NEIGHBOR_OFFSETS:
check_location = (
@ -86,9 +86,9 @@ class Tilemap:
return tiles
def physics_rects_around(self, pos: list) -> list:
def physics_rects_around(self, pos: list[float]) -> list[pg.Rect]:
# Erzeuge eine leere Liste für die Rechtecke
rects = []
rects: list[pg.Rect] = []
# Durchlaufe alle Tiles aus der Umgebung (tiles_around)
for tile in self.tiles_around(pos):
# Prüfe, ob der Tile-Typ in PHYSICS_TILES enthalten ist
@ -110,7 +110,7 @@ class Tilemap:
# Gib die Liste der Rechtecke zurück
return rects
def autotile(self):
def autotile(self) -> None:
for loc in self.tilemap:
tile = self.tilemap[loc]

View file

@ -1,13 +1,17 @@
import pygame
import os
BASE_IMG_PATH = 'data/images/'
def load_image(path):
BASE_IMG_PATH: str = 'data/images/'
def load_image(path: str) -> pygame.Surface:
img = pygame.image.load(BASE_IMG_PATH + path).convert()
img.set_colorkey((0, 0, 0))
return img
def load_images(path):
images = []
def load_images(path: str) -> list[pygame.Surface]:
images: list[pygame.Surface] = []
for img_name in sorted(os.listdir(BASE_IMG_PATH + path)):
images.append(load_image(path + '/' + img_name))
return images