Stand Ende 17.04.2026
This commit is contained in:
parent
4e8227aead
commit
4d8ef1f040
2 changed files with 57 additions and 28 deletions
62
game.py
62
game.py
|
|
@ -1,68 +1,75 @@
|
|||
import pygame
|
||||
import sys
|
||||
from scripts.entities import PhysicsEntity, Player
|
||||
from scripts.utils import *
|
||||
from scripts.tilemap import *
|
||||
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
|
||||
|
||||
|
||||
class Game:
|
||||
def __init__(self):
|
||||
pygame.init()
|
||||
|
||||
|
||||
pygame.display.set_caption("Jump and run spiel")
|
||||
self.screen = pygame.display.set_mode((640, 480))
|
||||
self.display = pygame.Surface((320, 240))
|
||||
|
||||
|
||||
|
||||
self.clock = pygame.time.Clock()
|
||||
self.running = True
|
||||
|
||||
self.collision_area = pygame.Rect(50, 50, 300, 50)
|
||||
self.movement = [False, False]
|
||||
|
||||
|
||||
self.assets = {
|
||||
# Andere Dinge
|
||||
"background":load_image("background.png"),
|
||||
"background": load_image("background.png"),
|
||||
"decor": load_images("tiles/decor"),
|
||||
"grass": load_images("tiles/grass"),
|
||||
"large_decor": load_images("tiles/large_decor"),
|
||||
"stone": load_images("tiles/stone"),
|
||||
"clouds": load_images("clouds"),
|
||||
|
||||
# Spieler
|
||||
"player/idle":Animation(load_images("entities/player/idle"), image_duration=6),
|
||||
"player/run":Animation(load_images("entities/player/run"), image_duration=4),
|
||||
"player/jump":Animation(load_images("entities/player/jump")),
|
||||
"player/slide":Animation(load_images("entities/player/slide")),
|
||||
"player/wall_slide":Animation(load_images("entities/player/wall_slide")),
|
||||
"player/idle": Animation(
|
||||
load_images("entities/player/idle"), image_duration=6
|
||||
),
|
||||
"player/run": Animation(
|
||||
load_images("entities/player/run"), image_duration=4
|
||||
),
|
||||
"player/jump": Animation(load_images("entities/player/jump")),
|
||||
"player/slide": Animation(load_images("entities/player/slide")),
|
||||
"player/wall_slide": Animation(load_images("entities/player/wall_slide")),
|
||||
}
|
||||
self.player = Player(self, (50, 50), (8, 15))
|
||||
|
||||
self.tilemap = Tilemap(self, 16)
|
||||
self.tilemap.load("map.json")
|
||||
self.clouds = Clouds(self.assets['clouds'], count=16)
|
||||
self.clouds = Clouds(self.assets["clouds"], count=16)
|
||||
self.isJumping = False
|
||||
self.scroll = [0, 0]
|
||||
self.test :dict = {"dsa"}
|
||||
|
||||
self.test: dict = {"dsa"}
|
||||
|
||||
def testolino(self):
|
||||
print(self.test)
|
||||
print(type(self.test))
|
||||
|
||||
|
||||
def run(self):
|
||||
while self.running:
|
||||
self.display.blit(self.assets["background"], (0, 0))
|
||||
|
||||
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
|
||||
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 = (int(self.scroll[0]), int(self.scroll[1]))
|
||||
|
||||
|
||||
self.clouds.update()
|
||||
self.clouds.render(self.display, offset=render_scroll)
|
||||
self.tilemap.render(self.display, offset=render_scroll)
|
||||
|
|
@ -72,7 +79,6 @@ class Game:
|
|||
# print(int(self.player.pos[1]))
|
||||
# print(self.tilemap.tiles_around(self.player.pos))
|
||||
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
self.running = False
|
||||
|
|
@ -83,7 +89,7 @@ class Game:
|
|||
self.movement[0] = True
|
||||
if event.key == pygame.K_d:
|
||||
self.movement[1] = True
|
||||
if event.key == pygame.K_SPACE: # and not self.isJumping
|
||||
if event.key == pygame.K_SPACE: # and not self.isJumping
|
||||
self.player.velocity[1] -= 3
|
||||
self.isJumping = True
|
||||
if event.type == pygame.KEYUP:
|
||||
|
|
@ -92,12 +98,12 @@ class Game:
|
|||
if event.key == pygame.K_d:
|
||||
self.movement[1] = False
|
||||
|
||||
|
||||
self.screen.blit(pygame.transform.scale(self.display, self.screen.get_size()), (0,0))
|
||||
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()
|
||||
|
||||
|
|
|
|||
23
scripts/models.py
Normal file
23
scripts/models.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Tile:
|
||||
type: str
|
||||
variant: int
|
||||
pos: list[int]
|
||||
|
||||
def grid_key(self) -> str:
|
||||
"""Erzeugt den String-Key für die Tilemap."""
|
||||
return f"{self.pos[0]};{self.pos[1]}"
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""Konvertiert zurück in ein Dict (für JSON-Speicherung)."""
|
||||
|
||||
return {"type": self.type, "variant": self.variant, "pos": list(self.pos)}
|
||||
|
||||
@staticmethod
|
||||
def from_dict(data: dict) -> "Tile":
|
||||
"""Erstellt ein Tile aus einem Dictionary."""
|
||||
|
||||
return Tile(type=data["type"], variant=data["variant"], pos=data["pos"])
|
||||
Loading…
Reference in a new issue