school-horror-game/game.py

153 lines
5.1 KiB
Python
Raw Normal View History

2026-08-26 16:35:06 +00:00
import pygame
from pygame.math import Vector2
from pygame.rect import Rect
2026-08-26 16:35:06 +00:00
2026-08-07 16:20:24 +00:00
SCREEN_WIDTH = 1280
SCREEN_HEIGHT = 720
PLAYER_SIZE = 20
LIGHT_RADIUS = 75
2026-07-17 15:45:18 +00:00
class Game:
def __init__(self):
# Pygame setup
2026-07-17 15:45:18 +00:00
pygame.init()
pygame.display.set_caption("School Game")
self.clock = pygame.Clock()
# Surfaces
2026-09-26 11:19:09 +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))
# 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
# Spieler
2026-07-24 15:50:34 +00:00
self.player_pos = pygame.Vector2(self.display.get_width() / 2, self.display.get_height() / 2)
self.player = Player(self, self.player_pos, width=PLAYER_SIZE, height=PLAYER_SIZE)
# Items
2026-09-18 15:29:30 +00:00
self.item_rect = pygame.Rect(70, 150, 10, 10)
self.flashlight_rect = pygame.Rect(0, 0, 0, 0)
self.color_item = (0, 0, 0)
# Level
2026-09-26 11:19:09 +00:00
self.walls = [pygame.Rect(100, 100, 20, 50), pygame.Rect(300, 300, 200, 20), pygame.Rect(550, 80, 20, 200)]
# Licht
self.light_points: list[pygame.Vector2]
2026-08-19 16:27:21 +00:00
2026-07-17 15:45:18 +00:00
def run(self) -> None:
while self.running:
2026-09-18 15:29:30 +00:00
self.dt: float = self.clock.tick(60) / 1000.0
2026-09-18 15:29:30 +00:00
self.update()
self.calculate_collsions()
self.render()
2026-08-07 16:20:24 +00:00
2026-09-18 15:29:30 +00:00
def update(self) -> None:
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
2026-09-26 11:27:37 +00:00
if event.type == pygame.KEYUP and event.key == pygame.K_LSHIFT:
self.player.speed = 150
2026-08-28 16:46:44 +00:00
2026-09-18 15:29:30 +00:00
self.player.update(walls=self.walls, dt=self.dt)
self.light_points: list[Vector2] = self.create_light_points()
2026-08-19 16:27:21 +00:00
2026-08-26 16:35:06 +00:00
def create_light_points(self)-> list[pygame.Vector2]:
light_points: list[pygame.Vector2] = []
vector = pygame.Vector2(LIGHT_RADIUS, 0)
2026-08-26 16:35:06 +00:00
2026-09-18 15:29:30 +00:00
# Lichtstrahlen berechnen
for i in range(360):
rotated_vector = vector.rotate(i)
ray_end = self.player_pos + rotated_vector
2026-07-17 15:45:18 +00:00
2026-09-18 15:29:30 +00:00
# Überschneidung mit den Wänden prüfen
for wall in self.walls:
überschneidung = wall.clipline(self.player_pos, ray_end)
if überschneidung:
schnittpunkt = pygame.Vector2(überschneidung[0])
ray_end = schnittpunkt
light_points.append(ray_end)
return light_points
def render(self) -> None:
self.display.fill((0, 0, 0, 0))
self.flashlight_rect: Rect = pygame.draw.polygon(self.display, (243, 255, 74), self.light_points)
2026-09-18 15:29:30 +00:00
self.player.render(surface=self.display)
pygame.draw.rect(self.display, self.color_item, self.item_rect)
2026-08-06 09:38:54 +00:00
2026-09-18 15:29:30 +00:00
for wall in self.walls:
pygame.draw.rect(self.display, "grey", wall)
2026-09-18 16:06:44 +00:00
self.screen.blit(pygame.transform.scale(self.display, self.screen.get_size()))
pygame.display.update()
2026-09-18 15:29:30 +00:00
def calculate_collsions(self) -> None:
pass
2026-09-08 13:27:52 +00:00
class Player:
def __init__(self, game, pos: pygame.Vector2, width, height):
self.game = game
self.pos = pos
2026-09-18 16:06:44 +00:00
self.width = width
self.height = height
2026-09-18 15:29:30 +00:00
self.speed = 150
2026-09-26 11:19:09 +00:00
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):
# Smooth Movement
keys = pygame.key.get_pressed()
2026-09-26 11:19:09 +00:00
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-09-26 11:19:09 +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 / 2:
self.pos.x = SCREEN_WIDTH / 2 - self.width / 2
2026-09-18 16:06:44 +00:00
self.rect.centerx = round(self.pos.x)
2026-09-26 11:19:09 +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-09-26 11:19:09 +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 / 2:
self.pos.y = SCREEN_HEIGHT / 2 - 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
2026-07-17 15:45:18 +00:00
game = Game()
2026-07-24 15:50:34 +00:00
game.run()