From b42a2c377b600edf2c94641b75146b55e70d3646 Mon Sep 17 00:00:00 2001 From: Giovani Date: Tue, 8 Jun 2021 16:09:21 -0400 Subject: [PATCH] feat: implement stack --- Tetris.py | 24 +++++++++++++++++++++--- config.yaml | 1 + entity/Entity.py | 2 +- entity/Piece.py | 1 - entity/Stack.py | 10 ++++++++++ entity/Well.py | 1 - 6 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 entity/Stack.py diff --git a/Tetris.py b/Tetris.py index 01c1cf1..4d1ef6d 100644 --- a/Tetris.py +++ b/Tetris.py @@ -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() diff --git a/config.yaml b/config.yaml index 9bc4850..a7c56c4 100644 --- a/config.yaml +++ b/config.yaml @@ -14,4 +14,5 @@ color: base-1: "#EE6352" base-2: "#59CD90" base-3: "#FAC05E" + base-4: "#4C5B5C" border: "#DFD9E2" diff --git a/entity/Entity.py b/entity/Entity.py index e7ace87..e7f3159 100644 --- a/entity/Entity.py +++ b/entity/Entity.py @@ -13,7 +13,7 @@ class Entity: pass def draw(self, surface: pygame.Surface) -> None: - tile_size = ConfigurationManager.configuration["engine"]["tile-size"] + tile_size = ConfigurationManager.configuration["engine"]["tile-size"] for square in self.points: pygame.draw.polygon(surface, pygame.Color(self.color), square, 0) diff --git a/entity/Piece.py b/entity/Piece.py index 748c963..c63ced8 100644 --- a/entity/Piece.py +++ b/entity/Piece.py @@ -1,5 +1,4 @@ from typing import List, Tuple -import pygame import copy from entity.Entity import Entity diff --git a/entity/Stack.py b/entity/Stack.py new file mode 100644 index 0000000..ee060d5 --- /dev/null +++ b/entity/Stack.py @@ -0,0 +1,10 @@ +from entity.Piece import Piece +from entity.Entity import Entity + +class Stack(Entity): + + def __init__(self, color: str, border_color: str): + super().__init__([], color, border_color) + + def add_piece(self, piece: Piece): + self.points += piece.points \ No newline at end of file diff --git a/entity/Well.py b/entity/Well.py index a9b6417..129488e 100644 --- a/entity/Well.py +++ b/entity/Well.py @@ -1,5 +1,4 @@ from typing import List, Tuple -import pygame from entity.Entity import Entity from util.ConfigurationManager import ConfigurationManager