school-horror-game/game.py

252 lines
7.6 KiB
Python
Raw Normal View History

2026-08-26 16:35:06 +00:00
import pygame
2026-10-02 15:22:08 +00:00
from pygame.math import Vector2
from pygame.rect import Rect
from pygame.typing import SequenceLike
2026-08-26 16:35:06 +00:00
from scripts.utils import load_image
TILE_SIZE = 64
COLS = 20
ROWS = 12
SCREEN_WIDTH = TILE_SIZE * COLS
SCREEN_HEIGHT = TILE_SIZE * ROWS
2026-10-02 15:22:08 +00:00
SCREEN_CENTER = (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
PLAYER_SIZE = 40
LIGHT_RADIUS = 150
class Item:
def __init__(self, x: float, y: float, width: float, height: float, color):
self.x: float = x
self.y: float = y
self.vector = pygame.Vector2(self.x, self.y)
self.width: float = width
self.height: float = height
self.color: SequenceLike[int] | str | int = color
@property
def rect(self) -> pygame.Rect:
2026-10-02 15:22:08 +00:00
return pygame.Rect(self.x, self.y, self.width, self.height)
def render(self, surface):
2026-10-02 15:22:08 +00:00
pygame.draw.rect(surface, self.color, self.rect)
2026-07-17 15:45:18 +00:00
2026-07-17 15:45:18 +00:00
class Game:
def __init__(self):
2026-10-02 15:22:08 +00:00
# Pygame setup
2026-07-17 15:45:18 +00:00
pygame.init()
pygame.display.set_caption("School Game")
self.clock = pygame.Clock()
2026-10-02 15:22:08 +00:00
# Surfaces
self.screen: pygame.Surface = pygame.display.set_mode(
(SCREEN_WIDTH, SCREEN_HEIGHT)
)
2026-10-02 15:22:08 +00:00
# Pygame-Variablen
2026-07-17 15:45:18 +00:00
self.running: bool = True
2026-07-24 15:50:34 +00:00
self.dt: int = 0
2026-10-02 15:22:08 +00:00
# Spieler
self.player_pos = pygame.Vector2(
self.screen.get_width() / 2, self.screen.get_height() / 2
)
2026-10-02 15:22:08 +00:00
self.player = Player(self.player_pos, width=PLAYER_SIZE, height=PLAYER_SIZE)
# Flashlight
self.flashlight = Taschenlampe(self)
2026-10-02 15:22:08 +00:00
# Items
self.test_item = Item(140, 300, 20, 20, "blue")
2026-10-02 15:22:08 +00:00
self.test_item_mask = pygame.mask.Mask(self.test_item.rect.size, fill=True)
# Level
self.walls: list[Rect] = [
pygame.Rect(100 * 2, 100 * 2, 20 * 2, 50 * 2),
pygame.Rect(300 * 2, 300 * 2, 200 * 2, 20 * 2),
pygame.Rect(550 * 2, 80 * 2, 20 * 2, 200 * 2),
]
2026-10-02 15:22:08 +00:00
# Assets
self.assets = {"floor_01": load_image("level/ground/floor_1.png")}
2026-08-28 16:46:44 +00:00
2026-09-18 15:29:30 +00:00
def update(self) -> None:
2026-10-02 15:22:08 +00:00
# Ereignisse
2026-09-18 15:29:30 +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_LSHIFT:
self.player.speed = 300
if event.key == pygame.K_q:
self.flashlight.light_state = not self.flashlight.light_state
if self.flashlight.render_item:
self.flashlight.render_item = False
2026-10-02 15:22:08 +00:00
if event.type == pygame.KEYUP and event.key == pygame.K_LSHIFT:
self.player.speed = 150
2026-10-02 15:22:08 +00:00
# Spielzustände aktualisieren
2026-09-18 15:29:30 +00:00
self.player.update(walls=self.walls, dt=self.dt)
self.flashlight.update()
2026-08-26 16:35:06 +00:00
2026-09-18 15:29:30 +00:00
def render(self) -> None:
2026-10-02 15:22:08 +00:00
# Hintergrund
self.screen.fill((0, 0, 0, 0))
for y in range(ROWS):
for x in range(COLS):
self.screen.blit(
self.assets["floor_01"],
pygame.Rect(x * 64, y * 64, TILE_SIZE, TILE_SIZE),
)
# 1. Zeichne 1 Tile -> []
# 2. Zeichne eine Reihe von Tiles -> [][][][][][]
# 3. Zeichne mehrere Reihen von Tiles
# -> [][][][][][]
# -> [][][][][][]
# -> [][][][][][]
2026-10-02 15:22:08 +00:00
# Licht
self.flashlight.render(self.screen)
if self.flashlight.render_item:
self.test_item.render(self.screen)
2026-10-02 15:22:08 +00:00
# Wände
2026-09-18 15:29:30 +00:00
for wall in self.walls:
2026-10-02 15:22:08 +00:00
pygame.draw.rect(self.screen, "grey", wall)
2026-10-02 15:22:08 +00:00
# Spieler
self.player.render(self.screen)
2026-10-02 15:22:08 +00:00
# Surfaces aktualisieren
pygame.display.update()
def create_light_points(self) -> list[pygame.Vector2]:
points: list[pygame.Vector2] = []
origin: Vector2 = self.player.pos
base_ray = pygame.Vector2(LIGHT_RADIUS, 0)
# Lichtstrahlen berechnen
for angle in range(360):
ray_end = origin + base_ray.rotate(angle)
# Überschneidung mit den Wänden prüfen
for wall in self.walls:
# Überschneidung
intersection = wall.clipline(origin, ray_end)
if intersection:
ray_end = pygame.Vector2(intersection[0])
points.append(ray_end)
return points
2026-10-02 15:22:08 +00:00
def run(self) -> None:
while self.running:
self.dt: float = self.clock.tick(60) / 1000.0
self.update()
self.render()
2026-09-08 13:27:52 +00:00
class Player:
2026-10-02 15:22:08 +00:00
def __init__(self, pos: pygame.Vector2, width, height):
self.pos: Vector2 = pos
self.width: float = width
self.height: float = height
2026-09-18 15:29:30 +00:00
self.speed = 150
self.rect = pygame.Rect(
self.pos[0] - self.width / 2, self.pos[1] - self.height / 2, width, height
)
2026-09-08 13:27:52 +00:00
def update(self, walls, dt):
keys = pygame.key.get_pressed()
direction = pygame.Vector2(
keys[pygame.K_d] - keys[pygame.K_a], keys[pygame.K_s] - keys[pygame.K_w]
)
2026-09-08 13:27:52 +00:00
if direction.length_squared() > 0:
direction = direction.normalize()
2026-09-18 15:29:30 +00:00
movement: pygame.Vector2 = direction * self.speed * dt
self.pos.x += movement.x
2026-10-02 15:22:08 +00:00
if self.pos.x - self.width / 2 < 0:
self.pos.x = self.width / 2
if self.pos.x + self.width / 2 > SCREEN_WIDTH:
self.pos.x = SCREEN_WIDTH - self.width / 2
2026-09-18 16:06:44 +00:00
self.rect.centerx = round(self.pos.x)
2026-10-02 15:22:08 +00:00
2026-09-08 13:27:52 +00:00
for wall in walls:
2026-09-18 16:06:44 +00:00
if self.rect.colliderect(wall):
2026-09-08 13:27:52 +00:00
if movement.x > 0:
2026-09-18 16:06:44 +00:00
self.rect.right = wall.left
2026-09-08 13:27:52 +00:00
elif movement.x < 0:
2026-09-18 16:06:44 +00:00
self.rect.left = wall.right
self.pos.x = self.rect.centerx
2026-09-08 13:27:52 +00:00
2026-09-18 15:29:30 +00:00
self.pos.y += movement.y
2026-10-02 15:22:08 +00:00
if self.pos.y - self.height / 2 < 0:
self.pos.y = self.height / 2
if self.pos.y + self.height / 2 > SCREEN_HEIGHT:
self.pos.y = SCREEN_HEIGHT - self.height / 2
2026-09-18 16:06:44 +00:00
self.rect.centery = round(self.pos.y)
2026-09-08 13:27:52 +00:00
for wall in walls:
2026-09-18 16:06:44 +00:00
if self.rect.colliderect(wall):
2026-09-08 13:27:52 +00:00
if movement.y > 0:
2026-09-18 16:06:44 +00:00
self.rect.bottom = wall.top
2026-09-08 13:27:52 +00:00
elif movement.y < 0:
2026-09-18 16:06:44 +00:00
self.rect.top = wall.bottom
self.pos.y = self.rect.centery
2026-09-08 13:27:52 +00:00
def render(self, surface):
2026-09-18 16:06:44 +00:00
pygame.draw.rect(surface, (255, 0, 0), self.rect)
2026-09-08 13:27:52 +00:00
class Taschenlampe:
def __init__(self, game):
self.light_points: list[pygame.Vector2]
self.light_surface: pygame.Surface = pygame.Surface(
(SCREEN_WIDTH, SCREEN_HEIGHT), pygame.SRCALPHA
)
self.light_state: bool = True
self.game = game
self.render_item = False
def update(self):
self.light_points: list[Vector2] = self.game.create_light_points()
def render(self, surface) -> bool:
if self.light_state:
self.light_surface.fill((0, 0, 0, 0))
pygame.draw.polygon(
self.light_surface, (243, 255, 74, 255), self.game.create_light_points()
)
surface.blit(self.light_surface, (0, 0))
light_mask = pygame.mask.from_surface(self.light_surface)
if light_mask.overlap(self.game.test_item_mask, self.game.test_item.rect.topleft) is not None:
self.render_item = True
else:
self.render_item = False
# Item zeichnen, wenn Masken sich überlappen
2026-07-17 15:45:18 +00:00
game = Game()
2026-07-24 15:50:34 +00:00
game.run()
"""
class Taschenlampe erstellen
Methoden: init(), update(), render()
WICHTIG: Alles in ihre richtigen Methoden
"""