Funken-Logik hinzugefügt und Projektilverhalten aktualisiert
This commit is contained in:
parent
ea2bc80c27
commit
c6d4cef993
4 changed files with 113 additions and 9 deletions
10
editor.py
10
editor.py
|
|
@ -32,10 +32,7 @@ class Editor:
|
||||||
|
|
||||||
self.tilemap: Tilemap = Tilemap(game=self, tile_size=16)
|
self.tilemap: Tilemap = Tilemap(game=self, tile_size=16)
|
||||||
|
|
||||||
try:
|
self.load_level("map")
|
||||||
self.tilemap.load(path="map.json")
|
|
||||||
except FileNotFoundError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
self.scroll: list[float] = [0, 0]
|
self.scroll: list[float] = [0, 0]
|
||||||
|
|
||||||
|
|
@ -49,6 +46,11 @@ class Editor:
|
||||||
self.ongrid: bool = True
|
self.ongrid: bool = True
|
||||||
self.running = True
|
self.running = True
|
||||||
|
|
||||||
|
def load_level(self, map_id: str|int):
|
||||||
|
try:
|
||||||
|
self.tilemap.load(path="data/maps/" + str(map_id) + ".json")
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
def run(self):
|
def run(self):
|
||||||
while self.running:
|
while self.running:
|
||||||
# Hintergrund löschen
|
# Hintergrund löschen
|
||||||
|
|
|
||||||
47
game.py
47
game.py
|
|
@ -9,6 +9,8 @@ from scripts.tilemap import Tilemap
|
||||||
from scripts.clouds import Clouds
|
from scripts.clouds import Clouds
|
||||||
from scripts.animation import Animation
|
from scripts.animation import Animation
|
||||||
from scripts.particle import Particle
|
from scripts.particle import Particle
|
||||||
|
from scripts.spark import Spark
|
||||||
|
|
||||||
|
|
||||||
SCREEN_WIDTH = 640
|
SCREEN_WIDTH = 640
|
||||||
SCREEN_HEIGHT = 480
|
SCREEN_HEIGHT = 480
|
||||||
|
|
@ -74,7 +76,7 @@ class Game:
|
||||||
|
|
||||||
self.tilemap: Tilemap = Tilemap(game=self, tile_size=16)
|
self.tilemap: Tilemap = Tilemap(game=self, tile_size=16)
|
||||||
|
|
||||||
self.load_level(0)
|
self.load_level("map")
|
||||||
|
|
||||||
self.leaf_spawners: list[pygame.Rect] = []
|
self.leaf_spawners: list[pygame.Rect] = []
|
||||||
self.enemys: list[Enemy] = []
|
self.enemys: list[Enemy] = []
|
||||||
|
|
@ -92,13 +94,14 @@ class Game:
|
||||||
|
|
||||||
self.particles: list[Particle] = []
|
self.particles: list[Particle] = []
|
||||||
self.projectiles: list = []
|
self.projectiles: list = []
|
||||||
|
self.sparks: list[Spark] = []
|
||||||
|
|
||||||
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
|
# self.boost: bool = False
|
||||||
|
|
||||||
def load_level(self, map_id: int):
|
def load_level(self, map_id: str|int):
|
||||||
try:
|
try:
|
||||||
self.tilemap.load(path="data/maps/" + str(map_id) + ".json")
|
self.tilemap.load(path="data/maps/" + str(map_id) + ".json")
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
|
|
@ -147,7 +150,6 @@ class Game:
|
||||||
)
|
)
|
||||||
self.player.render(surface=self.display, offset=render_scroll)
|
self.player.render(surface=self.display, offset=render_scroll)
|
||||||
|
|
||||||
|
|
||||||
# [[x, y], direction, timer]
|
# [[x, y], direction, timer]
|
||||||
for projectile in self.projectiles.copy():
|
for projectile in self.projectiles.copy():
|
||||||
projectile[0][0] += projectile[1]
|
projectile[0][0] += projectile[1]
|
||||||
|
|
@ -162,12 +164,45 @@ class Game:
|
||||||
)
|
)
|
||||||
if self.tilemap.solid_check(projectile[0]):
|
if self.tilemap.solid_check(projectile[0]):
|
||||||
self.projectiles.remove(projectile)
|
self.projectiles.remove(projectile)
|
||||||
|
for _ in range(4):
|
||||||
|
self.sparks.append(
|
||||||
|
Spark(
|
||||||
|
projectile[0],
|
||||||
|
random.random()
|
||||||
|
- 0.5
|
||||||
|
+ (math.pi if projectile[1] > 0 else 0),
|
||||||
|
2 + random.random(),
|
||||||
|
)
|
||||||
|
)
|
||||||
elif projectile[2] > 360:
|
elif projectile[2] > 360:
|
||||||
self.projectiles.remove(projectile)
|
self.projectiles.remove(projectile)
|
||||||
elif abs(self.player.dashing) < 50:
|
elif abs(self.player.dashing) < 50:
|
||||||
if self.player.rect().collidepoint(projectile[0]):
|
if self.player.rect().collidepoint(projectile[0]):
|
||||||
self.projectiles.remove(projectile)
|
self.projectiles.remove(projectile)
|
||||||
|
for i in range(30):
|
||||||
|
angle = random.random() * math.pi * 2
|
||||||
|
speed = random.random() * 5
|
||||||
|
self.sparks.append(
|
||||||
|
Spark(self.player.rect().center, angle, 2 + random.random())
|
||||||
|
)
|
||||||
|
self.particles.append(
|
||||||
|
Particle(
|
||||||
|
self,
|
||||||
|
"particle",
|
||||||
|
self.player.rect().center,
|
||||||
|
velocity=[
|
||||||
|
math.cos(angle + math.pi) * speed * 0.5,
|
||||||
|
math.sin(angle + math.pi) * speed * 0.5,
|
||||||
|
],
|
||||||
|
frame=random.randint(0, 7),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
for spark in self.sparks.copy():
|
||||||
|
kill = spark.update()
|
||||||
|
spark.render(self.display, offset=render_scroll)
|
||||||
|
if kill:
|
||||||
|
self.sparks.remove(spark)
|
||||||
|
|
||||||
# self.player.boost(self.boost)
|
# self.player.boost(self.boost)
|
||||||
# print(int(self.player.pos[0]))
|
# print(int(self.player.pos[0]))
|
||||||
|
|
@ -200,7 +235,11 @@ class Game:
|
||||||
# self.boost = True
|
# self.boost = True
|
||||||
self.player.boost()
|
self.player.boost()
|
||||||
|
|
||||||
if event.key == pygame.K_x or event.key == pygame.K_LSHIFT or event.key == pygame.K_RSHIFT:
|
if (
|
||||||
|
event.key == pygame.K_x
|
||||||
|
or event.key == pygame.K_LSHIFT
|
||||||
|
or event.key == pygame.K_RSHIFT
|
||||||
|
):
|
||||||
self.player.dash()
|
self.player.dash()
|
||||||
|
|
||||||
if event.type == pygame.KEYUP:
|
if event.type == pygame.KEYUP:
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ 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.particle import Particle
|
||||||
|
from scripts.spark import Spark
|
||||||
from scripts.tilemap import Tilemap
|
from scripts.tilemap import Tilemap
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -136,10 +137,26 @@ class Enemy(PhysicsEntity):
|
||||||
self.game.projectiles.append(
|
self.game.projectiles.append(
|
||||||
[[self.rect().centerx - 7, self.rect().centery], -1.5, 0]
|
[[self.rect().centerx - 7, self.rect().centery], -1.5, 0]
|
||||||
)
|
)
|
||||||
|
for _ in range(4):
|
||||||
|
self.game.sparks.append(
|
||||||
|
Spark(
|
||||||
|
self.game.projectiles[-1][0],
|
||||||
|
random.random() - 0.5 + math.pi,
|
||||||
|
random.random(),
|
||||||
|
)
|
||||||
|
)
|
||||||
if not self.flip and distance[0] > 0:
|
if not self.flip and distance[0] > 0:
|
||||||
self.game.projectiles.append(
|
self.game.projectiles.append(
|
||||||
[[self.rect().centerx + 7, self.rect().centery], 1.5, 0]
|
[[self.rect().centerx + 7, self.rect().centery], 1.5, 0]
|
||||||
)
|
)
|
||||||
|
for _ in range(4):
|
||||||
|
self.game.sparks.append(
|
||||||
|
Spark(
|
||||||
|
self.game.projectiles[-1][0],
|
||||||
|
random.random() - 0.5,
|
||||||
|
random.random(),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
elif random.random() < 0.02:
|
elif random.random() < 0.02:
|
||||||
self.walking = random.randint(30, 120)
|
self.walking = random.randint(30, 120)
|
||||||
|
|
|
||||||
46
scripts/spark.py
Normal file
46
scripts/spark.py
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
import math
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
|
||||||
|
|
||||||
|
class Spark:
|
||||||
|
"""Ein Funken, der von einer Klinge oder einem Projektil ausgeht."""
|
||||||
|
|
||||||
|
def __init__(self, pos, angle, speed):
|
||||||
|
self.pos = list(pos)
|
||||||
|
self.angle = angle
|
||||||
|
self.speed = speed
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
# Bewegt den Funken in seine Richtung
|
||||||
|
self.pos[0] += math.cos(self.angle) * self.speed
|
||||||
|
self.pos[1] += math.sin(self.angle) * self.speed
|
||||||
|
|
||||||
|
# Macht den Funken langsamer, bis er stehen bleibt
|
||||||
|
self.speed = max(0, self.speed - 0.1)
|
||||||
|
|
||||||
|
# Gibt True zurück, wenn der Funken stillsteht (kann dann gelöscht werden)
|
||||||
|
return not self.speed
|
||||||
|
|
||||||
|
def render(self, surface: pygame.Surface, offset=(0, 0)):
|
||||||
|
"""Zeichnet den Funken als kleines, in Flugrichtung gestrecktes Viereck."""
|
||||||
|
render_points = [
|
||||||
|
(
|
||||||
|
self.pos[0] + math.cos(self.angle) * self.speed * 3 - offset[0],
|
||||||
|
self.pos[1] + math.sin(self.angle) * self.speed * 3 - offset[1],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
self.pos[0] + math.cos(self.angle + math.pi * 0.5) * self.speed * 0.5 - offset[0],
|
||||||
|
self.pos[1] + math.sin(self.angle + math.pi * 0.5) * self.speed * 0.5 - offset[1],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
self.pos[0] + math.cos(self.angle + math.pi) * self.speed * 3 - offset[0],
|
||||||
|
self.pos[1] + math.sin(self.angle + math.pi) * self.speed * 3 - offset[1],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
self.pos[0] + math.cos(self.angle - math.pi * 0.5) * self.speed * 0.5 - offset[0],
|
||||||
|
self.pos[1] + math.sin(self.angle - math.pi * 0.5) * self.speed * 0.5 - offset[1],
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
pygame.draw.polygon(surface, (255, 255, 180), render_points) # (255, 255, 180)
|
||||||
Loading…
Reference in a new issue