Source code for game.ui.combined_graph

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

[docs] MAX_DATA_POINTS = 200
[docs] GRAPH_WIDTH = 200
[docs] GRAPH_HEIGHT = 150
[docs] GRAPH_X = SCREEN_WIDTH - GRAPH_WIDTH - 10
[docs] GRAPH_Y = 10
[docs] class CombinedGraph: """Displays ecosystem health, villager happiness, and pig count on one graph.""" def __init__(self):
[docs] self.rect = pygame.Rect(GRAPH_X, GRAPH_Y, GRAPH_WIDTH, GRAPH_HEIGHT)
[docs] self.font = pygame.font.Font(None, 18)
[docs] self.health_data = []
[docs] self.happiness_data = []
[docs] self.pig_data = []
[docs] self.health_color = (80, 220, 80) # green
[docs] self.happiness_color = (80, 160, 255) # blue
[docs] self.pig_color = (255, 160, 40) # orange
[docs] def add_data_point(self, degradation, happiness, pig_count): """Record one sample. Pig count is normalised against HAPPINESS_MAX_PIG_COUNT.""" self.health_data.append(1.0 - degradation) self.happiness_data.append(happiness) self.pig_data.append(min(1.0, pig_count / max(1, HAPPINESS_MAX_PIG_COUNT))) for data in (self.health_data, self.happiness_data, self.pig_data): if len(data) > MAX_DATA_POINTS: data.pop(0)
def _map_points(self, data): """Map normalised data (0–1) to pixel coordinates inside the graph rect.""" if len(data) < 2: return [] plot_top = self.rect.y + 30 # leave room for legend plot_bottom = self.rect.bottom - 4 plot_height = plot_bottom - plot_top points = [] for i, value in enumerate(data): x = self.rect.x + int((i / (MAX_DATA_POINTS - 1)) * self.rect.width) y = plot_bottom - int(value * plot_height) points.append((x, y)) return points
[docs] def render(self, screen): bg = pygame.Surface(self.rect.size, pygame.SRCALPHA) bg.fill((20, 20, 20, 180)) screen.blit(bg, self.rect.topleft) pygame.draw.rect(screen, (100, 100, 100), self.rect, 1) for data, color in ( (self.health_data, self.health_color), (self.happiness_data, self.happiness_color), (self.pig_data, self.pig_color), ): pts = self._map_points(data) if pts: pygame.draw.lines(screen, color, False, pts, 2) # Legend — three labels on one row labels = [ (localization_manager.get('health_label', default="Health"), self.health_color), (localization_manager.get('happiness_label', default="Happiness"), self.happiness_color), (localization_manager.get('pigs_label', default="Pigs"), self.pig_color), ] x = self.rect.x + 5 for text, color in labels: surf = self.font.render(text, True, color) screen.blit(surf, (x, self.rect.y + 5)) x += surf.get_width() + 8