/
vechnoilive
/
RuleForge_GenAI_Lab
Обзор
Документация
Войти
/
vechnoilive
/
RuleForge_GenAI_Lab
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
ui.py
85 строк
3 KB
vechnoilive
upload files
20 ноя 2025, 01:00
20 ноя 2025, 01:00
823b56d
Код
Авторство
О чём код?
import pygame from settings import FONT_NAME def draw_text(surface, text, font, color, x, y): """Рисует текст с переданным шрифтом""" surf = font.render(str(text), True, color) surface.blit(surf, (int(x), int(y))) def draw_text_center(surface, text, font, color, x, y): """Рисует текст, выровненный по центру""" surf = font.render(str(text), True, color) rect = surf.get_rect(center=(int(x), int(y))) surface.blit(surf, rect) # ════════════════════════════════════════════════════════════════════════════ # ОПТИМИЗИРОВАННЫЙ DamageText - кеширует render() # ════════════════════════════════════════════════════════════════════════════ class DamageText: """Плавающий текст урона - ОПТИМИЗИРОВАН""" def __init__(self, x, y, text, color, duration=0.7): self.x = x self.y = y self.text = str(text) self.color = color self.timer = 1.0 self.vy = -60 # Upward velocity self.font = pygame.font.Font(None, 24) self.duration = duration # 🔥 ОПТИМИЗАЦИЯ: Кешируем surface один раз! self.text_surface = self.font.render(self.text, True, self.color) self.text_rect = self.text_surface.get_rect() def update(self, dt): """Обновить позицию и прозрачность""" self.y += self.vy * dt self.vy += 200 * dt # Gravity self.timer -= dt # Возвращаем True если ещё живой return self.timer > 0 def render(self, surface): """Рендер - БЕЗ пересчёта render()!""" if self.timer > 0: # 🔥 БЫСТРО: просто blit кешированного surface surface.blit( self.text_surface, (self.x - self.text_rect.width // 2, int(self.y)) ) def draw_text_wrapped(surface, text, font, color, x, y, max_width=800): """Рисует текст с переносом по словам""" words = text.split() lines = [] current_line = "" for word in words: test_line = current_line + (" " if current_line else "") + word test_surf = font.render(test_line, True, color) if test_surf.get_width() > max_width: if current_line: lines.append(current_line) current_line = word else: current_line = test_line if current_line: lines.append(current_line) line_height = font.get_height() total_height = len(lines) * line_height start_y = y - total_height // 2 for i, line in enumerate(lines): surf = font.render(line, True, color) rect = surf.get_rect(center=(x, start_y + i * line_height)) surface.blit(surf, rect)