165 lines
5.9 KiB
Python
165 lines
5.9 KiB
Python
import pygame # ty: ignore[unresolved-import]
|
|
import sys
|
|
import random
|
|
import math
|
|
from scripts.entities import Player
|
|
from scripts.utils import load_image
|
|
from scripts.utils import load_images
|
|
from scripts.tilemap import Tilemap
|
|
from scripts.clouds import Clouds
|
|
from scripts.animation import Animation
|
|
from scripts.particle import Particle
|
|
|
|
SCREEN_WIDTH = 640
|
|
SCREEN_HEIGHT = 480
|
|
|
|
|
|
class Game:
|
|
def __init__(self):
|
|
pygame.init()
|
|
|
|
pygame.display.set_caption("Jump and run spiel")
|
|
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)
|
|
)
|
|
|
|
self.clock: pygame.time.Clock = pygame.time.Clock()
|
|
self.running: bool = True
|
|
|
|
self.collision_area: pygame.Rect = pygame.Rect(50, 50, 300, 50)
|
|
self.movement: list[bool] = [False, False]
|
|
|
|
self.assets: dict = {
|
|
# Andere Dinge
|
|
"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"),
|
|
# Spieler
|
|
"player/idle": Animation(
|
|
load_images(path="entities/player/idle"), image_duration=6
|
|
),
|
|
"player/run": Animation(
|
|
load_images(path="entities/player/run"), image_duration=4
|
|
),
|
|
"player/jump": Animation(load_images(path="entities/player/jump")),
|
|
"player/slide": Animation(load_images(path="entities/player/slide")),
|
|
"player/wall_slide": Animation(
|
|
load_images(path="entities/player/wall_slide")
|
|
),
|
|
"particles/leaf": Animation(
|
|
load_images(path="particles/leaf"), image_duration=20, loop=False
|
|
),
|
|
}
|
|
self.player: Player = Player(game=self, pos=(50, 50), size=(8, 15))
|
|
|
|
self.tilemap: Tilemap = Tilemap(game=self, tile_size=16)
|
|
|
|
try:
|
|
self.tilemap.load(path="map.json")
|
|
except FileNotFoundError:
|
|
pass
|
|
|
|
self.leaf_spawners: list[pygame.Rect] = []
|
|
|
|
for tree in self.tilemap.extract([("large_decor", 2)], keep=True):
|
|
self.leaf_spawners.append(
|
|
pygame.Rect(4 + tree["pos"][0], 4 + tree["pos"][1], 23, 13)
|
|
)
|
|
|
|
self.particles: list[Particle] = []
|
|
|
|
self.clouds: Clouds = Clouds(cloud_images=self.assets["clouds"], count=16)
|
|
self.isJumping: bool = False
|
|
self.scroll: list[float] = [0, 0]
|
|
|
|
def run(self) -> None:
|
|
while self.running:
|
|
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
|
|
render_scroll: tuple[int, int] = (int(self.scroll[0]), int(self.scroll[1]))
|
|
|
|
for rect in self.leaf_spawners:
|
|
if random.random() * 49999 < rect.width * rect.height:
|
|
pos = (
|
|
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),
|
|
)
|
|
)
|
|
|
|
self.clouds.update()
|
|
self.clouds.render(surface=self.display, offset=render_scroll)
|
|
self.tilemap.render(surface=self.display, offset=render_scroll)
|
|
self.player.update(
|
|
self.tilemap, movement=((self.movement[1] - self.movement[0]), 0)
|
|
)
|
|
self.player.render(surface=self.display, offset=render_scroll)
|
|
# print(int(self.player.pos[0]))
|
|
# print(int(self.player.pos[1]))
|
|
# print(self.tilemap.tiles_around(self.player.pos))
|
|
for particle in self.particles.copy():
|
|
kill: bool = particle.update()
|
|
particle.render(self.display, offset=render_scroll)
|
|
if particle.type == 'leaf':
|
|
particle.pos[0] += math.sin(particle.animation.frame * 0.035) * 0.3
|
|
if kill:
|
|
self.particles.remove(particle)
|
|
|
|
|
|
|
|
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
|
|
if event.key == pygame.K_SPACE: # and not self.isJumping
|
|
self.player.velocity[1] -= 3
|
|
self.isJumping = True
|
|
if event.type == pygame.KEYUP:
|
|
if event.key == pygame.K_a:
|
|
self.movement[0] = False
|
|
if event.key == pygame.K_d:
|
|
self.movement[1] = False
|
|
|
|
self.screen.blit(
|
|
pygame.transform.scale(self.display, self.screen.get_size()), (0, 0)
|
|
)
|
|
pygame.display.update()
|
|
self.clock.tick(60)
|
|
|
|
pygame.quit()
|
|
sys.exit()
|
|
|
|
|
|
game = Game()
|
|
|
|
|
|
game.run()
|