feat: implement stack

This commit is contained in:
Giovani Rodriguez
2021-06-08 16:09:21 -04:00
parent a9f5320431
commit 7fa913d6ad
6 changed files with 33 additions and 6 deletions

View File

@@ -4,6 +4,7 @@ import pygame
from util.ConfigurationManager import ConfigurationManager
from util.PieceGenerator import PieceGenerator
from entity.Well import Well
from entity.Stack import Stack
# TODO should be a singleton?
class Tetris:
@@ -13,8 +14,9 @@ class Tetris:
self.tile_size = -1
self.screen = None
self.clock = None
self.well = None
self.current_piece = None
self.well = None
self.stack = None
def initialize(self) -> None:
pygame.init()
@@ -29,6 +31,7 @@ class Tetris:
self.screen = pygame.display.set_mode((win_width, win_height))
self.clock = pygame.time.Clock()
self.well = Well((280, 80), ConfigurationManager.configuration["color"]["border"]) # TODO calculate position later and redo color config for well
self.stack = Stack(ConfigurationManager.configuration["color"]["base-4"], ConfigurationManager.configuration["color"]["border"])
loaded_icon = pygame.image.load(win_icon)
pygame.display.set_caption(win_title)
@@ -51,25 +54,38 @@ class Tetris:
self.current_piece.rotate()
if self.well and self.current_piece.collide(self.well):
self.current_piece.revert()
if self.stack and self.current_piece.collide(self.stack):
self.current_piece.revert()
if event.key == pygame.K_LEFT:
self.current_piece.move((-self.tile_size, 0))
if self.well and self.current_piece.collide(self.well):
self.current_piece.revert()
if self.stack and self.current_piece.collide(self.stack):
self.current_piece.revert()
if event.key == pygame.K_RIGHT:
self.current_piece.move((self.tile_size, 0))
if self.well and self.current_piece.collide(self.well):
self.current_piece.revert()
if self.stack and self.current_piece.collide(self.stack):
self.current_piece.revert()
if event.key == pygame.K_UP:
self.current_piece.move((0, -self.tile_size))
if self.well and self.current_piece.collide(self.well):
self.current_piece.revert()
if self.stack and self.current_piece.collide(self.stack):
self.current_piece.revert()
if event.key == pygame.K_DOWN:
self.current_piece.move((0, self.tile_size))
if self.well and self.current_piece.collide(self.well):
self.current_piece.revert()
if self.stack and self.current_piece.collide(self.stack):
self.current_piece.revert()
if event.key == pygame.K_z:
if self.current_piece:
self.stack.add_piece(self.current_piece)
self.__generate_piece((300, 100))
def draw(self) -> None:
# TODO write not initialized exception
@@ -78,10 +94,12 @@ class Tetris:
self.screen.fill(bg_color)
# draw all game objects
if self.well:
self.well.draw(self.screen)
if self.current_piece:
self.current_piece.draw(self.screen)
if self.well:
self.well.draw(self.screen)
if self.stack:
self.stack.draw(self.screen)
# update display
pygame.display.update()