mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2025-10-21 20:00:35 +00:00
feat(game): add generation of next Figures
This commit is contained in:
parent
2c040b4575
commit
29c0460fb2
@ -1,4 +1,4 @@
|
|||||||
from typing import Optional
|
from typing import Callable, Optional
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import pygame
|
import pygame
|
||||||
@ -11,13 +11,15 @@ from .timer import Timer, Timers
|
|||||||
|
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
def __init__(self) -> None:
|
def __init__(self, get_next_shape: Callable[[], Figure]) -> None:
|
||||||
self.surface = pygame.Surface(CONFIG.game.size)
|
self.surface = pygame.Surface(CONFIG.game.size)
|
||||||
self.dispaly_surface = pygame.display.get_surface()
|
self.dispaly_surface = pygame.display.get_surface()
|
||||||
self.rect = self.surface.get_rect(topleft=CONFIG.game.pos)
|
self.rect = self.surface.get_rect(topleft=CONFIG.game.pos)
|
||||||
|
|
||||||
self.sprites: pygame.sprite.Group[Block] = pygame.sprite.Group()
|
self.sprites: pygame.sprite.Group[Block] = pygame.sprite.Group()
|
||||||
|
|
||||||
|
self.get_next_shape = get_next_shape
|
||||||
|
|
||||||
self._create_grid_surface()
|
self._create_grid_surface()
|
||||||
|
|
||||||
self.field = self._generate_empty_field()
|
self.field = self._generate_empty_field()
|
||||||
@ -91,6 +93,7 @@ class Game:
|
|||||||
self.sprites,
|
self.sprites,
|
||||||
self.create_new_tetromino,
|
self.create_new_tetromino,
|
||||||
self.field,
|
self.field,
|
||||||
|
self.get_next_shape(),
|
||||||
)
|
)
|
||||||
|
|
||||||
def _create_grid_surface(self) -> None:
|
def _create_grid_surface(self) -> None:
|
||||||
|
|||||||
@ -1,11 +1,13 @@
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
from utils import CONFIG
|
from utils import CONFIG, Figure
|
||||||
|
|
||||||
from .game import Game
|
from .game import Game
|
||||||
|
from .log import log
|
||||||
from .preview import Preview
|
from .preview import Preview
|
||||||
from .score import Score
|
from .score import Score
|
||||||
|
from .tetromino import Tetromino
|
||||||
|
|
||||||
|
|
||||||
class Main:
|
class Main:
|
||||||
@ -16,7 +18,9 @@ class Main:
|
|||||||
self.display_surface.fill(CONFIG.colors.bg)
|
self.display_surface.fill(CONFIG.colors.bg)
|
||||||
self.clock = pygame.time.Clock()
|
self.clock = pygame.time.Clock()
|
||||||
|
|
||||||
self.game = Game()
|
self.next_shapes = self._generate_next_shapes()
|
||||||
|
|
||||||
|
self.game = Game(self._get_next_shape)
|
||||||
self.score = Score()
|
self.score = Score()
|
||||||
self.preview = Preview()
|
self.preview = Preview()
|
||||||
|
|
||||||
@ -46,3 +50,11 @@ class Main:
|
|||||||
def exit(self) -> None:
|
def exit(self) -> None:
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
|
def _generate_next_shapes(self, amount: int = 3) -> list[Figure]:
|
||||||
|
return [Figure.random() for _ in range(amount)]
|
||||||
|
|
||||||
|
def _get_next_shape(self) -> Figure:
|
||||||
|
next_shape = self.next_shapes.pop(0)
|
||||||
|
self.next_shapes.append(Figure.random())
|
||||||
|
return next_shape
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user