mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2025-10-21 20:00:35 +00:00
feat(game): show only 1 next figure
This commit is contained in:
parent
2a2d1412b6
commit
364988a8f8
@ -26,7 +26,7 @@ class Main:
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
# log.info("Initializing the game")
|
||||
log.info("Initializing the game")
|
||||
self._initialize_pygeme()
|
||||
self._initialize_game_components()
|
||||
self._start_background_music()
|
||||
@ -47,7 +47,7 @@ class Main:
|
||||
|
||||
self.game.run()
|
||||
self.score.run()
|
||||
self.preview.run(self.next_figures)
|
||||
self.preview.run(self.next_figure)
|
||||
|
||||
pygame.display.update()
|
||||
self.clock.tick(CONFIG.fps)
|
||||
@ -66,6 +66,14 @@ class Main:
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
|
||||
def _initialize_game_components(self) -> None:
|
||||
"""Initialize game-related components."""
|
||||
self.next_figure: Figure = self._generate_next_figure()
|
||||
|
||||
self.game = Game(self._get_next_figure, self._update_score)
|
||||
self.score = Score()
|
||||
self.preview = Preview()
|
||||
|
||||
def mute(self) -> None:
|
||||
"""Mute the game."""
|
||||
self.music.set_volume(0)
|
||||
@ -82,17 +90,14 @@ class Main:
|
||||
"""
|
||||
self.score.update(lines, score, level)
|
||||
|
||||
def _generate_next_figures(self, amount: int = 3) -> list[Figure]:
|
||||
def _generate_next_figure(self) -> Figure:
|
||||
"""
|
||||
Generate the next set of random figures.
|
||||
|
||||
Args:
|
||||
amount: Number of figures to generate (default is 3).
|
||||
|
||||
Returns:
|
||||
List of randomly generated figures.
|
||||
Randomly generated figure.
|
||||
"""
|
||||
return [Figure.random() for _ in range(amount)]
|
||||
return Figure.random()
|
||||
|
||||
def _get_next_figure(self) -> Figure:
|
||||
"""
|
||||
@ -101,8 +106,8 @@ class Main:
|
||||
Returns:
|
||||
The next figure in the sequence.
|
||||
"""
|
||||
next_figure = self.next_figures.pop(0)
|
||||
self.next_figures.append(Figure.random())
|
||||
next_figure: Figure = self.next_figure
|
||||
self.next_figure = self._generate_next_figure()
|
||||
return next_figure
|
||||
|
||||
def _initialize_pygeme(self) -> None:
|
||||
@ -113,14 +118,6 @@ class Main:
|
||||
self.display_surface.fill(CONFIG.colors.bg)
|
||||
self.clock = pygame.time.Clock()
|
||||
|
||||
def _initialize_game_components(self) -> None:
|
||||
"""Initialize game-related components."""
|
||||
self.next_figures: list[Figure] = self._generate_next_figures()
|
||||
|
||||
self.game = Game(self._get_next_figure, self._update_score)
|
||||
self.score = Score()
|
||||
self.preview = Preview()
|
||||
|
||||
def _start_background_music(self) -> None:
|
||||
"""Start playing background music."""
|
||||
self.music = pygame.mixer.Sound(CONFIG.music.background)
|
||||
|
||||
@ -16,9 +16,8 @@ class Preview:
|
||||
def __init__(self) -> None:
|
||||
self._initialize_surface()
|
||||
self._initialize_rect()
|
||||
self._initialize_increment_height()
|
||||
|
||||
def run(self, next_figures: list[Figure]) -> None:
|
||||
def run(self, next_figure: Figure) -> None:
|
||||
"""
|
||||
Run the preview by updating the display and drawing next figures.
|
||||
|
||||
@ -26,7 +25,7 @@ class Preview:
|
||||
next_figures (list[Figure]): List of upcoming figures.
|
||||
"""
|
||||
self.dispaly_surface.blit(self.surface, self.rect)
|
||||
self._draw_preview(next_figures)
|
||||
self._draw_preview(next_figure)
|
||||
|
||||
def _draw_border(self) -> None:
|
||||
"""Draw the border around the preview surface."""
|
||||
@ -38,17 +37,7 @@ class Preview:
|
||||
CONFIG.game.border_radius,
|
||||
)
|
||||
|
||||
def _draw_figures(self, figures: list[Figure]) -> None:
|
||||
"""
|
||||
Draw the upcoming figures on the preview surface.
|
||||
|
||||
Args:
|
||||
figures (list[Figure]): List of upcoming figures.
|
||||
"""
|
||||
for idx, figure in enumerate(figures):
|
||||
self._draw_figure(figure, idx)
|
||||
|
||||
def _draw_figure(self, figure: Figure, idx: int) -> None:
|
||||
def _draw_figure(self, figure: Figure) -> None:
|
||||
"""
|
||||
Draw a single upcoming figure on the preview surface.
|
||||
|
||||
@ -58,20 +47,20 @@ class Preview:
|
||||
"""
|
||||
figure_surface = figure.value.image
|
||||
x = self.surface.get_width() / 2
|
||||
y = self.increment_height / 2 + idx * self.increment_height
|
||||
y = self.surface.get_height() / 2
|
||||
rect = figure_surface.get_rect(center=(x, y))
|
||||
self.surface.blit(figure_surface, rect)
|
||||
|
||||
def _draw_preview(self, next_figures: list[Figure]) -> None:
|
||||
def _draw_preview(self, next_figure: Figure) -> None:
|
||||
"""
|
||||
Draw the preview with the background, border, and next figures.
|
||||
Draw the preview with the background, border, and next figure.
|
||||
|
||||
Args:
|
||||
next_figures (list[Figure]): List of upcoming figures.
|
||||
"""
|
||||
self._draw_background()
|
||||
self._draw_border()
|
||||
self._draw_figures(next_figures)
|
||||
self._draw_figure(next_figure)
|
||||
|
||||
def _draw_background(self) -> None:
|
||||
"""Draw the background of the preview."""
|
||||
@ -90,7 +79,3 @@ class Preview:
|
||||
CONFIG.window.padding,
|
||||
)
|
||||
)
|
||||
|
||||
def _initialize_increment_height(self) -> None:
|
||||
"""Initialize the increment height for positioning text elements."""
|
||||
self.increment_height = self.surface.get_height() / 3
|
||||
|
||||
@ -21,7 +21,7 @@ class Game:
|
||||
size: Size = Size(columns * cell.width, rows * cell.width)
|
||||
pos: Vec2 = Vec2(padding, padding)
|
||||
offset: Vec2 = Vec2(columns // 2, -1)
|
||||
initial_speed: float | int = 50
|
||||
initial_speed: float | int = 200
|
||||
movment_delay: int = 200
|
||||
rotation_delay: int = 200
|
||||
score: dict[int, int] = {1: 40, 2: 100, 3: 300, 4: 1200}
|
||||
@ -32,7 +32,7 @@ class SideBar:
|
||||
padding: int = PADDING
|
||||
size: Size = Size(200, Game().size.height)
|
||||
score: Size = Size(size.width, size.height * 0.3 - padding)
|
||||
preview: Size = Size(size.width, size.height * 0.7)
|
||||
preview: Size = Size(size.width, size.width)
|
||||
|
||||
|
||||
@define
|
||||
|
||||
Loading…
Reference in New Issue
Block a user