From 261cc92897588e4f892af97e46f5fb56e4a6175a Mon Sep 17 00:00:00 2001 From: Benjamin Date: Wed, 20 May 2026 17:57:59 +0200 Subject: [PATCH] =?UTF-8?q?Zuf=C3=A4lliges=20Spawnen=20von=20Blatt-Partike?= =?UTF-8?q?ln=20in=20Spawn-Rechtecken?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game.py | 54 ++++++++++++++++++++++++++++++++++++--------- scripts/particle.py | 30 ++++++++++++------------- 2 files changed, 58 insertions(+), 26 deletions(-) diff --git a/game.py b/game.py index a67ee1a..0a52b6e 100644 --- a/game.py +++ b/game.py @@ -1,22 +1,29 @@ -import pygame # ty: ignore[unresolved-import] +import pygame # ty: ignore[unresolved-import] import sys +import random from scripts.entities import Player from scripts.utils import load_image from scripts.utils import load_images from scripts.tilemap import Tilemap from scripts.clouds import Clouds from scripts.animation import Animation +from scripts.particle import Particle SCREEN_WIDTH = 640 SCREEN_HEIGHT = 480 + class Game: def __init__(self): pygame.init() - + pygame.display.set_caption("Jump and run spiel") - self.screen: pygame.Surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) - self.display: pygame.Surface = pygame.Surface((self.screen.get_width()//2, self.screen.get_height()//2)) + self.screen: pygame.Surface = pygame.display.set_mode( + (SCREEN_WIDTH, SCREEN_HEIGHT) + ) + self.display: pygame.Surface = pygame.Surface( + (self.screen.get_width() // 2, self.screen.get_height() // 2) + ) self.clock: pygame.time.Clock = pygame.time.Clock() self.running: bool = True @@ -41,8 +48,12 @@ class Game: ), "player/jump": Animation(load_images(path="entities/player/jump")), "player/slide": Animation(load_images(path="entities/player/slide")), - "player/wall_slide": Animation(load_images(path="entities/player/wall_slide")), - "particles/leaf": Animation(load_images(path="particles/leaf"), image_duration=20, loop=False), + "player/wall_slide": Animation( + load_images(path="entities/player/wall_slide") + ), + "particles/leaf": Animation( + load_images(path="particles/leaf"), image_duration=20, loop=False + ), } self.player: Player = Player(game=self, pos=(50, 50), size=(8, 15)) @@ -55,11 +66,15 @@ class Game: self.leaf_spawners: list[pygame.Rect] = [] - self.particles = [] for tree in self.tilemap.extract([("large_decor", 2)], keep=True): - self.leaf_spawners.append(pygame.Rect(left=4 + tree['pos'][0], top=4 + tree['pos'][1], width=23, height=13)) - print(len(self.leaf_spawners), "Baum-Spawner gefunden") + self.leaf_spawners.append( + pygame.Rect( + 4 + tree["pos"][0], 4 + tree["pos"][1], 23, 13 + ) + ) + + self.particles = [] self.clouds: Clouds = Clouds(cloud_images=self.assets["clouds"], count=16) self.isJumping: bool = False @@ -81,16 +96,33 @@ class Game: ) / 30 render_scroll: tuple[int, int] = (int(self.scroll[0]), int(self.scroll[1])) + for rect in self.leaf_spawners: + if random.random() * 49999 < rect.width * rect.height: + pos = ( + rect.x + random.random() * rect.width, + rect.y + random.random() * rect.height, + ) + self.particles.append( + Particle( + self, + "leaf", + pos, + velocity=[-0.1, 0.3], + frame=random.randint(0, 20), + ) + ) + self.clouds.update() self.clouds.render(surface=self.display, offset=render_scroll) self.tilemap.render(surface=self.display, offset=render_scroll) - self.player.update(self.tilemap, movement=((self.movement[1] - self.movement[0]), 0)) + self.player.update( + self.tilemap, movement=((self.movement[1] - self.movement[0]), 0) + ) self.player.render(surface=self.display, offset=render_scroll) # print(int(self.player.pos[0])) # print(int(self.player.pos[1])) # print(self.tilemap.tiles_around(self.player.pos)) - for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False diff --git a/scripts/particle.py b/scripts/particle.py index 9ed40da..6cb6f2d 100644 --- a/scripts/particle.py +++ b/scripts/particle.py @@ -4,32 +4,32 @@ from scripts.animation import Animation class Particle: - def __init__(self, game, p_type, pos: tuple, velocity=[0, 0], frame=0): + def __init__(self, game, p_type: str, pos: tuple[float, float], velocity: list[float] = [0, 0], frame: int = 0): self.game = game - self.p_type = p_type - self.pos = list(pos) - self.velocity = list(velocity) - self.frame = frame - - self.animation: Animation = self.game.assets["particles/" + self.p_type].copy() + self.type: str = p_type + self.pos: list[float] = list(pos) + self.velocity: list[float] = list(velocity) + self.animation: Animation = self.game.assets['particles/' + p_type].copy() self.animation.frame = frame def update(self) -> bool: - - kill = False - + kill: bool = False if self.animation.done: kill = True self.pos[0] += self.velocity[0] self.pos[1] += self.velocity[1] + self.animation.update() return kill - def render(self, surface: pygame.Surface, offset: tuple[float, float]=(0, 0)): + def render(self, surface: pygame.Surface, offset: tuple[float, float] = (0, 0)) -> None: img = self.animation.img() - surface.blit(img, ( - self.pos[0] - offset[0] - img.get_width() // 2, - self.pos[1] - offset[1] - img.get_height() // 2, - )) \ No newline at end of file + surface.blit( + img, + ( + self.pos[0] - offset[0] - img.get_width() // 2, + self.pos[1] - offset[1] - img.get_height() // 2, + ), + ) \ No newline at end of file