/
sibiskus
/
Drive_Studio
Обзор
Документация
Войти
/
sibiskus
/
Drive_Studio
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
view/lighting.py
112 строк
4 KB
Snezha
ещё раз прошлась по всем файлам, где-то дописала вот эти мини документации, чтобы быстрее ориентироваться по проекту
23 июн 2026, 03:06
23 июн 2026, 03:06
12ea40b
Код
Авторство
О чём код?
import pygame import config def apply_intensity(color, intensity): """Умножает цвет на яркость и возвращает затемненный оттенок""" return ( int(color[0] * intensity), int(color[1] * intensity), int(color[2] * intensity) ) def create_radial_light(radius, color, falloff): """Создает круглую текстуру света с плавным затуханием""" size = radius * 2 light = pygame.Surface((size, size)) center = radius for y in range(size): for x in range(size): dx = x - center dy = y - center distance = (dx**2 + dy**2) ** 0.5 intensity = max(0, 1 - distance / radius) intensity = intensity ** falloff pixel_color = apply_intensity(color, intensity) light.set_at((x, y), pixel_color) return light def create_headlight_cone(width, height, color, falloff, bottom_width): """Создает текстуру света фар в форме расширяющегося конуса""" cone = pygame.Surface((width, height)) center_x = width // 2 bottom_half_width = bottom_width // 2 top_half_width = width // 2 for y in range(height): progress = y / (height - 1) half_width = int(bottom_half_width + (1 - progress) * (top_half_width - bottom_half_width)) for x in range(width): distance_from_center = abs(x - center_x) if half_width <= 0 or distance_from_center > half_width: continue edge_distance = distance_from_center / half_width horizontal_intensity = max(0, 1 - edge_distance**2) vertical_intensity = progress intensity = horizontal_intensity * vertical_intensity intensity = intensity ** falloff pixel_color = apply_intensity(color, intensity) cone.set_at((x, y), pixel_color) return cone class LightmapRenderer: def __init__(self): self.light_map = pygame.Surface((config.SCREEN_WIDTH, config.SCREEN_HEIGHT)) self.player_light_radius = config.PLAYER_LIGHT_RADIUS self.player_light = create_radial_light( self.player_light_radius, config.PLAYER_LIGHT_COLOR, falloff=config.PLAYER_LIGHT_FALLOFF ) self.headlight_width = config.HEADLIGHT_WIDTH self.headlight_height = config.HEADLIGHT_HEIGHT self.headlight_cone = create_headlight_cone( self.headlight_width, self.headlight_height, config.HEADLIGHT_COLOR, falloff=config.HEADLIGHT_FALLOFF, bottom_width=config.HEADLIGHT_BOTTOM_WIDTH ) def apply(self, screen, player): """Накладывает карту освещения на уже нарисованную сцену""" self.light_map.fill(config.NIGHT_AMBIENT_COLOR) player_center_x = int(player.x + player.width // 2) player_center_y = int(player.y + player.height // 2) light_x = player_center_x - self.player_light_radius light_y = player_center_y - self.player_light_radius self.light_map.blit( self.player_light, (light_x, light_y), special_flags=pygame.BLEND_RGB_ADD ) headlight_x = player_center_x - self.headlight_width // 2 headlight_y = int(player.y - self.headlight_height + config.HEADLIGHT_Y_OFFSET) self.light_map.blit( self.headlight_cone, (headlight_x, headlight_y), special_flags=pygame.BLEND_RGB_ADD ) screen.blit(self.light_map, (0, 0), special_flags=pygame.BLEND_RGB_MULT)