mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2025-10-21 20:00:35 +00:00
feat(game): add pause screen
This commit is contained in:
parent
a136a6ebf7
commit
280c6be84d
@ -4,13 +4,14 @@ import pygame
|
|||||||
from loguru import logger
|
from loguru import logger
|
||||||
from utils import CONFIG, Figure, GameMode
|
from utils import CONFIG, Figure, GameMode
|
||||||
|
|
||||||
from .base import BaseScreen
|
from .base import BaseScreen, SceenElement
|
||||||
|
from .pause import Pause
|
||||||
from .preview import Preview
|
from .preview import Preview
|
||||||
from .score import Score
|
from .score import Score
|
||||||
from .tetris import Tetris
|
from .tetris import Tetris
|
||||||
|
|
||||||
|
|
||||||
class Game(BaseScreen):
|
class Game(BaseScreen, SceenElement):
|
||||||
"""
|
"""
|
||||||
Game class.
|
Game class.
|
||||||
|
|
||||||
@ -33,16 +34,16 @@ class Game(BaseScreen):
|
|||||||
def __init__(self, game_mode: GameMode, settings: dict[str, Any]) -> None:
|
def __init__(self, game_mode: GameMode, settings: dict[str, Any]) -> None:
|
||||||
self.game_mode = game_mode
|
self.game_mode = game_mode
|
||||||
self.settings = settings
|
self.settings = settings
|
||||||
|
self._initialize_surface()
|
||||||
|
self._initialize_rect()
|
||||||
self._initialize_game_components()
|
self._initialize_game_components()
|
||||||
self._start_background_music()
|
self._start_background_music()
|
||||||
self.paused = False
|
self.paused = False
|
||||||
|
|
||||||
def draw(self) -> None:
|
def draw(self) -> None:
|
||||||
"""
|
"""Draw the score on the score surface."""
|
||||||
Raises:
|
self._update_display_surface()
|
||||||
NotImplementedError: Not implemented yet.
|
self._draw_background()
|
||||||
"""
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
"""
|
"""
|
||||||
@ -53,12 +54,15 @@ class Game(BaseScreen):
|
|||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
"""Run a single iteration of the game loop."""
|
"""Run a single iteration of the game loop."""
|
||||||
|
self.draw()
|
||||||
self.tetris.run()
|
self.tetris.run()
|
||||||
self.score.run()
|
self.score.run()
|
||||||
self.preview.update(self.next_figure)
|
self.preview.update(self.next_figure)
|
||||||
self.preview.run()
|
self.preview.run()
|
||||||
|
|
||||||
|
if self.paused:
|
||||||
|
self.pause_screen.draw()
|
||||||
|
|
||||||
self.clock.tick(CONFIG.game.fps)
|
self.clock.tick(CONFIG.game.fps)
|
||||||
|
|
||||||
def mute(self) -> None:
|
def mute(self) -> None:
|
||||||
@ -87,6 +91,7 @@ class Game(BaseScreen):
|
|||||||
self.tetris = Tetris(self._get_next_figure, self._update_score, self.game_mode, self.settings)
|
self.tetris = Tetris(self._get_next_figure, self._update_score, self.game_mode, self.settings)
|
||||||
self.score = Score(self.game_mode)
|
self.score = Score(self.game_mode)
|
||||||
self.preview = Preview()
|
self.preview = Preview()
|
||||||
|
self.pause_screen = Pause()
|
||||||
|
|
||||||
def _update_score(self, lines: int, score: int, level: int) -> None:
|
def _update_score(self, lines: int, score: int, level: int) -> None:
|
||||||
"""
|
"""
|
||||||
@ -125,3 +130,28 @@ class Game(BaseScreen):
|
|||||||
self.music = pygame.mixer.Sound(CONFIG.music.background)
|
self.music = pygame.mixer.Sound(CONFIG.music.background)
|
||||||
self.music.set_volume(self.settings["Volume"]["Music"]["level"])
|
self.music.set_volume(self.settings["Volume"]["Music"]["level"])
|
||||||
self.music.play(-1)
|
self.music.play(-1)
|
||||||
|
|
||||||
|
def _initialize_surface(self) -> None:
|
||||||
|
"""Initialize the pause screen surface."""
|
||||||
|
self.surface = pygame.Surface(CONFIG.window.size)
|
||||||
|
self.display_surface = pygame.display.get_surface()
|
||||||
|
|
||||||
|
def _initialize_rect(self) -> None:
|
||||||
|
"""Initialize the score rectangle."""
|
||||||
|
self.rect = self.surface.get_rect(topleft=(0, 0))
|
||||||
|
|
||||||
|
def _draw_background(self) -> None:
|
||||||
|
"""Draw the background."""
|
||||||
|
self.surface.fill(CONFIG.colors.bg)
|
||||||
|
|
||||||
|
def _update_display_surface(self) -> None:
|
||||||
|
"""Update the display surface."""
|
||||||
|
self.display_surface.blit(self.surface, self.rect)
|
||||||
|
|
||||||
|
def _draw_border(self) -> None:
|
||||||
|
"""Draw the border.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
NotImplementedError: Not implemented yet.
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|||||||
@ -63,14 +63,13 @@ class Main(BaseScreen, SceenElement, TextScreen):
|
|||||||
|
|
||||||
def run_game_loop(self) -> None:
|
def run_game_loop(self) -> None:
|
||||||
"""Run a single iteration of the game loop."""
|
"""Run a single iteration of the game loop."""
|
||||||
if not self.game:
|
if self.game:
|
||||||
|
self.game.run()
|
||||||
|
else:
|
||||||
self.draw()
|
self.draw()
|
||||||
|
|
||||||
self.handle_events()
|
self.handle_events()
|
||||||
|
|
||||||
if self.game:
|
|
||||||
self.game.run()
|
|
||||||
|
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
def exit(self) -> None:
|
def exit(self) -> None:
|
||||||
|
|||||||
71
src/game/screens/pause.py
Normal file
71
src/game/screens/pause.py
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
import pygame
|
||||||
|
from utils import CONFIG
|
||||||
|
|
||||||
|
from .base import BaseScreen, SceenElement, TextScreen
|
||||||
|
|
||||||
|
|
||||||
|
class Pause(BaseScreen, SceenElement, TextScreen):
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self._initialize_surface()
|
||||||
|
self._initialize_rect()
|
||||||
|
self._initialize_font()
|
||||||
|
|
||||||
|
def run(self) -> None:
|
||||||
|
"""
|
||||||
|
Raises:
|
||||||
|
NotImplementedError: Not implemented yet.
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def update(self) -> None:
|
||||||
|
self._update_display_surface()
|
||||||
|
|
||||||
|
def draw(self) -> None:
|
||||||
|
"""Update the display."""
|
||||||
|
self.update()
|
||||||
|
self._draw_background()
|
||||||
|
self._draw_text()
|
||||||
|
|
||||||
|
def _draw_text(self) -> None:
|
||||||
|
"""Draw the text."""
|
||||||
|
self._display_text("Paused")
|
||||||
|
|
||||||
|
def _display_text(self, text: str) -> None:
|
||||||
|
"""Display the text."""
|
||||||
|
text_surface = self.font.render(text, True, CONFIG.colors.fg_float)
|
||||||
|
text_rect = text_surface.get_rect(center=self.rect.center)
|
||||||
|
self.text_surface.blit(text_surface, text_rect)
|
||||||
|
|
||||||
|
def _draw_background(self) -> None:
|
||||||
|
"""Draw the background."""
|
||||||
|
self.surface.fill(CONFIG.colors.bg_float)
|
||||||
|
self.surface.set_alpha(100)
|
||||||
|
self.text_surface.set_colorkey("#000000")
|
||||||
|
self.text_surface.set_alpha(255)
|
||||||
|
|
||||||
|
def _initialize_surface(self) -> None:
|
||||||
|
"""Initialize the pause screen surface."""
|
||||||
|
self.surface = pygame.Surface(CONFIG.window.size)
|
||||||
|
self.display_surface = pygame.display.get_surface()
|
||||||
|
self.text_surface = pygame.Surface(CONFIG.window.size)
|
||||||
|
|
||||||
|
def _initialize_rect(self) -> None:
|
||||||
|
"""Initialize the score rectangle."""
|
||||||
|
self.rect = self.surface.get_rect(topleft=(0, 0))
|
||||||
|
|
||||||
|
def _initialize_font(self) -> None:
|
||||||
|
"""Initialize the font used to display the text."""
|
||||||
|
self.font = pygame.font.Font(CONFIG.font.family, CONFIG.font.size * 2)
|
||||||
|
|
||||||
|
def _update_display_surface(self) -> None:
|
||||||
|
"""Update the display surface."""
|
||||||
|
self.display_surface.blit(self.surface, self.rect)
|
||||||
|
self.display_surface.blit(self.text_surface, self.rect)
|
||||||
|
|
||||||
|
def _draw_border(self) -> None:
|
||||||
|
"""Draw the border.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
NotImplementedError: Not implemented yet.
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
Loading…
Reference in New Issue
Block a user