Source code for game.ui.info_popup
"""Information popup system."""
import pygame
import time
from ..utils.constants import *
[docs]
class InfoPopup:
"""Manages information popups and messages."""
def __init__(self):
pygame.font.init()
[docs]
def show_message(self, message, duration):
"""Show a message for a specified duration."""
end_time = time.time() + duration
# Remove duplicate messages
self.messages = [(msg, end) for msg, end in self.messages if msg != message]
# Add new message
self.messages.append((message, end_time))
# Keep only the 3 most recent messages
if len(self.messages) > 3:
self.messages = self.messages[-3:]
[docs]
def update(self, dt):
"""Update popup system."""
current_time = time.time()
self.messages = [(msg, end) for msg, end in self.messages if end > current_time]
[docs]
def render(self, screen):
"""Render active popups."""
current_time = time.time()
for i, (message, end_time) in enumerate(self.messages):
if end_time > current_time:
# Calculate fade effect
remaining_time = end_time - current_time
alpha = min(255, int(remaining_time * 255))
# Render message background
text_surface = self.font.render(message, True, (255, 255, 255))
text_rect = text_surface.get_rect()
# Position messages vertically
y_offset = 150 + i * 40
text_rect.center = (SCREEN_WIDTH // 2, y_offset)
# Background rectangle
bg_rect = text_rect.inflate(20, 10)
bg_surface = pygame.Surface((bg_rect.width, bg_rect.height))
bg_surface.set_alpha(alpha // 2)
bg_surface.fill((0, 0, 0))
# Render background and text
screen.blit(bg_surface, bg_rect.topleft)
# Apply alpha to text
text_surface.set_alpha(alpha)
screen.blit(text_surface, text_rect)