Esempio n. 1
0
class PickingHeroView:
    def __init__(self, pygame_screen,
                 images_by_portrait_sprite: Dict[PortraitIconSprite, Any]):
        self.screen_size = pygame_screen.get_size()
        self.screen_render = DrawableArea(pygame_screen)
        self.font_large = pygame.font.Font(DIR_FONTS + 'Merchant Copy.ttf', 32)
        self.font = pygame.font.Font(DIR_FONTS + 'Merchant Copy.ttf', 24)
        self.font_width = 4.5
        self.images_by_portrait_sprite = images_by_portrait_sprite

    def render(self, heroes: List[HeroId], selected_index: int):
        self.screen_render.fill(COLOR_BLACK)
        self.screen_render.rect(
            COLOR_WHITE, Rect(0, 0, self.screen_size[0], self.screen_size[1]),
            1)
        y_0 = 200
        x_mid = self.screen_size[0] // 2
        x_base = x_mid - 175
        self.screen_render.text_centered(self.font_large, "SELECT HERO",
                                         (x_mid, y_0 - 100), 5.5)
        for i, hero in enumerate(heroes):
            hero_data = HEROES[hero]
            sprite = hero_data.portrait_icon_sprite
            image = self.images_by_portrait_sprite[sprite]
            x = x_base + i * (PORTRAIT_ICON_SIZE[0] + 20)
            self.screen_render.image(image, (x, y_0))
            if i == selected_index:
                self.screen_render.rect(
                    COLOR_HIGHLIGHTED_ICON,
                    Rect(x, y_0, PORTRAIT_ICON_SIZE[0], PORTRAIT_ICON_SIZE[1]),
                    3)
            else:
                self.screen_render.rect(
                    COLOR_WHITE,
                    Rect(x, y_0, PORTRAIT_ICON_SIZE[0], PORTRAIT_ICON_SIZE[1]),
                    1)
            self.screen_render.text_centered(
                self.font, hero.name, (x + PORTRAIT_ICON_SIZE[0] // 2,
                                       y_0 + PORTRAIT_ICON_SIZE[1] + 10),
                self.font_width)
        description = HEROES[heroes[selected_index]].description
        description_lines = split_text_into_lines(description, 40)
        y_1 = 350
        for i, description_line in enumerate(description_lines):
            self.screen_render.text(self.font, description_line,
                                    (x_base - 8, y_1 + i * 20))

        y_2 = 500
        self.screen_render.text_centered(self.font, "Choose with arrow-keys",
                                         (x_mid, y_2), self.font_width)
        y_3 = 530
        self.screen_render.text_centered(self.font,
                                         "Press Space/Enter to confirm",
                                         (x_mid, y_3), self.font_width)
class MainMenuView:
    def __init__(self, pygame_screen,
                 images_by_portrait_sprite: Dict[PortraitIconSprite, Any]):
        self._screen_size = pygame_screen.get_size()
        self._screen_render = DrawableArea(pygame_screen)
        self._font = pygame.font.Font(DIR_FONTS + 'Merchant Copy.ttf', 24)
        self._images_by_portrait_sprite = images_by_portrait_sprite
        self._font_width = 2.5

    def render(self, saved_characters: List[SavedPlayerState],
               selected_option_index: int, first_shown_index: int):
        self._screen_render.fill(COLOR_BLACK)
        self._screen_render.rect(
            COLOR_WHITE, Rect(0, 0, self._screen_size[0],
                              self._screen_size[1]), 1)
        x_mid = self._screen_size[0] // 2
        x = x_mid - 175
        x_text = x + PORTRAIT_ICON_SIZE[0] + 25
        y = 50

        if len(saved_characters) == 1:
            top_text = "You have 1 saved character"
        else:
            top_text = "You have " + str(
                len(saved_characters)) + " saved characters"
        x_mid = 350
        self._screen_render.text_centered(self._font, top_text, (x_mid, y),
                                          self._font_width)
        y += 100

        w_rect = 340
        h_rect = 80
        padding = 5
        for i in range(
                first_shown_index,
                min(len(saved_characters),
                    first_shown_index + NUM_SHOWN_SAVE_FILES)):
            saved_player_state = saved_characters[i]
            color = COLOR_HIGHLIGHTED_RECT if i == selected_option_index else COLOR_RECT
            self._screen_render.rect(color, Rect(x, y, w_rect, h_rect), 2)
            image = self._get_portrait_image(saved_player_state)
            self._screen_render.image(image, (x + padding, y + padding))
            self._screen_render.rect(
                COLOR_WHITE,
                Rect(x + padding, y + padding, PORTRAIT_ICON_SIZE[0],
                     PORTRAIT_ICON_SIZE[1]), 1)
            text_1 = saved_player_state.hero_id + " LEVEL " + str(
                saved_player_state.level)

            self._screen_render.text(self._font, text_1, (x_text, y + 20))
            text_2 = "played time: " + _get_time_str(
                saved_player_state.total_time_played_on_character)
            self._screen_render.text(self._font, text_2, (x_text, y + 45))
            y += 90

        y += 40
        color = COLOR_HIGHLIGHTED_RECT if selected_option_index == len(
            saved_characters) else COLOR_RECT
        self._screen_render.rect(color, Rect(x, y, w_rect, h_rect), 2)
        self._screen_render.text_centered(self._font, "CREATE NEW CHARACTER",
                                          (x_mid, y + 32), self._font_width)

    def _get_portrait_image(self, saved_player_state):
        hero_data: HeroData = HEROES[HeroId[saved_player_state.hero_id]]
        sprite = hero_data.portrait_icon_sprite
        image = self._images_by_portrait_sprite[sprite]
        return image