Gegner-Logik hinzugefügt und Tilemap aktualisiert
This commit is contained in:
parent
62c6ae19d8
commit
8e465ba27b
2 changed files with 25 additions and 2 deletions
12
game.py
12
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)
|
||||
)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue