From 8e465ba27ba00a8d6d85062aa4a6f431e5b88ce1 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Wed, 10 Jun 2026 18:23:23 +0200 Subject: [PATCH] =?UTF-8?q?Gegner-Logik=20hinzugef=C3=BCgt=20und=20Tilemap?= =?UTF-8?q?=20aktualisiert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game.py | 12 ++++++++++-- scripts/entities.py | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/game.py b/game.py index bff2006..60aff50 100644 --- a/game.py +++ b/game.py @@ -2,7 +2,7 @@ import pygame # ty: ignore[unresolved-import] import sys import random import math -from scripts.entities import Player +from scripts.entities import Enemy, Player from scripts.utils import load_image from scripts.utils import load_images from scripts.tilemap import Tilemap @@ -52,6 +52,10 @@ class Game: "player/wall_slide": Animation( load_images(path="entities/player/wall_slide") ), + + "enemy/idle": Animation(load_images(path="entities/enemy/idle"), image_duration=6), + "enemy/run": Animation(load_images(path="entities/enemy/run"), image_duration=4), + "particles/leaf": Animation( load_images(path="particles/leaf"), image_duration=20, loop=False ), @@ -72,6 +76,7 @@ class Game: pass self.leaf_spawners: list[pygame.Rect] = [] + self.enemys: list[Enemy] = [] for tree in self.tilemap.extract([("large_decor", 2)], keep=True): self.leaf_spawners.append( @@ -82,7 +87,7 @@ class Game: if spawner["variant"] == 0: self.player.pos = spawner["pos"] else: - print(spawner["pos"], "enemy") + self.enemys.append(Enemy(self, spawner["pos"], (8, 15))) self.particles: list[Particle] = [] @@ -128,6 +133,9 @@ class Game: self.clouds.update() self.clouds.render(surface=self.display, offset=render_scroll) self.tilemap.render(surface=self.display, offset=render_scroll) + for enemy in self.enemys.copy(): + enemy.update(self.tilemap, (0, 0)) + enemy.render(self.display, offset=render_scroll) self.player.update( self.tilemap, movement=((self.movement[1] - self.movement[0]), 0) ) diff --git a/scripts/entities.py b/scripts/entities.py index 30b36e0..1821df5 100644 --- a/scripts/entities.py +++ b/scripts/entities.py @@ -105,7 +105,22 @@ class PhysicsEntity: image = self.e_type + "/" + self.action self.animation = self.game.assets[image].copy() +class Enemy(PhysicsEntity): + def __init__(self, game, pos: tuple[float, float], size: tuple[int, int]): + super().__init__(game, "enemy", pos, size) + self.walking = 0 + + def update(self, tilemap, movement: tuple[float, float] = (0, 0)): + if self.walking: + movement = (movement[0] - 0.5 if self.flip else 0.5, movement[1]) + self.walking = max(0, self.walking - 1) + elif random.random() < 0.01: + self.walking = random.randint(30, 120) + + super().update(tilemap, movement) + + class Player(PhysicsEntity): def __init__(self, game, pos: tuple[float, float], size: tuple[int, int]): super().__init__(game, "player", pos, size)