feat: add next piece logic

This commit is contained in:
Giovani Rodriguez
2021-06-14 14:38:25 -04:00
parent 86cb2e5841
commit 7ee1c08789
2 changed files with 13 additions and 6 deletions

View File

@@ -147,12 +147,13 @@ class Piece(Entity):
self.gravity_time = gravity_time - (game.get_level() * gravity_increase) self.gravity_time = gravity_time - (game.get_level() * gravity_increase)
self.set_time = set_time 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") tile_size = ConfigurationManager.get("engine", "tile-size")
# ghost piece # ghost piece
for square in self._get_ghost_piece_points(well, stack): if well and stack:
pygame.draw.polygon(surface, pygame.Color("#FFFFFF"), square, max(tile_size // 6, 1)) # TODO add white to the yaml 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) super().draw(surface)

View File

@@ -1,10 +1,8 @@
import sys import sys
from typing import Text
import pygame import pygame
from pygame import mixer from pygame import mixer
from tetris.util import ConfigurationManager from tetris.util import ConfigurationManager
from tetris.util import TextGenerator from tetris.util import TextGenerator
from tetris.util import Controller
from tetris.entity import PieceGenerator from tetris.entity import PieceGenerator
from tetris.entity import Well from tetris.entity import Well
from tetris.entity import Stack from tetris.entity import Stack
@@ -19,6 +17,7 @@ class Game:
self.clock = None self.clock = None
self.main_music = None self.main_music = None
self.current_piece = None self.current_piece = None
self.next_piece = None
self.well = None self.well = None
self.stack = None self.stack = None
@@ -53,10 +52,15 @@ class Game:
# TODO write not initialized exception # TODO write not initialized exception
elapsed_time = self.clock.tick(self.fps) elapsed_time = self.clock.tick(self.fps)
if not self.next_piece:
self.next_piece = PieceGenerator.get_piece((620, 160))
if self.current_piece: if self.current_piece:
self.current_piece.update(elapsed_time, self) self.current_piece.update(elapsed_time, self)
else: 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 if self.stack and self.current_piece.collide(self.stack): # TODO game over redo
pygame.quit() pygame.quit()
sys.exit() sys.exit()
@@ -77,6 +81,8 @@ class Game:
self.screen.fill(bg_color) self.screen.fill(bg_color)
# draw all game objects # draw all game objects
if self.next_piece:
self.next_piece.draw(self.screen)
if self.well: if self.well:
self.well.draw(self.screen) self.well.draw(self.screen)
if self.stack: if self.stack: