import pygame from pygame.math import Vector2 from scripts.constants import * 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.get_rect().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.get_rect().clipline(origin, ray_end) if intersection: ray_end = pygame.Vector2(intersection[0]) points.append(ray_end) return points