2023-03-28 10:13:03 +02:00
|
|
|
import pygame
|
|
|
|
|
2023-03-27 15:11:35 +02:00
|
|
|
from level.Level import Level
|
2023-03-28 16:59:29 +02:00
|
|
|
from ui_elements.Background import Background
|
2023-03-28 10:13:03 +02:00
|
|
|
from ui_elements.ClickEvent import ClickEvent
|
2023-03-27 15:11:35 +02:00
|
|
|
from ui_elements.TextLabel import TextLabel
|
|
|
|
|
|
|
|
|
|
|
|
class LevelMenu:
|
|
|
|
|
2023-03-28 16:59:29 +02:00
|
|
|
def __init__(self, levels: [Level], x_pos: float, y_pos: float, width: float, height: float, column_count: int):
|
|
|
|
super().__init__()
|
2023-03-27 15:11:35 +02:00
|
|
|
self.levels = levels
|
|
|
|
self.x_pos = x_pos
|
|
|
|
self.y_pos = y_pos
|
|
|
|
self.level_select_listener = lambda selected_level: None
|
|
|
|
self.level_text_labels = []
|
2023-03-27 17:48:28 +02:00
|
|
|
self.width = width
|
|
|
|
self.height = height
|
|
|
|
self.column_count = column_count
|
2023-03-27 15:11:35 +02:00
|
|
|
|
|
|
|
self.create_level_text_labels()
|
2023-03-28 16:59:29 +02:00
|
|
|
self.background = Background(self.x_pos, self.y_pos, self.width, self.height, (100, 100, 100, 180))
|
2023-03-27 15:11:35 +02:00
|
|
|
|
|
|
|
def create_level_text_labels(self):
|
|
|
|
current_text_label_x = self.x_pos
|
|
|
|
current_text_label_y = self.y_pos
|
|
|
|
|
|
|
|
for index, level in enumerate(self.levels):
|
2023-03-27 18:41:12 +02:00
|
|
|
text_label = TextLabel(level.name, current_text_label_x, current_text_label_y, 60)
|
|
|
|
text_label.position_scale.scale = (0.5, 0.5)
|
2023-03-28 12:30:12 +02:00
|
|
|
text_label.add_click_listener(
|
|
|
|
lambda click_event, selected_level=level:
|
|
|
|
self.handle_level_text_label_click(click_event, selected_level)
|
|
|
|
)
|
2023-03-27 17:48:28 +02:00
|
|
|
self.level_text_labels.append(text_label)
|
2023-03-27 15:11:35 +02:00
|
|
|
|
2023-03-27 17:48:28 +02:00
|
|
|
if (index + 1) % self.column_count == 0:
|
|
|
|
current_text_label_y += self.height / (len(self.levels) / self.column_count)
|
2023-03-27 15:11:35 +02:00
|
|
|
current_text_label_x = self.x_pos
|
|
|
|
else:
|
2023-03-27 17:48:28 +02:00
|
|
|
current_text_label_x += self.width / self.column_count
|
2023-03-27 15:11:35 +02:00
|
|
|
|
2023-03-28 10:13:03 +02:00
|
|
|
def handle_level_text_label_click(self, click_event: ClickEvent, level: Level):
|
|
|
|
if click_event.event.type == pygame.MOUSEBUTTONUP:
|
|
|
|
self.level_select_listener(level)
|
|
|
|
|
2023-03-27 18:41:12 +02:00
|
|
|
def extract_level(self):
|
|
|
|
return self
|