- 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
40 lines
No EOL
1.6 KiB
Python
40 lines
No EOL
1.6 KiB
Python
import pygame
|
|
import random
|
|
|
|
|
|
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[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(),
|
|
render_pos[1] % (surface.get_height() + self.image.get_height()) - self.image.get_height())
|
|
)
|
|
|
|
|
|
class 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.6 + 0.2))
|
|
self.clouds.sort(key=lambda x: x.depth)
|
|
|
|
def update(self) -> None:
|
|
for cloud in self.clouds:
|
|
cloud.update()
|
|
|
|
def render(self, surface: pygame.Surface, offset: tuple[int, int] = (0, 0)) -> None:
|
|
for cloud in self.clouds:
|
|
cloud.render(surface, offset) |