/
Vibek
/
First_Game
Обзор
Документация
Войти
/
Vibek
/
First_Game
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
views/render.py
133 строки
5 KB
Vibek
все перменные перенесены в конфиг, отрисовка динамическая
21 июн 2026, 20:32
21 июн 2026, 20:32
f32a834
Код
Авторство
О чём код?
import pygame from pygame.math import Vector2 from core.config import Config from models.entity import Entity from models.thrown_sword import ThrownSword, ThrowState from resources.entity_profile import EntityProfile, SpriteLayout class EntityRenderer: """ View: преобразование Model-данных и профилей в пиксели на экране. """ @staticmethod def draw_sprite( surface: pygame.Surface, sprite: pygame.Surface, world_anchor: Vector2, angle_deg: float, layout: SpriteLayout, ) -> None: """Рисует спрайт; world_anchor совпадает с hitbox_center модели.""" width, height = sprite.get_size() pivot = Vector2( width * layout.anchor_x + layout.draw_offset_x, height * layout.anchor_y + layout.draw_offset_y, ) pivot_from_center = pivot - Vector2(width * 0.5, height * 0.5) rotated = pygame.transform.rotozoom(sprite, -angle_deg, 1.0) rot_w, rot_h = rotated.get_size() rotated_pivot_offset = pivot_from_center.rotate(-angle_deg) blit_center = world_anchor - rotated_pivot_offset draw_x = int(blit_center.x - rot_w * 0.5) draw_y = int(blit_center.y - rot_h * 0.5) surface.blit(rotated, (draw_x, draw_y)) @staticmethod def draw_entity_sprite( surface: pygame.Surface, entity: Entity, profile: EntityProfile, sprite: pygame.Surface, ) -> None: """Спрайт привязан к hitbox_center; facing_angle общий для OBB и картинки.""" EntityRenderer.draw_sprite( surface, sprite, entity.hitbox_center, entity.facing_angle, profile.sprite, ) @staticmethod def draw_arena_background(surface: pygame.Surface, background: pygame.Surface) -> None: surface.blit(background, (0, 0)) @staticmethod def draw_thrown_sword( surface: pygame.Surface, sword: ThrownSword, profile: EntityProfile, sprite: pygame.Surface, ) -> None: EntityRenderer.draw_sprite( surface, sprite, sword.collision_center, sword.facing_angle, profile.sprite, ) @staticmethod def draw_thrown_sword_hitbox_debug( surface: pygame.Surface, sword: ThrownSword, color: tuple[int, int, int] | None = None, ) -> None: color = color or Config.UI.DEBUG_SWORD_HITBOX_COLOR if sword.state == ThrowState.OUTWARD: pygame.draw.circle( surface, color, (int(sword.collision_center.x), int(sword.collision_center.y)), int(sword.outward_hit_radius), 1, ) return corners = sword.get_obb_corners() if len(corners) >= 3: points = [(int(point.x), int(point.y)) for point in corners] pygame.draw.polygon(surface, color, points, 1) @staticmethod def draw_hitbox_debug( surface: pygame.Surface, entity: Entity, color: tuple[int, int, int] | None = None, ) -> None: color = color or Config.UI.DEBUG_HITBOX_COLOR corners = entity.get_obb_corners() points = [(int(point.x), int(point.y)) for point in corners] if len(points) >= 3: pygame.draw.polygon(surface, color, points, 1) @staticmethod def draw_rotated_rect(surface: pygame.Surface, center: Vector2, size: tuple, color: tuple, angle_deg: float) -> None: """Fallback-отрисовка прямоугольником (debug).""" surf = pygame.Surface(size, pygame.SRCALPHA) surf.fill((0, 0, 0, 0)) pygame.draw.rect(surf, color, (0, 0, *size)) rotated = pygame.transform.rotate(surf, -angle_deg) rect = rotated.get_rect(center=(int(center.x), int(center.y))) surface.blit(rotated, rect) @staticmethod def draw_attack_arc( surface: pygame.Surface, points: list[tuple[int, int]], color: tuple | None = None, ) -> None: """Рисует полупрозрачный конус атаки по мировым координатам.""" if len(points) != 3: return color = color or Config.UI.ATTACK_ARC_COLOR arc_surf = pygame.Surface(surface.get_size(), pygame.SRCALPHA) pygame.draw.polygon(arc_surf, color, points) surface.blit(arc_surf, (0, 0)) def draw_projectiles(self, projectiles: list, surface: pygame.Surface) -> None: """Отрисовка активных снарядов.""" radius = Config.UI.PROJECTILE_DRAW_RADIUS for proj in projectiles: pos = proj.pos color = Config.CYAN if proj.is_reflected else Config.RED pygame.draw.circle(surface, color, (int(pos.x), int(pos.y)), radius)