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

View File

@@ -14,4 +14,5 @@ color:
base-1: "#EE6352" base-1: "#EE6352"
base-2: "#59CD90" base-2: "#59CD90"
base-3: "#FAC05E" base-3: "#FAC05E"
base-4: "#4C5B5C"
border: "#DFD9E2" border: "#DFD9E2"

View File

@@ -13,7 +13,7 @@ class Entity:
pass pass
def draw(self, surface: pygame.Surface) -> None: 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: for square in self.points:
pygame.draw.polygon(surface, pygame.Color(self.color), square, 0) pygame.draw.polygon(surface, pygame.Color(self.color), square, 0)

View File

@@ -1,5 +1,4 @@
from typing import List, Tuple from typing import List, Tuple
import pygame
import copy import copy
from entity.Entity import Entity from entity.Entity import Entity

10
entity/Stack.py Normal file
View File

@@ -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

View File

@@ -1,5 +1,4 @@
from typing import List, Tuple from typing import List, Tuple
import pygame
from entity.Entity import Entity from entity.Entity import Entity
from util.ConfigurationManager import ConfigurationManager from util.ConfigurationManager import ConfigurationManager