py_stepik/pygame-wasm/phantomcastle/game/endlevelmenu.py

98 lines
3.9 KiB
Python
Raw Permalink Normal View History

import pygame
from coords import Coords
from common import DrawableGameObject, EventHandler, FONT_NAME, IS_WASM, SurfaceWithRect
class EndLevelMenu(DrawableGameObject, EventHandler):
def __init__(self, scene: DrawableGameObject):
super().__init__(Coords.zero(), scene, scene.assets)
self._surface, self.rect = self._create_end_game_label()
self.win_image = self._create_win_image()
self.win_label = self._create_win_label()
self.keys_hint = self._create_keys_hint()
self.stats_label = None
self.active = False
def _create_end_game_label(self) -> SurfaceWithRect:
"""Надпись завершения игры"""
font = pygame.font.SysFont(FONT_NAME, 70)
text = "Конец игры!"
surface = font.render(text, 1, "#1b10a8c4")
rect = surface.get_rect()
rect.center = Coords(*self.parent.rect.center) + Coords(0, 38)
return SurfaceWithRect(surface, rect)
def _create_keys_hint(self) -> SurfaceWithRect:
"""Совет по кнопкам"""
hint_text = "Для новой игры нажмите N"
if not IS_WASM:
hint_text += ", для выхода Q"
font_hint = pygame.font.SysFont(FONT_NAME, 27)
surface = font_hint.render(hint_text, 1, "#24053da4")
rect = surface.get_rect()
rect.center = self.parent.rect.center
rect = rect.move(Coords(0, 220))
return SurfaceWithRect(surface, rect)
def _create_win_label(self) -> SurfaceWithRect:
"""Надпись для хорошего финала"""
font = pygame.font.SysFont(FONT_NAME, 33)
text = "Все монетки собраны!"
surface = font.render(text, 1, "#96081ba4")
rect = surface.get_rect()
rect.center = self.parent.rect.center
rect.move_ip(Coords(0, -200))
return SurfaceWithRect(surface, rect)
def _create_win_image(self) -> SurfaceWithRect:
"""Картинка для хорошего финала"""
2024-04-07 17:37:02 +00:00
surface = pygame.image.load(self.scene.assets["win.png"]).convert_alpha()
rect = surface.get_rect()
rect.center = self.parent.rect.center
return SurfaceWithRect(surface, rect)
def _create_stats_label(self) -> SurfaceWithRect:
"""Общая статистика игры"""
stats_text = f"Всего пройдено уровней: {self.scene.total_levels}, собрано монет: {self.scene.total_coins}"
stats_font = pygame.font.SysFont(FONT_NAME, 27)
surface = stats_font.render(stats_text, 1, "#031f03a4")
rect = surface.get_rect()
rect.center = Coords(*self.scene.rect.center) + Coords(0, 350)
return SurfaceWithRect(surface, rect)
def draw(self):
if not self.active:
return
if self.scene.coins.all_collected:
self.win_image.draw_to(self.parent.surface)
self.win_label.draw_to(self.parent.surface)
self.parent.surface.blit(self.surface, self.rect)
self.keys_hint.draw_to(self.parent.surface)
# статистика
if self.stats_label is None:
self.stats_label = self._create_stats_label()
self.stats_label.draw_to(self.parent.surface)
def request_new_level(self):
self.scene.want_new_level = True
self.scene.done = True
def handle_keys(self, keys_pressed):
if not self.active:
return
if keys_pressed[pygame.K_n]:
self.request_new_level()
def handle_mouse_event(self, event: pygame.event.Event):
if event.type == pygame.MOUSEBUTTONDOWN and self.keys_hint.rect.collidepoint(
event.pos
):
self.request_new_level()
def handle_event(self, event: pygame.event.Event):
if not self.active:
return
self.handle_mouse_event(event)