From 2134455b245094930e9fc7312adf9a8049096dd7 Mon Sep 17 00:00:00 2001 From: Giovani Date: Mon, 14 Jun 2021 14:38:25 -0400 Subject: [PATCH] feat: add next piece logic --- tetris/entity.py | 7 ++++--- tetris/game.py | 12 +++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/tetris/entity.py b/tetris/entity.py index 55cc220..e93d765 100644 --- a/tetris/entity.py +++ b/tetris/entity.py @@ -147,12 +147,13 @@ class Piece(Entity): self.gravity_time = gravity_time - (game.get_level() * gravity_increase) self.set_time = set_time - def draw(self, surface: pygame.Surface, well: Well, stack: "Stack") -> None: + def draw(self, surface: pygame.Surface, well: Well = None, stack: "Stack" = None) -> None: tile_size = ConfigurationManager.get("engine", "tile-size") # ghost piece - for square in self._get_ghost_piece_points(well, stack): - pygame.draw.polygon(surface, pygame.Color("#FFFFFF"), square, max(tile_size // 6, 1)) # TODO add white to the yaml + if well and stack: + for square in self._get_ghost_piece_points(well, stack): + pygame.draw.polygon(surface, pygame.Color("#FFFFFF"), square, max(tile_size // 6, 1)) # TODO add white to the yaml super().draw(surface) diff --git a/tetris/game.py b/tetris/game.py index c1fc28b..238ade6 100644 --- a/tetris/game.py +++ b/tetris/game.py @@ -1,10 +1,8 @@ import sys -from typing import Text import pygame from pygame import mixer from tetris.util import ConfigurationManager from tetris.util import TextGenerator -from tetris.util import Controller from tetris.entity import PieceGenerator from tetris.entity import Well from tetris.entity import Stack @@ -19,6 +17,7 @@ class Game: self.clock = None self.main_music = None self.current_piece = None + self.next_piece = None self.well = None self.stack = None @@ -53,10 +52,15 @@ class Game: # TODO write not initialized exception elapsed_time = self.clock.tick(self.fps) + if not self.next_piece: + self.next_piece = PieceGenerator.get_piece((620, 160)) + if self.current_piece: self.current_piece.update(elapsed_time, self) else: - self.current_piece = PieceGenerator.get_piece((360, 100)) # TODO calculate spawn position + self.current_piece = self.next_piece + self.current_piece.move((360 - 620, 100 - 160)) # TODO calculate spawn position correctly + self.next_piece = PieceGenerator.get_piece((620, 160)) # (360, 100) if self.stack and self.current_piece.collide(self.stack): # TODO game over redo pygame.quit() sys.exit() @@ -77,6 +81,8 @@ class Game: self.screen.fill(bg_color) # draw all game objects + if self.next_piece: + self.next_piece.draw(self.screen) if self.well: self.well.draw(self.screen) if self.stack: