2026-06-12 14:43:28 +00:00
|
|
|
import pygame # ty: ignore[unresolved-import]
|
2026-02-06 17:51:05 +00:00
|
|
|
import sys
|
2026-05-20 15:57:59 +00:00
|
|
|
import random
|
2026-05-20 16:28:03 +00:00
|
|
|
import math
|
2026-06-10 16:23:23 +00:00
|
|
|
from scripts.entities import Enemy, Player
|
2026-04-17 16:45:10 +00:00
|
|
|
from scripts.utils import load_image
|
|
|
|
|
from scripts.utils import load_images
|
|
|
|
|
from scripts.tilemap import Tilemap
|
2026-02-06 17:51:05 +00:00
|
|
|
from scripts.clouds import Clouds
|
2026-03-04 15:50:44 +00:00
|
|
|
from scripts.animation import Animation
|
2026-05-20 15:57:59 +00:00
|
|
|
from scripts.particle import Particle
|
2026-02-06 17:51:05 +00:00
|
|
|
|
2026-05-20 15:23:09 +00:00
|
|
|
SCREEN_WIDTH = 640
|
|
|
|
|
SCREEN_HEIGHT = 480
|
2026-04-17 16:45:10 +00:00
|
|
|
|
2026-05-20 15:57:59 +00:00
|
|
|
|
2026-02-06 17:51:05 +00:00
|
|
|
class Game:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
pygame.init()
|
2026-05-20 15:57:59 +00:00
|
|
|
|
2026-02-06 17:51:05 +00:00
|
|
|
pygame.display.set_caption("Jump and run spiel")
|
2026-05-20 15:57:59 +00:00
|
|
|
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)
|
|
|
|
|
)
|
2026-02-06 17:51:05 +00:00
|
|
|
|
2026-05-11 08:37:37 +00:00
|
|
|
self.clock: pygame.time.Clock = pygame.time.Clock()
|
|
|
|
|
self.running: bool = True
|
2026-02-06 17:51:05 +00:00
|
|
|
|
2026-05-11 08:37:37 +00:00
|
|
|
self.collision_area: pygame.Rect = pygame.Rect(50, 50, 300, 50)
|
|
|
|
|
self.movement: list[bool] = [False, False]
|
2026-04-17 16:45:10 +00:00
|
|
|
|
2026-05-11 08:37:37 +00:00
|
|
|
self.assets: dict = {
|
2026-03-04 15:50:44 +00:00
|
|
|
# Andere Dinge
|
2026-05-20 15:23:09 +00:00
|
|
|
"background": load_image(path="background.png"),
|
|
|
|
|
"decor": load_images(path="tiles/decor"),
|
|
|
|
|
"grass": load_images(path="tiles/grass"),
|
|
|
|
|
"large_decor": load_images(path="tiles/large_decor"),
|
|
|
|
|
"stone": load_images(path="tiles/stone"),
|
|
|
|
|
"clouds": load_images(path="clouds"),
|
2026-03-04 15:50:44 +00:00
|
|
|
# Spieler
|
2026-04-17 16:45:10 +00:00
|
|
|
"player/idle": Animation(
|
2026-05-20 15:23:09 +00:00
|
|
|
load_images(path="entities/player/idle"), image_duration=6
|
2026-04-17 16:45:10 +00:00
|
|
|
),
|
|
|
|
|
"player/run": Animation(
|
2026-05-20 15:23:09 +00:00
|
|
|
load_images(path="entities/player/run"), image_duration=4
|
2026-04-17 16:45:10 +00:00
|
|
|
),
|
2026-05-20 15:23:09 +00:00
|
|
|
"player/jump": Animation(load_images(path="entities/player/jump")),
|
|
|
|
|
"player/slide": Animation(load_images(path="entities/player/slide")),
|
2026-05-20 15:57:59 +00:00
|
|
|
"player/wall_slide": Animation(
|
|
|
|
|
load_images(path="entities/player/wall_slide")
|
|
|
|
|
),
|
2026-06-12 14:43:28 +00:00
|
|
|
"enemy/idle": Animation(
|
|
|
|
|
load_images(path="entities/enemy/idle"), image_duration=6
|
|
|
|
|
),
|
|
|
|
|
"enemy/run": Animation(
|
|
|
|
|
load_images(path="entities/enemy/run"), image_duration=4
|
|
|
|
|
),
|
2026-05-20 15:57:59 +00:00
|
|
|
"particles/leaf": Animation(
|
|
|
|
|
load_images(path="particles/leaf"), image_duration=20, loop=False
|
|
|
|
|
),
|
2026-06-10 16:00:11 +00:00
|
|
|
"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
|
2026-06-12 14:43:28 +00:00
|
|
|
),
|
2026-06-12 15:43:04 +00:00
|
|
|
"gun": load_image("gun.png"),
|
|
|
|
|
"projectile": load_image("projectile.png"),
|
2026-03-04 15:50:44 +00:00
|
|
|
}
|
2026-05-20 15:23:09 +00:00
|
|
|
self.player: Player = Player(game=self, pos=(50, 50), size=(8, 15))
|
|
|
|
|
|
|
|
|
|
self.tilemap: Tilemap = Tilemap(game=self, tile_size=16)
|
2026-05-11 16:06:31 +00:00
|
|
|
|
2026-06-12 15:48:59 +00:00
|
|
|
self.load_level(0)
|
2026-05-11 16:06:31 +00:00
|
|
|
|
2026-05-20 15:25:29 +00:00
|
|
|
self.leaf_spawners: list[pygame.Rect] = []
|
2026-06-10 16:23:23 +00:00
|
|
|
self.enemys: list[Enemy] = []
|
2026-05-20 15:25:29 +00:00
|
|
|
|
2026-05-20 15:23:09 +00:00
|
|
|
for tree in self.tilemap.extract([("large_decor", 2)], keep=True):
|
2026-05-20 15:57:59 +00:00
|
|
|
self.leaf_spawners.append(
|
2026-05-20 16:28:03 +00:00
|
|
|
pygame.Rect(4 + tree["pos"][0], 4 + tree["pos"][1], 23, 13)
|
2026-05-20 15:57:59 +00:00
|
|
|
)
|
|
|
|
|
|
2026-06-10 16:12:24 +00:00
|
|
|
for spawner in self.tilemap.extract([("spawners", 0), ("spawners", 1)]):
|
|
|
|
|
if spawner["variant"] == 0:
|
|
|
|
|
self.player.pos = spawner["pos"]
|
|
|
|
|
else:
|
2026-06-10 16:23:23 +00:00
|
|
|
self.enemys.append(Enemy(self, spawner["pos"], (8, 15)))
|
2026-06-10 16:12:24 +00:00
|
|
|
|
2026-05-20 16:28:03 +00:00
|
|
|
self.particles: list[Particle] = []
|
2026-06-12 15:43:04 +00:00
|
|
|
self.projectiles: list = []
|
2026-05-20 15:23:09 +00:00
|
|
|
|
|
|
|
|
self.clouds: Clouds = Clouds(cloud_images=self.assets["clouds"], count=16)
|
2026-05-11 08:37:37 +00:00
|
|
|
self.isJumping: bool = False
|
|
|
|
|
self.scroll: list[float] = [0, 0]
|
2026-06-10 16:00:11 +00:00
|
|
|
# self.boost: bool = False
|
2026-05-20 15:23:09 +00:00
|
|
|
|
2026-06-12 15:48:59 +00:00
|
|
|
def load_level(self, map_id: int):
|
|
|
|
|
try:
|
|
|
|
|
self.tilemap.load(path="data/maps/" + str(map_id) + ".json")
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
pass
|
|
|
|
|
|
2026-05-11 08:37:37 +00:00
|
|
|
def run(self) -> None:
|
2026-02-06 17:51:05 +00:00
|
|
|
while self.running:
|
2026-04-17 16:45:10 +00:00
|
|
|
self.display.blit(self.assets["background"], (0, 0))
|
|
|
|
|
|
|
|
|
|
self.scroll[0] += (
|
|
|
|
|
self.player.rect().centerx
|
|
|
|
|
- self.display.get_width() / 2
|
|
|
|
|
- self.scroll[0]
|
|
|
|
|
) / 30
|
|
|
|
|
self.scroll[1] += (
|
|
|
|
|
self.player.rect().centery
|
|
|
|
|
- self.display.get_height() / 2
|
|
|
|
|
- self.scroll[1]
|
|
|
|
|
) / 30
|
2026-05-20 15:23:09 +00:00
|
|
|
render_scroll: tuple[int, int] = (int(self.scroll[0]), int(self.scroll[1]))
|
2026-02-06 17:51:05 +00:00
|
|
|
|
2026-05-20 15:57:59 +00:00
|
|
|
for rect in self.leaf_spawners:
|
2026-05-20 16:28:03 +00:00
|
|
|
if random.random() * 49999 < rect.width * rect.height:
|
2026-06-12 14:43:28 +00:00
|
|
|
pos = (
|
2026-05-20 16:28:03 +00:00
|
|
|
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),
|
|
|
|
|
)
|
|
|
|
|
)
|
2026-05-20 15:57:59 +00:00
|
|
|
|
2026-02-06 17:51:05 +00:00
|
|
|
self.clouds.update()
|
2026-05-20 15:23:09 +00:00
|
|
|
self.clouds.render(surface=self.display, offset=render_scroll)
|
|
|
|
|
self.tilemap.render(surface=self.display, offset=render_scroll)
|
2026-06-10 16:23:23 +00:00
|
|
|
for enemy in self.enemys.copy():
|
|
|
|
|
enemy.update(self.tilemap, (0, 0))
|
|
|
|
|
enemy.render(self.display, offset=render_scroll)
|
2026-05-20 15:57:59 +00:00
|
|
|
self.player.update(
|
|
|
|
|
self.tilemap, movement=((self.movement[1] - self.movement[0]), 0)
|
|
|
|
|
)
|
2026-05-20 15:23:09 +00:00
|
|
|
self.player.render(surface=self.display, offset=render_scroll)
|
2026-06-12 15:43:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# [[x, y], direction, timer]
|
|
|
|
|
for projectile in self.projectiles.copy():
|
|
|
|
|
projectile[0][0] += projectile[1]
|
|
|
|
|
projectile[2] += 1
|
|
|
|
|
img = self.assets["projectile"]
|
|
|
|
|
self.display.blit(
|
|
|
|
|
img,
|
|
|
|
|
(
|
|
|
|
|
projectile[0][0] - img.get_width() / 2 - render_scroll[0],
|
|
|
|
|
projectile[0][1] - img.get_height() / 2 - render_scroll[1],
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
if self.tilemap.solid_check(projectile[0]):
|
|
|
|
|
self.projectiles.remove(projectile)
|
|
|
|
|
elif projectile[2] > 360:
|
|
|
|
|
self.projectiles.remove(projectile)
|
|
|
|
|
elif abs(self.player.dashing) < 50:
|
|
|
|
|
if self.player.rect().collidepoint(projectile[0]):
|
|
|
|
|
self.projectiles.remove(projectile)
|
|
|
|
|
|
|
|
|
|
|
2026-06-10 16:00:11 +00:00
|
|
|
# self.player.boost(self.boost)
|
2026-02-06 17:51:05 +00:00
|
|
|
# print(int(self.player.pos[0]))
|
|
|
|
|
# print(int(self.player.pos[1]))
|
|
|
|
|
# print(self.tilemap.tiles_around(self.player.pos))
|
2026-05-20 16:28:03 +00:00
|
|
|
for particle in self.particles.copy():
|
|
|
|
|
kill: bool = particle.update()
|
|
|
|
|
particle.render(self.display, offset=render_scroll)
|
2026-06-12 14:43:28 +00:00
|
|
|
if particle.type == "leaf":
|
2026-05-20 16:28:03 +00:00
|
|
|
particle.pos[0] += math.sin(particle.animation.frame * 0.035) * 0.3
|
|
|
|
|
if kill:
|
|
|
|
|
self.particles.remove(particle)
|
|
|
|
|
|
2026-02-06 17:51:05 +00:00
|
|
|
for event in pygame.event.get():
|
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
|
self.running = False
|
|
|
|
|
if event.type == pygame.KEYDOWN:
|
|
|
|
|
if event.key == pygame.K_ESCAPE:
|
|
|
|
|
self.running = False
|
|
|
|
|
if event.key == pygame.K_a:
|
|
|
|
|
self.movement[0] = True
|
|
|
|
|
if event.key == pygame.K_d:
|
|
|
|
|
self.movement[1] = True
|
2026-06-12 14:43:28 +00:00
|
|
|
if (
|
|
|
|
|
event.key == pygame.K_SPACE or event.key == pygame.K_w
|
|
|
|
|
): # and not self.isJumping
|
2026-06-10 16:00:11 +00:00
|
|
|
self.player.jump()
|
2026-06-12 14:43:28 +00:00
|
|
|
|
2026-06-10 16:00:11 +00:00
|
|
|
if event.key == pygame.K_m:
|
|
|
|
|
# self.boost = True
|
|
|
|
|
self.player.boost()
|
2026-06-12 14:43:28 +00:00
|
|
|
|
2026-06-12 15:43:04 +00:00
|
|
|
if event.key == pygame.K_x or event.key == pygame.K_LSHIFT or event.key == pygame.K_RSHIFT:
|
2026-06-10 16:00:11 +00:00
|
|
|
self.player.dash()
|
|
|
|
|
|
2026-02-06 17:51:05 +00:00
|
|
|
if event.type == pygame.KEYUP:
|
|
|
|
|
if event.key == pygame.K_a:
|
|
|
|
|
self.movement[0] = False
|
2026-06-10 16:00:11 +00:00
|
|
|
|
2026-02-06 17:51:05 +00:00
|
|
|
if event.key == pygame.K_d:
|
|
|
|
|
self.movement[1] = False
|
|
|
|
|
|
2026-06-10 16:00:11 +00:00
|
|
|
if event.key == pygame.K_m:
|
|
|
|
|
self.boost = False
|
|
|
|
|
|
2026-04-17 16:45:10 +00:00
|
|
|
self.screen.blit(
|
|
|
|
|
pygame.transform.scale(self.display, self.screen.get_size()), (0, 0)
|
|
|
|
|
)
|
2026-06-10 16:00:11 +00:00
|
|
|
|
2026-02-06 17:51:05 +00:00
|
|
|
pygame.display.update()
|
|
|
|
|
self.clock.tick(60)
|
|
|
|
|
|
|
|
|
|
pygame.quit()
|
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
|
2026-06-10 16:00:11 +00:00
|
|
|
game = Game()
|
2026-06-12 14:43:28 +00:00
|
|
|
game.run()
|