285 lines
No EOL
8.7 KiB
Python
285 lines
No EOL
8.7 KiB
Python
import pygame
|
|
from pygame.math import Vector2
|
|
from pygame.rect import Rect
|
|
from pygame.typing import SequenceLike
|
|
|
|
from scripts.utils import load_image
|
|
|
|
TILE_SIZE = 64
|
|
COLS = 20
|
|
ROWS = 12
|
|
SCREEN_WIDTH = TILE_SIZE * COLS
|
|
SCREEN_HEIGHT = TILE_SIZE * ROWS
|
|
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, flashlight, game, image: pygame.Surface = None):
|
|
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
|
|
self.visible: bool = False
|
|
self.flashlight = flashlight
|
|
self.game = game
|
|
self.mask = pygame.mask.Mask(self.rect.size, fill=True)
|
|
self.image = image
|
|
|
|
@property
|
|
def rect(self) -> pygame.Rect:
|
|
if not self.image:
|
|
return pygame.Rect(self.x, self.y, self.width, self.height)
|
|
|
|
else:
|
|
return pygame.Rect(self.x, self.y, self.image.get_width(), self.image.get_height())
|
|
|
|
def update(self):
|
|
self.visible = False
|
|
self.visible = self.flashlight.light_mask.overlap(self.mask, self.rect.topleft) is not None
|
|
|
|
|
|
def render(self, surface: pygame.Surface):
|
|
if self.visible and self.flashlight.light_state:
|
|
if not self.image:
|
|
pygame.draw.rect(surface, self.color, self.rect)
|
|
else:
|
|
surface.blit(self.image, (self.x, self.y, self.image.get_width(), self.image.get_height()))
|
|
|
|
|
|
class Game:
|
|
def __init__(self):
|
|
# Pygame setup
|
|
pygame.init()
|
|
pygame.display.set_caption("School Game")
|
|
self.clock = pygame.Clock()
|
|
# Surfaces
|
|
self.screen: pygame.Surface = pygame.display.set_mode(
|
|
(SCREEN_WIDTH, SCREEN_HEIGHT)
|
|
)
|
|
# Pygame-Variablen
|
|
self.running: bool = True
|
|
self.dt: int = 0
|
|
# Spieler
|
|
self.player_pos = pygame.Vector2(
|
|
self.screen.get_width() / 2, self.screen.get_height() / 2
|
|
)
|
|
self.player = Player(self.player_pos, width=PLAYER_SIZE, height=PLAYER_SIZE)
|
|
|
|
# Flashlight
|
|
self.flashlight_player = Flashlight(self, self.player)
|
|
|
|
# Assets
|
|
self.assets = {
|
|
"floor_01": load_image("level/ground/floor_1.png"),
|
|
"plant_01": load_image("items/furniture/plant.png"),
|
|
}
|
|
|
|
# Items
|
|
self.items = [
|
|
Item(140, 300, 20, 20, "blue", self.flashlight_player, self, self.assets["plant_01"]),
|
|
Item(140, 340, 20, 20, "red", self.flashlight_player, self),
|
|
]
|
|
|
|
|
|
# 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),
|
|
]
|
|
|
|
|
|
|
|
def update(self) -> None:
|
|
# Ereignisse
|
|
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_player.light_state = not self.flashlight_player.light_state
|
|
|
|
|
|
if event.type == pygame.KEYUP and event.key == pygame.K_LSHIFT:
|
|
self.player.speed = 150
|
|
|
|
# Spielzustände aktualisieren
|
|
self.player.update(walls=self.walls, dt=self.dt)
|
|
self.flashlight_player.update()
|
|
|
|
for item in self.items:
|
|
item.update()
|
|
|
|
def render(self) -> None:
|
|
# 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
|
|
# -> [][][][][][]
|
|
# -> [][][][][][]
|
|
# -> [][][][][][]
|
|
|
|
# Licht
|
|
self.flashlight_player.render(self.screen)
|
|
|
|
for item in self.items:
|
|
item.render(self.screen)
|
|
|
|
# Wände
|
|
for wall in self.walls:
|
|
pygame.draw.rect(self.screen, "grey", wall)
|
|
|
|
# Spieler
|
|
self.player.render(self.screen)
|
|
|
|
# Surfaces aktualisieren
|
|
pygame.display.update()
|
|
|
|
|
|
|
|
def run(self) -> None:
|
|
while self.running:
|
|
self.dt: float = self.clock.tick(60) / 1000.0
|
|
self.update()
|
|
self.render()
|
|
|
|
|
|
class Player:
|
|
def __init__(self, pos: pygame.Vector2, width, height):
|
|
self.pos: Vector2 = pos
|
|
self.width: float = width
|
|
self.height: float = height
|
|
self.speed = 150
|
|
self.rect = pygame.Rect(
|
|
self.pos[0] - self.width / 2, self.pos[1] - self.height / 2, width, height
|
|
)
|
|
|
|
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]
|
|
)
|
|
|
|
if direction.length_squared() > 0:
|
|
direction = direction.normalize()
|
|
|
|
movement: pygame.Vector2 = direction * self.speed * dt
|
|
|
|
self.pos.x += movement.x
|
|
|
|
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
|
|
self.rect.centerx = round(self.pos.x)
|
|
|
|
for wall in walls:
|
|
if self.rect.colliderect(wall):
|
|
if movement.x > 0:
|
|
self.rect.right = wall.left
|
|
elif movement.x < 0:
|
|
self.rect.left = wall.right
|
|
self.pos.x = self.rect.centerx
|
|
|
|
self.pos.y += movement.y
|
|
|
|
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
|
|
self.rect.centery = round(self.pos.y)
|
|
|
|
for wall in walls:
|
|
if self.rect.colliderect(wall):
|
|
if movement.y > 0:
|
|
self.rect.bottom = wall.top
|
|
elif movement.y < 0:
|
|
self.rect.top = wall.bottom
|
|
self.pos.y = self.rect.centery
|
|
|
|
def render(self, surface):
|
|
pygame.draw.rect(surface, (255, 0, 0), self.rect)
|
|
|
|
|
|
class Flashlight:
|
|
def __init__(self, game, sprite):
|
|
self.game = game
|
|
self.sprite = sprite
|
|
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.light_mask = None
|
|
|
|
def create_light_points(self) -> list[pygame.Vector2]:
|
|
points: list[pygame.Vector2] = []
|
|
origin: Vector2 = self.sprite.pos
|
|
base_ray = pygame.Vector2(LIGHT_RADIUS, 0)
|
|
|
|
for angle in range(360):
|
|
ray_end = origin + base_ray.rotate(angle)
|
|
for wall in self.game.walls:
|
|
intersection = wall.clipline(origin, ray_end)
|
|
if intersection:
|
|
ray_end = pygame.Vector2(intersection[0])
|
|
points.append(ray_end)
|
|
|
|
return points
|
|
|
|
def update(self):
|
|
if not self.light_state:
|
|
return
|
|
|
|
self.light_points = self.create_light_points()
|
|
self.light_surface.fill((0, 0, 0, 0))
|
|
pygame.draw.polygon(self.light_surface, (243, 255, 74, 255), self.light_points)
|
|
self.light_mask = pygame.mask.from_surface(self.light_surface)
|
|
|
|
|
|
def render(self, surface):
|
|
if self.light_state:
|
|
surface.blit(self.light_surface, (0, 0))
|
|
points: list[pygame.Vector2] = []
|
|
origin: Vector2 = self.sprite.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.game.walls:
|
|
# Überschneidung
|
|
intersection = wall.clipline(origin, ray_end)
|
|
if intersection:
|
|
ray_end = pygame.Vector2(intersection[0])
|
|
points.append(ray_end)
|
|
|
|
return points
|
|
|
|
game = Game()
|
|
game.run()
|
|
|
|
|
|
|
|
"""
|
|
Ein paar Assets schonmal zurechtschneiden.
|
|
""" |