Source code for game.ui.stability_graph

import pygame
from ..utils.constants import SCREEN_WIDTH, SCREEN_HEIGHT
from ..utils.localization import localization_manager

[docs] class StabilityGraph: """A UI component to display ecosystem stability over time.""" def __init__(self, width=200, height=100): """ Initialize the stability graph. Args: width (int): The width of the graph surface. height (int): The height of the graph surface. """
[docs] self.width = width
[docs] self.height = height
[docs] self.x = SCREEN_WIDTH - self.width - 10
[docs] self.y = 10 # Positioned at the top right
[docs] self.bg_color = (20, 20, 20, 180) # Semi-transparent background
[docs] self.font = pygame.font.Font(None, 18)
[docs] self.surface = pygame.Surface((self.width, self.height), pygame.SRCALPHA)
[docs] def render(self, screen, history): """ Render the stability graph onto the screen. Args: screen: The main pygame screen surface. history (list): A list of recent degradation values (0.0 to 1.0). """ self.surface.fill(self.bg_color) pygame.draw.rect(self.surface, (100, 100, 100), self.surface.get_rect(), 1) if history: # Determine line color based on the most recent stability value current_stability = 1.0 - history[-1] if current_stability < 0.5: line_color = (255, 0, 0) # Red for critical elif current_stability < 0.7: line_color = (255, 255, 0) # Yellow for warning else: line_color = (0, 255, 0) # Green for stable # We are plotting stability (1 - degradation) if len(history) > 1: points = [] for i, degradation_value in enumerate(history): px = int((i / (len(history) - 1)) * self.width) # Invert value for stability, and invert y-axis for drawing py = int(self.height - ((1.0 - degradation_value) * self.height)) points.append((px, py)) if len(points) > 1: pygame.draw.lines(self.surface, line_color, False, points, 2) # Draw title using the localization manager title_text = self.font.render(localization_manager.get('stability_graph_title'), True, (255, 255, 255)) self.surface.blit(title_text, (5, 5)) screen.blit(self.surface, (self.x, self.y))