Source code for game.ui.tutorial_popup
import pygame
from ..utils.constants import SCREEN_WIDTH, SCREEN_HEIGHT
from ..utils.localization import localization_manager
[docs]
class TutorialPopup:
"""A modal dialog box for displaying tutorial steps that pauses the game."""
def __init__(self):
# Define layout
[docs]
self.box_rect = pygame.Rect(
(SCREEN_WIDTH - self.box_width) // 2,
(SCREEN_HEIGHT - self.box_height) // 2,
self.box_width,
self.box_height
)
[docs]
self.button_rect = pygame.Rect(
self.box_rect.centerx - self.button_width // 2,
self.box_rect.bottom - self.button_height - 20,
self.button_width,
self.button_height
)
# Fonts
[docs]
def show(self, message_key, is_last_message=False):
"""Activates the popup with a specific message."""
self.message = localization_manager.get(message_key)
self.button_text = localization_manager.get('tutorial_done_button') if is_last_message else localization_manager.get('tutorial_next_button')
self.is_active = True
[docs]
def handle_click(self, pos):
"""Checks if the button was clicked and hides the popup if so."""
if self.is_active and self.button_rect.collidepoint(pos):
self.hide()
return True # Indicates the button was successfully clicked
return False
[docs]
def render(self, screen):
"""Draws the popup if it's active."""
if not self.is_active:
return
# Draw a semi-transparent overlay to dim the background
overlay = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.SRCALPHA)
overlay.fill((0, 0, 0, 170))
screen.blit(overlay, (0, 0))
# Draw the message box background (similar to info_popup)
box_surface = pygame.Surface(self.box_rect.size, pygame.SRCALPHA)
box_surface.fill((0, 0, 0, 180)) # Semi-transparent black
screen.blit(box_surface, self.box_rect.topleft)
# Draw the border
#pygame.draw.rect(screen, (255, 255, 255), self.box_rect, 2, border_radius=15)
# Draw the message text with word wrapping
words = self.message.split(' ')
lines = []
current_line = ""
for word in words:
test_line = f"{current_line} {word}"
if self.font.size(test_line)[0] < self.box_width - 40:
current_line = test_line
else:
lines.append(current_line.strip())
current_line = word
lines.append(current_line.strip())
y_offset = self.box_rect.y + 30
for line in lines:
text_surf = self.font.render(line, True, (255, 255, 255))
text_rect = text_surf.get_rect(centerx=self.box_rect.centerx, y=y_offset)
screen.blit(text_surf, text_rect)
y_offset += self.font.get_linesize()
# Draw the button with the new color scheme
pygame.draw.rect(screen, (50, 50, 50), self.button_rect, border_radius=10) # Dark grey fill
pygame.draw.rect(screen, (255, 255, 255), self.button_rect, 2, border_radius=10) # White border
button_text_surf = self.button_font.render(self.button_text, True, (255, 255, 255))
button_text_rect = button_text_surf.get_rect(center=self.button_rect.center)
screen.blit(button_text_surf, button_text_rect)