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:
|
def __init__(self, game_mode: GameMode) -> None:
|
||||||
|
self.game_mode = game_mode
|
||||||
self._initialize_game_components()
|
self._initialize_game_components()
|
||||||
self._start_background_music()
|
self._start_background_music()
|
||||||
self.game_mode = game_mode # TODO: use this
|
|
||||||
|
|
||||||
def draw(self) -> None:
|
def draw(self) -> None:
|
||||||
"""Update the display."""
|
"""Update the display."""
|
||||||
@ -60,8 +60,8 @@ class Game(BaseScreen):
|
|||||||
self.clock = pygame.time.Clock()
|
self.clock = pygame.time.Clock()
|
||||||
self.next_figure: Figure = self._generate_next_figure()
|
self.next_figure: Figure = self._generate_next_figure()
|
||||||
|
|
||||||
self.tetris = Tetris(self._get_next_figure, self._update_score)
|
self.tetris = Tetris(self._get_next_figure, self._update_score, self.game_mode)
|
||||||
self.score = Score()
|
self.score = Score(self.game_mode)
|
||||||
self.preview = Preview()
|
self.preview = Preview()
|
||||||
|
|
||||||
def _update_score(self, lines: int, score: int, level: int) -> None:
|
def _update_score(self, lines: int, score: int, level: int) -> None:
|
||||||
@ -97,6 +97,7 @@ class Game(BaseScreen):
|
|||||||
|
|
||||||
def _start_background_music(self) -> None:
|
def _start_background_music(self) -> None:
|
||||||
"""Start playing background music."""
|
"""Start playing background music."""
|
||||||
self.music = pygame.mixer.Sound(CONFIG.music.background)
|
if self.game_mode is GameMode.PLAYER:
|
||||||
self.music.set_volume(CONFIG.music.volume)
|
self.music = pygame.mixer.Sound(CONFIG.music.background)
|
||||||
self.music.play(-1)
|
self.music.set_volume(CONFIG.music.volume)
|
||||||
|
self.music.play(-1)
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import pygame
|
import pygame
|
||||||
from utils import CONFIG, Size
|
from utils import CONFIG, GameMode, Size
|
||||||
|
|
||||||
from game.log import log
|
from game.log import log
|
||||||
|
|
||||||
@ -19,7 +19,8 @@ class Score(BaseScreen, SceenElement, TextScreen):
|
|||||||
increment_height: Height of each text element in the score.
|
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_surface()
|
||||||
self._initialize_rect()
|
self._initialize_rect()
|
||||||
self._initialize_font()
|
self._initialize_font()
|
||||||
@ -39,13 +40,14 @@ class Score(BaseScreen, SceenElement, TextScreen):
|
|||||||
score (int): Current game score.
|
score (int): Current game score.
|
||||||
level (int): Current game level.
|
level (int): Current game level.
|
||||||
"""
|
"""
|
||||||
self.text: tuple[tuple[str, int], ...] = (
|
self.text: list[tuple[str, int], ...] = [
|
||||||
("Score", score),
|
("Score", score),
|
||||||
("Level", level),
|
("Level", level),
|
||||||
("Lines", lines),
|
("Lines", lines),
|
||||||
("High Score", CONFIG.game.highscore),
|
("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:
|
def draw(self) -> None:
|
||||||
"""Draw the score on the score surface."""
|
"""Draw the score on the score surface."""
|
||||||
|
|||||||
@ -2,7 +2,7 @@ from typing import Any, Callable, Optional
|
|||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import pygame
|
import pygame
|
||||||
from utils import CONFIG, Direction, Figure, Rotation
|
from utils import CONFIG, Direction, Figure, GameMode, Rotation
|
||||||
|
|
||||||
from game.log import log
|
from game.log import log
|
||||||
from game.sprites.block import Block
|
from game.sprites.block import Block
|
||||||
@ -45,6 +45,7 @@ class Tetris(BaseScreen):
|
|||||||
self,
|
self,
|
||||||
get_next_figure: Callable[[], Figure],
|
get_next_figure: Callable[[], Figure],
|
||||||
update_score: Callable[[int, int, int], None],
|
update_score: Callable[[int, int, int], None],
|
||||||
|
game_mode: GameMode,
|
||||||
) -> None:
|
) -> None:
|
||||||
self._initialize_surface()
|
self._initialize_surface()
|
||||||
self._initialize_rect()
|
self._initialize_rect()
|
||||||
@ -52,6 +53,7 @@ class Tetris(BaseScreen):
|
|||||||
|
|
||||||
self.get_next_figure = get_next_figure
|
self.get_next_figure = get_next_figure
|
||||||
self.update_score = update_score
|
self.update_score = update_score
|
||||||
|
self.game_mode = game_mode
|
||||||
|
|
||||||
self._initialize_grid_surface()
|
self._initialize_grid_surface()
|
||||||
self._initialize_field_and_tetromino()
|
self._initialize_field_and_tetromino()
|
||||||
@ -302,8 +304,9 @@ class Tetris(BaseScreen):
|
|||||||
|
|
||||||
def _initialize_sound(self) -> None:
|
def _initialize_sound(self) -> None:
|
||||||
"""Initialize game sounds."""
|
"""Initialize game sounds."""
|
||||||
self.landing_sound = pygame.mixer.Sound(CONFIG.music.landing)
|
if self.game_mode is GameMode.PLAYER:
|
||||||
self.landing_sound.set_volume(CONFIG.music.volume * 2)
|
self.landing_sound = pygame.mixer.Sound(CONFIG.music.landing)
|
||||||
|
self.landing_sound.set_volume(CONFIG.music.volume * 2)
|
||||||
|
|
||||||
def _play_landing_sound(self) -> None:
|
def _play_landing_sound(self) -> None:
|
||||||
"""Play the landing sound effect."""
|
"""Play the landing sound effect."""
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user