mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2025-10-21 20:00:35 +00:00
feat(game): utilize GameMode
This commit is contained in:
parent
82d26ddc03
commit
231640dec6
@ -28,9 +28,9 @@ class Game(BaseScreen):
|
||||
"""
|
||||
|
||||
def __init__(self, game_mode: GameMode) -> None:
|
||||
self.game_mode = game_mode
|
||||
self._initialize_game_components()
|
||||
self._start_background_music()
|
||||
self.game_mode = game_mode # TODO: use this
|
||||
|
||||
def draw(self) -> None:
|
||||
"""Update the display."""
|
||||
@ -60,8 +60,8 @@ class Game(BaseScreen):
|
||||
self.clock = pygame.time.Clock()
|
||||
self.next_figure: Figure = self._generate_next_figure()
|
||||
|
||||
self.tetris = Tetris(self._get_next_figure, self._update_score)
|
||||
self.score = Score()
|
||||
self.tetris = Tetris(self._get_next_figure, self._update_score, self.game_mode)
|
||||
self.score = Score(self.game_mode)
|
||||
self.preview = Preview()
|
||||
|
||||
def _update_score(self, lines: int, score: int, level: int) -> None:
|
||||
@ -97,6 +97,7 @@ class Game(BaseScreen):
|
||||
|
||||
def _start_background_music(self) -> None:
|
||||
"""Start playing background music."""
|
||||
if self.game_mode is GameMode.PLAYER:
|
||||
self.music = pygame.mixer.Sound(CONFIG.music.background)
|
||||
self.music.set_volume(CONFIG.music.volume)
|
||||
self.music.play(-1)
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import pygame
|
||||
from utils import CONFIG, Size
|
||||
from utils import CONFIG, GameMode, Size
|
||||
|
||||
from game.log import log
|
||||
|
||||
@ -19,7 +19,8 @@ class Score(BaseScreen, SceenElement, TextScreen):
|
||||
increment_height: Height of each text element in the score.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
def __init__(self, game_mode: GameMode) -> None:
|
||||
self.game_mode = game_mode
|
||||
self._initialize_surface()
|
||||
self._initialize_rect()
|
||||
self._initialize_font()
|
||||
@ -39,13 +40,14 @@ class Score(BaseScreen, SceenElement, TextScreen):
|
||||
score (int): Current game score.
|
||||
level (int): Current game level.
|
||||
"""
|
||||
self.text: tuple[tuple[str, int], ...] = (
|
||||
self.text: list[tuple[str, int], ...] = [
|
||||
("Score", score),
|
||||
("Level", level),
|
||||
("Lines", lines),
|
||||
("High Score", CONFIG.game.highscore),
|
||||
("Generations", 0),
|
||||
)
|
||||
]
|
||||
if self.game_mode in (GameMode.AI_PLAYING, GameMode.AI_TRAINING):
|
||||
self.text.append(("Generations", 0))
|
||||
|
||||
def draw(self) -> None:
|
||||
"""Draw the score on the score surface."""
|
||||
|
||||
@ -2,7 +2,7 @@ from typing import Any, Callable, Optional
|
||||
|
||||
import numpy as np
|
||||
import pygame
|
||||
from utils import CONFIG, Direction, Figure, Rotation
|
||||
from utils import CONFIG, Direction, Figure, GameMode, Rotation
|
||||
|
||||
from game.log import log
|
||||
from game.sprites.block import Block
|
||||
@ -45,6 +45,7 @@ class Tetris(BaseScreen):
|
||||
self,
|
||||
get_next_figure: Callable[[], Figure],
|
||||
update_score: Callable[[int, int, int], None],
|
||||
game_mode: GameMode,
|
||||
) -> None:
|
||||
self._initialize_surface()
|
||||
self._initialize_rect()
|
||||
@ -52,6 +53,7 @@ class Tetris(BaseScreen):
|
||||
|
||||
self.get_next_figure = get_next_figure
|
||||
self.update_score = update_score
|
||||
self.game_mode = game_mode
|
||||
|
||||
self._initialize_grid_surface()
|
||||
self._initialize_field_and_tetromino()
|
||||
@ -302,6 +304,7 @@ class Tetris(BaseScreen):
|
||||
|
||||
def _initialize_sound(self) -> None:
|
||||
"""Initialize game sounds."""
|
||||
if self.game_mode is GameMode.PLAYER:
|
||||
self.landing_sound = pygame.mixer.Sound(CONFIG.music.landing)
|
||||
self.landing_sound.set_volume(CONFIG.music.volume * 2)
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user