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:
parent
d796b47c46
commit
4449656a69
6 changed files with 83 additions and 82 deletions
42
editor.py
42
editor.py
|
|
@ -3,7 +3,7 @@ import sys
|
||||||
from scripts.tilemap import Tilemap
|
from scripts.tilemap import Tilemap
|
||||||
from scripts.utils import load_images
|
from scripts.utils import load_images
|
||||||
|
|
||||||
RENDER_SCALE = 2.0
|
RENDER_SCALE: float = 2.0
|
||||||
|
|
||||||
|
|
||||||
class better_list(list):
|
class better_list(list):
|
||||||
|
|
@ -16,45 +16,45 @@ class Editor:
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
|
||||||
pygame.display.set_caption("Editor")
|
pygame.display.set_caption("Editor")
|
||||||
self.screen = pygame.display.set_mode((640, 480))
|
self.screen: pygame.Surface = pygame.display.set_mode((640, 480))
|
||||||
self.display = pygame.Surface((320, 240))
|
self.display: pygame.Surface = pygame.Surface((320, 240))
|
||||||
|
|
||||||
self.clock = pygame.time.Clock()
|
self.clock: pygame.time.Clock = pygame.time.Clock()
|
||||||
self.running = True
|
self.running: bool = True
|
||||||
|
|
||||||
self.collision_area = pygame.Rect(50, 50, 300, 50)
|
self.collision_area: pygame.Rect = pygame.Rect(50, 50, 300, 50)
|
||||||
self.movement = [False, False, False, False]
|
self.movement: list[bool] = [False, False, False, False]
|
||||||
|
|
||||||
self.assets = {
|
self.assets: dict[str, list[pygame.Surface]] = {
|
||||||
"decor": load_images("tiles/decor"),
|
"decor": load_images("tiles/decor"),
|
||||||
"grass": load_images("tiles/grass"),
|
"grass": load_images("tiles/grass"),
|
||||||
"large_decor": load_images("tiles/large_decor"),
|
"large_decor": load_images("tiles/large_decor"),
|
||||||
"stone": load_images("tiles/stone"),
|
"stone": load_images("tiles/stone"),
|
||||||
}
|
}
|
||||||
|
|
||||||
self.tilemap = Tilemap(self, 16)
|
self.tilemap: Tilemap = Tilemap(self, 16)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.tilemap.load("map.json")
|
self.tilemap.load("map.json")
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
self.isJumping = False
|
self.isJumping: bool = False
|
||||||
self.scroll = [0, 0]
|
self.scroll: list[float] = [0, 0]
|
||||||
self.tile_list = better_list(self.assets)
|
self.tile_list: better_list = better_list(self.assets)
|
||||||
self.tile_group = 0
|
self.tile_group: int = 0
|
||||||
self.tile_variant = 0
|
self.tile_variant: int = 0
|
||||||
|
|
||||||
self.clicking = False
|
self.clicking: bool = False
|
||||||
self.right_clicking = False
|
self.right_clicking: bool = False
|
||||||
self.shifting = False
|
self.shifting: bool = False
|
||||||
|
|
||||||
self.sekunde = 60
|
self.sekunde: int = 60
|
||||||
self.zaehler = 0
|
self.zaehler: int = 0
|
||||||
|
|
||||||
self.on_grid = True
|
self.on_grid: bool = True
|
||||||
|
|
||||||
def run(self):
|
def run(self) -> None:
|
||||||
while self.running:
|
while self.running:
|
||||||
self.display.fill((0, 0, 0))
|
self.display.fill((0, 0, 0))
|
||||||
|
|
||||||
|
|
|
||||||
26
game.py
26
game.py
|
|
@ -13,16 +13,16 @@ class Game:
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
|
||||||
pygame.display.set_caption("Jump and run spiel")
|
pygame.display.set_caption("Jump and run spiel")
|
||||||
self.screen = pygame.display.set_mode((640, 480))
|
self.screen: pygame.Surface = pygame.display.set_mode((640, 480))
|
||||||
self.display = pygame.Surface((320, 240))
|
self.display: pygame.Surface = pygame.Surface((320, 240))
|
||||||
|
|
||||||
self.clock = pygame.time.Clock()
|
self.clock: pygame.time.Clock = pygame.time.Clock()
|
||||||
self.running = True
|
self.running: bool = True
|
||||||
|
|
||||||
self.collision_area = pygame.Rect(50, 50, 300, 50)
|
self.collision_area: pygame.Rect = pygame.Rect(50, 50, 300, 50)
|
||||||
self.movement = [False, False]
|
self.movement: list[bool] = [False, False]
|
||||||
|
|
||||||
self.assets = {
|
self.assets: dict = {
|
||||||
# Andere Dinge
|
# Andere Dinge
|
||||||
"background": load_image("background.png"),
|
"background": load_image("background.png"),
|
||||||
"decor": load_images("tiles/decor"),
|
"decor": load_images("tiles/decor"),
|
||||||
|
|
@ -41,18 +41,18 @@ class Game:
|
||||||
"player/slide": Animation(load_images("entities/player/slide")),
|
"player/slide": Animation(load_images("entities/player/slide")),
|
||||||
"player/wall_slide": Animation(load_images("entities/player/wall_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:
|
try:
|
||||||
self.tilemap.load("map.json")
|
self.tilemap.load("map.json")
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
self.clouds = Clouds(self.assets["clouds"], count=16)
|
self.clouds: Clouds = Clouds(self.assets["clouds"], count=16)
|
||||||
self.isJumping = False
|
self.isJumping: bool = False
|
||||||
self.scroll = [0, 0]
|
self.scroll: list[float] = [0, 0]
|
||||||
|
|
||||||
def run(self):
|
def run(self) -> None:
|
||||||
while self.running:
|
while self.running:
|
||||||
self.display.blit(self.assets["background"], (0, 0))
|
self.display.blit(self.assets["background"], (0, 0))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
class Animation:
|
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.
|
Initialisiert eine Animation mit einer Liste von Bildern.
|
||||||
|
|
||||||
|
|
@ -11,13 +11,13 @@ class Animation:
|
||||||
- done startet bei False
|
- done startet bei False
|
||||||
"""
|
"""
|
||||||
# TODO: Implementieren
|
# TODO: Implementieren
|
||||||
self.images = images
|
self.images: list = images
|
||||||
self.image_duration = image_duration
|
self.image_duration: int = image_duration
|
||||||
self.loop = loop
|
self.loop: bool = loop
|
||||||
self.frame = 0
|
self.frame: int = 0
|
||||||
self.done = False
|
self.done: bool = False
|
||||||
|
|
||||||
def copy(self):
|
def copy(self) -> "Animation":
|
||||||
"""
|
"""
|
||||||
Erstellt eine Kopie der Animation.
|
Erstellt eine Kopie der Animation.
|
||||||
|
|
||||||
|
|
@ -29,7 +29,8 @@ class Animation:
|
||||||
# TODO: Implementieren
|
# TODO: Implementieren
|
||||||
copy_animation = Animation(self.images, self.image_duration, self.loop)
|
copy_animation = Animation(self.images, self.image_duration, self.loop)
|
||||||
return copy_animation
|
return copy_animation
|
||||||
def update(self):
|
|
||||||
|
def update(self) -> None:
|
||||||
"""
|
"""
|
||||||
Aktualisiert die Animation um einen Frame weiter.
|
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)
|
self.frame = min(self.frame + 1, len(self.images) * self.image_duration -1)
|
||||||
if self.frame >= self.image_duration * (len(self.images) - 1):
|
if self.frame >= self.image_duration * (len(self.images) - 1):
|
||||||
self.done = True
|
self.done = True
|
||||||
|
|
||||||
def img(self):
|
def img(self):
|
||||||
"""
|
"""
|
||||||
Gibt das aktuelle Bild der Animation zurück.
|
Gibt das aktuelle Bild der Animation zurück.
|
||||||
|
|
|
||||||
|
|
@ -1,45 +1,40 @@
|
||||||
import pygame
|
import pygame
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
class Cloud:
|
class Cloud:
|
||||||
def __init__(self, pos:tuple, img, speed:float, depth):
|
def __init__(self, pos: tuple[float, float], img: pygame.Surface, speed: float, depth: float):
|
||||||
self.pos = list(pos)
|
self.pos: list[float] = list(pos)
|
||||||
self.image = img
|
self.image: pygame.Surface = img
|
||||||
self.speed = speed
|
self.speed: float = speed
|
||||||
self.depth = depth
|
self.depth: float = depth
|
||||||
|
|
||||||
|
def update(self) -> None:
|
||||||
def update(self):
|
|
||||||
self.pos[0] += self.speed
|
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)
|
render_pos = (self.pos[0] - offset[0] * self.depth, self.pos[1] - offset[1] * self.depth)
|
||||||
|
|
||||||
surface.blit(self.image,
|
surface.blit(self.image,
|
||||||
(render_pos[0] % (surface.get_width() + self.image.get_width()) - self.image.get_width(),
|
(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())
|
render_pos[1] % (surface.get_height() + self.image.get_height()) - self.image.get_height())
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class Clouds:
|
class Clouds:
|
||||||
def __init__(self, cloud_images, count=16):
|
def __init__(self, cloud_images: list[pygame.Surface], count: int = 16):
|
||||||
self.clouds = []
|
self.clouds: list[Cloud] = []
|
||||||
for _ in range(count):
|
for _ in range(count):
|
||||||
self.clouds.append(Cloud((random.random() * 99999, random.random() * 99999),
|
self.clouds.append(Cloud((random.random() * 99999, random.random() * 99999),
|
||||||
random.choice(cloud_images),
|
random.choice(cloud_images),
|
||||||
(random.random() * 0.05 + 0.05) * 1,
|
(random.random() * 0.05 + 0.05) * 1,
|
||||||
random.random() * 0.6 + 0.2))
|
random.random() * 0.6 + 0.2))
|
||||||
self.clouds.sort(key=lambda x: x.depth)
|
self.clouds.sort(key=lambda x: x.depth)
|
||||||
"""
|
|
||||||
self.clouds = []
|
|
||||||
|
|
||||||
# Erstelle die Wolken mit zufälligen Eigenschaften
|
def update(self) -> None:
|
||||||
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):
|
|
||||||
for cloud in self.clouds:
|
for cloud in self.clouds:
|
||||||
cloud.update()
|
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:
|
for cloud in self.clouds:
|
||||||
cloud.render(surface, offset)
|
cloud.render(surface, offset)
|
||||||
|
|
@ -32,9 +32,9 @@ class Tilemap:
|
||||||
|
|
||||||
def __init__(self, game, tile_size: int = 16):
|
def __init__(self, game, tile_size: int = 16):
|
||||||
self.game = game
|
self.game = game
|
||||||
self.tile_size = tile_size
|
self.tile_size: int = tile_size
|
||||||
self.tilemap = {}
|
self.tilemap: dict[str, dict] = {}
|
||||||
self.offgrid_tiles = []
|
self.offgrid_tiles: list[dict] = []
|
||||||
for i in range(10):
|
for i in range(10):
|
||||||
self.tilemap[str(3 + i) + ";10"] = {
|
self.tilemap[str(3 + i) + ";10"] = {
|
||||||
"type": "grass",
|
"type": "grass",
|
||||||
|
|
@ -47,7 +47,7 @@ class Tilemap:
|
||||||
"pos": (10, i + 5),
|
"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:
|
for tile in self.offgrid_tiles:
|
||||||
surface.blit(
|
surface.blit(
|
||||||
self.game.assets[tile["type"]][tile["variant"]],
|
self.game.assets[tile["type"]][tile["variant"]],
|
||||||
|
|
@ -72,8 +72,8 @@ class Tilemap:
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
def tiles_around(self, pos: list):
|
def tiles_around(self, pos: list[float]) -> list[dict]:
|
||||||
tiles = []
|
tiles: list[dict] = []
|
||||||
tile_location = (int(pos[0] // self.tile_size), int(pos[1] // self.tile_size))
|
tile_location = (int(pos[0] // self.tile_size), int(pos[1] // self.tile_size))
|
||||||
for offset in NEIGHBOR_OFFSETS:
|
for offset in NEIGHBOR_OFFSETS:
|
||||||
check_location = (
|
check_location = (
|
||||||
|
|
@ -86,9 +86,9 @@ class Tilemap:
|
||||||
|
|
||||||
return tiles
|
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
|
# Erzeuge eine leere Liste für die Rechtecke
|
||||||
rects = []
|
rects: list[pg.Rect] = []
|
||||||
# Durchlaufe alle Tiles aus der Umgebung (tiles_around)
|
# Durchlaufe alle Tiles aus der Umgebung (tiles_around)
|
||||||
for tile in self.tiles_around(pos):
|
for tile in self.tiles_around(pos):
|
||||||
# Prüfe, ob der Tile-Typ in PHYSICS_TILES enthalten ist
|
# Prüfe, ob der Tile-Typ in PHYSICS_TILES enthalten ist
|
||||||
|
|
@ -110,7 +110,7 @@ class Tilemap:
|
||||||
# Gib die Liste der Rechtecke zurück
|
# Gib die Liste der Rechtecke zurück
|
||||||
return rects
|
return rects
|
||||||
|
|
||||||
def autotile(self):
|
def autotile(self) -> None:
|
||||||
|
|
||||||
for loc in self.tilemap:
|
for loc in self.tilemap:
|
||||||
tile = self.tilemap[loc]
|
tile = self.tilemap[loc]
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,17 @@
|
||||||
import pygame
|
import pygame
|
||||||
import os
|
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 = pygame.image.load(BASE_IMG_PATH + path).convert()
|
||||||
img.set_colorkey((0, 0, 0))
|
img.set_colorkey((0, 0, 0))
|
||||||
return img
|
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)):
|
for img_name in sorted(os.listdir(BASE_IMG_PATH + path)):
|
||||||
images.append(load_image(path + '/' + img_name))
|
images.append(load_image(path + '/' + img_name))
|
||||||
return images
|
return images
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue