Ninja-Jump-and-run/scripts/clouds.py

40 lines
1.6 KiB
Python
Raw Permalink Normal View History

2026-02-06 17:51:57 +00:00
import pygame
import random
2026-02-06 17:51:57 +00:00
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:
2026-02-06 17:51:57 +00:00
self.pos[0] += self.speed
def render(self, surface: pygame.Surface, offset: tuple[int, int] = (0, 0)) -> None:
2026-02-06 17:51:57 +00:00
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(),
2026-02-06 17:51:57 +00:00
render_pos[1] % (surface.get_height() + self.image.get_height()) - self.image.get_height())
)
2026-02-06 17:51:57 +00:00
class Clouds:
def __init__(self, cloud_images: list[pygame.Surface], count: int = 16):
self.clouds: list[Cloud] = []
2026-02-06 17:51:57 +00:00
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,
2026-02-06 17:51:57 +00:00
random.random() * 0.6 + 0.2))
self.clouds.sort(key=lambda x: x.depth)
def update(self) -> None:
2026-02-06 17:51:57 +00:00
for cloud in self.clouds:
cloud.update()
def render(self, surface: pygame.Surface, offset: tuple[int, int] = (0, 0)) -> None:
2026-02-06 17:51:57 +00:00
for cloud in self.clouds:
cloud.render(surface, offset)