Füge Boost- und Dash-Funktionen für den Spieler hinzu; verbessere die Physik-Logik und aktualisiere die Partikel-Generierung

This commit is contained in:
Benjamin 2026-06-10 18:00:11 +02:00
parent e1b5a42137
commit 1e4059dc0d
4 changed files with 151 additions and 18 deletions

2
.gitignore vendored
View file

@ -222,4 +222,4 @@ spickzettel.md
start.bat start.bat
Dateien erstellen.txt Dateien erstellen.txt
map.json map.json
testen testing/

View file

@ -57,7 +57,7 @@ class Editor:
# Scroll-Offset fürs Rendern # Scroll-Offset fürs Rendern
render_scroll: tuple[int, int] = (int(self.scroll[0]), int(self.scroll[1])) render_scroll: tuple[int, int] = (int(self.scroll[0]), int(self.scroll[1]))
#
self.tilemap.render(surface=self.display, offset=render_scroll) self.tilemap.render(surface=self.display, offset=render_scroll)
# Aktuelle Kachel (Kopie) # Aktuelle Kachel (Kopie)

30
game.py
View file

@ -55,6 +55,12 @@ class Game:
"particles/leaf": Animation( "particles/leaf": Animation(
load_images(path="particles/leaf"), image_duration=20, loop=False load_images(path="particles/leaf"), image_duration=20, loop=False
), ),
"particles/boost": Animation(
load_images(path="particles/boost"), image_duration=20, loop=False
),
"particles/particle": Animation(
load_images(path="particles/particle"), image_duration=6, loop=False
)
} }
self.player: Player = Player(game=self, pos=(50, 50), size=(8, 15)) self.player: Player = Player(game=self, pos=(50, 50), size=(8, 15))
@ -77,6 +83,7 @@ class Game:
self.clouds: Clouds = Clouds(cloud_images=self.assets["clouds"], count=16) self.clouds: Clouds = Clouds(cloud_images=self.assets["clouds"], count=16)
self.isJumping: bool = False self.isJumping: bool = False
self.scroll: list[float] = [0, 0] self.scroll: list[float] = [0, 0]
# self.boost: bool = False
def run(self) -> None: def run(self) -> None:
while self.running: while self.running:
@ -117,6 +124,7 @@ class Game:
self.tilemap, movement=((self.movement[1] - self.movement[0]), 0) self.tilemap, movement=((self.movement[1] - self.movement[0]), 0)
) )
self.player.render(surface=self.display, offset=render_scroll) self.player.render(surface=self.display, offset=render_scroll)
# self.player.boost(self.boost)
# print(int(self.player.pos[0])) # print(int(self.player.pos[0]))
# print(int(self.player.pos[1])) # print(int(self.player.pos[1]))
# print(self.tilemap.tiles_around(self.player.pos)) # print(self.tilemap.tiles_around(self.player.pos))
@ -140,18 +148,30 @@ class Game:
self.movement[0] = True self.movement[0] = True
if event.key == pygame.K_d: if event.key == pygame.K_d:
self.movement[1] = True self.movement[1] = True
if event.key == pygame.K_SPACE: # and not self.isJumping if event.key == pygame.K_SPACE or event.key == pygame.K_w: # and not self.isJumping
self.player.velocity[1] -= 3 self.player.jump()
self.isJumping = True
if event.key == pygame.K_m:
# self.boost = True
self.player.boost()
if event.key == pygame.K_x:
self.player.dash()
if event.type == pygame.KEYUP: if event.type == pygame.KEYUP:
if event.key == pygame.K_a: if event.key == pygame.K_a:
self.movement[0] = False self.movement[0] = False
if event.key == pygame.K_d: if event.key == pygame.K_d:
self.movement[1] = False self.movement[1] = False
if event.key == pygame.K_m:
self.boost = False
self.screen.blit( self.screen.blit(
pygame.transform.scale(self.display, self.screen.get_size()), (0, 0) pygame.transform.scale(self.display, self.screen.get_size()), (0, 0)
) )
pygame.display.update() pygame.display.update()
self.clock.tick(60) self.clock.tick(60)
@ -159,7 +179,7 @@ class Game:
sys.exit() sys.exit()
game = Game() game = Game()
game.run() game.run()

View file

@ -1,6 +1,9 @@
import pygame import pygame
import math
import random
from dataclasses import dataclass from dataclasses import dataclass
from scripts.animation import Animation from scripts.animation import Animation
from scripts.particle import Particle
from scripts.tilemap import Tilemap from scripts.tilemap import Tilemap
@ -13,7 +16,9 @@ class Collisions:
class PhysicsEntity: class PhysicsEntity:
def __init__(self, game, e_type: str, pos: tuple[float, float], size: tuple[int, int]): def __init__(
self, game, e_type: str, pos: tuple[float, float], size: tuple[int, int]
):
self.game = game self.game = game
self.e_type: str = e_type self.e_type: str = e_type
self.pos: list[float] = list(pos) self.pos: list[float] = list(pos)
@ -28,11 +33,12 @@ class PhysicsEntity:
self.animation_offset: tuple[int, int] = (-3, -3) self.animation_offset: tuple[int, int] = (-3, -3)
self.animation: Animation self.animation: Animation
self.set_action("idle") self.set_action("idle")
self.last_movement = [0, 0]
def update(self, tilemap: Tilemap, movement: tuple[float, float] = (0, 0)) -> None: def update(self, tilemap: Tilemap, movement: tuple[float, float] = (0, 0)) -> None:
self.collisions = Collisions() self.collisions = Collisions()
frame_movement: tuple[float, float] = ( frame_movement: tuple[float, float] = (
(movement[0] + self.velocity[0]) * self.speed, (movement[0] + self.velocity[0]),
movement[1] + self.velocity[1], movement[1] + self.velocity[1],
) )
self.pos[0] += frame_movement[0] self.pos[0] += frame_movement[0]
@ -72,6 +78,7 @@ class PhysicsEntity:
self.flip = False self.flip = False
if movement[0] < 0: if movement[0] < 0:
self.flip = True self.flip = True
self.last_movement = movement
if self.collisions.bottom or self.collisions.top: if self.collisions.bottom or self.collisions.top:
self.velocity[1] = 0 self.velocity[1] = 0
@ -102,7 +109,10 @@ class PhysicsEntity:
class Player(PhysicsEntity): class Player(PhysicsEntity):
def __init__(self, game, pos: tuple[float, float], size: tuple[int, int]): def __init__(self, game, pos: tuple[float, float], size: tuple[int, int]):
super().__init__(game, "player", pos, size) super().__init__(game, "player", pos, size)
self.air_time: int = 0 # Neu! Zählt, wie lange wir fallen/springe self.air_time: int = 0 # Neu! Zählt, wie lange wir fallen/springen
self.jumps: int = 0
self.wall_slide: bool = False
self.dashing: int = 0
def update(self, tilemap: Tilemap, movement: tuple[float, float] = (0, 0)) -> None: def update(self, tilemap: Tilemap, movement: tuple[float, float] = (0, 0)) -> None:
super().update(tilemap, movement) super().update(tilemap, movement)
@ -111,7 +121,22 @@ class Player(PhysicsEntity):
if self.collisions.bottom: if self.collisions.bottom:
self.air_time = 0 self.air_time = 0
self.jumps = 1
# Wenn wir länger als 4 Frames in der Luft sind -> Springen! # Wenn wir länger als 4 Frames in der Luft sind -> Springen!
self.wall_slide = False
if self.collisions.left or self.collisions.right and self.air_time > 4:
self.wall_slide = True
self.velocity[1] = min(self.velocity[1], 0.5)
if self.collisions.left:
self.flip = True
else:
self.flip = False
self.set_action("wall_slide")
if not self.wall_slide:
if self.air_time > 4: if self.air_time > 4:
self.set_action("jump") self.set_action("jump")
@ -122,3 +147,91 @@ class Player(PhysicsEntity):
# Sonst -> Rumstehen! # Sonst -> Rumstehen!
else: else:
self.set_action("idle") self.set_action("idle")
if abs(self.dashing) in {60, 50}:
for _ in range(20):
particle_angle = random.random() * math.pi * 2
particle_speed = random.random() * 0.5 + 0.5
particle_velocity = [
math.cos(particle_angle) * particle_speed,
math.sin(particle_angle) * particle_speed,
]
particle_pos = self.rect().center
particle_startframe = random.randint(0, 7)
self.game.particles.append(
Particle(
self.game,
"particle",
particle_pos,
particle_velocity,
particle_startframe,
)
)
if self.dashing > 0:
self.dashing = max(0, self.dashing - 1) # 60 → 59 → ... → 0
if self.dashing < 0:
self.dashing = min(0, self.dashing + 1) # -60 → -59 → ... → 0
if abs(self.dashing) > 50:
self.velocity[0] = abs(self.dashing) / self.dashing * 8
if abs(self.dashing) == 51:
self.velocity[0] *= 0.1
particle_velocity = [
abs(self.dashing) / self.dashing * random.random() * 3,
0,
]
self.game.particles.append(Particle(self.game, "particle", self.rect().center, particle_velocity, random.randint(0, 7)))
if self.velocity[0] > 0:
self.velocity[0] = max(self.velocity[0] - 0.1, 0)
else:
self.velocity[0] = min(self.velocity[0] + 0.1, 0)
def jump(self) -> None | bool:
if self.wall_slide:
if self.flip and self.last_movement[0] < 0:
self.velocity[0] = 3.5
self.velocity[1] = -2.5
self.air_time = 5
self.jumps = max(0, self.jumps - 1)
elif not self.flip and self.last_movement[0] > 0:
self.velocity[0] = -3.5
self.velocity[1] = -2.5
self.air_time = 5
self.jumps = max(0, self.jumps - 1)
elif self.jumps > 0:
self.velocity[1] = -3
self.jumps -= 1
self.air_time = 5
return True
def boost(self, boosting: bool = False):
# if boosting:
# self.velocity[1] -= 5
# boost_particle = Particle(self.game, "boost", (self.pos[0] + self.size[0]//2, self.pos[1] + self.size[1]//2))
# self.game.particles.append(boost_particle)
self.velocity[1] -= 100000
boost_particle = Particle(
self.game,
"boost",
(self.pos[0] + self.size[0] // 2, self.pos[1] + self.size[1] // 2),
)
self.game.particles.append(boost_particle)
def dash(self) -> None:
if not self.dashing:
if self.flip:
self.dashing = -60
else:
self.dashing = 60
def render(self, surface, offset=(0, 0)):
if abs(self.dashing) <= 50:
super().render(surface, offset)