Zufälliges Spawnen von Blatt-Partikeln in Spawn-Rechtecken

This commit is contained in:
Benjamin 2026-05-20 17:57:59 +02:00
parent 739481f18a
commit 261cc92897
2 changed files with 58 additions and 26 deletions

54
game.py
View file

@ -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

View file

@ -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,
))
surface.blit(
img,
(
self.pos[0] - offset[0] - img.get_width() // 2,
self.pos[1] - offset[1] - img.get_height() // 2,
),
)