From 997465f5006e9fc0da78c04b1954aa9ce73f796a Mon Sep 17 00:00:00 2001 From: Giovani Date: Thu, 3 Jun 2021 14:02:54 -0400 Subject: [PATCH] refactor: move existing piece logic into class --- config.yaml | 1 + entity/Piece.py | 26 +++++++++++++++++++++----- main.py | 21 +++------------------ 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/config.yaml b/config.yaml index b5397c4..7398fb7 100644 --- a/config.yaml +++ b/config.yaml @@ -6,6 +6,7 @@ window: engine: fps: 60 + tile-size: 20 # https://coolors.co/6495ed-ee6352-59cd90-fac05e-f79d84 color: diff --git a/entity/Piece.py b/entity/Piece.py index 87084de..441a2ed 100644 --- a/entity/Piece.py +++ b/entity/Piece.py @@ -1,13 +1,29 @@ import pygame +from util.ConfigurationManager import ConfigurationManager + ''' For information on the Tetris piece Tetromino go here: https://tetris.fandom.com/wiki/Tetromino ''' class Piece: - - def __init__(self): - pass - def draw(screen): - pass \ No newline at end of file + J_SHAPE = ((0, 0), (1, 0), (2, 0), (3, 0), (3, 1), (3, 2), (2, 2), (2, 1), (1, 1), (0, 1)) + + def __init__(self, shape, position, color): + self.color = color + self.points = self.__get_points(shape, position) + + def __get_points(self, shape, position): + tile_size = ConfigurationManager.configuration["engine"]["tile-size"] + points = [] + + for vertex in shape: + point = [vertex[0] * tile_size + position[0], vertex[1] * tile_size + position[1]] + points.append(point) + + return points + + def draw(self, surface): + pygame_color = pygame.Color(self.color) + pygame.draw.polygon(surface, pygame_color, self.points, 0) \ No newline at end of file diff --git a/main.py b/main.py index 3a6b4db..4340f6c 100644 --- a/main.py +++ b/main.py @@ -3,30 +3,15 @@ import pygame from entity.Piece import Piece from util.ConfigurationManager import ConfigurationManager -# potential color palette https://coolors.co/6495ed-ee6352-59cd90-fac05e-f79d84 -TILE_SIZE = 10 # TODO define tile size in config - def draw(screen): # draw window bg bg_color = pygame.Color(ConfigurationManager.configuration["color"]["cornflower-blue"]) screen.fill(bg_color) - # TODO move piece logic into class - # draw j tetromino? - j_points = [(0, 0), (1, 0), (2, 0), (3, 0), (3, 1), (3, 2), (2, 2), (2, 1), (1, 1), (0, 1)] + fire_opal = ConfigurationManager.configuration["color"]["fire-opal"] - # scale piece - j_scaled_points = [] - for point in j_points: - j_scaled_points.append((point[0] * TILE_SIZE, point[1] * TILE_SIZE)) - - # move piece diagonally - j_transalation_points = [] - for point in j_scaled_points: - j_transalation_points.append((point[0] + TILE_SIZE, point[1] + TILE_SIZE)) - - fire_opal = pygame.Color(ConfigurationManager.configuration["color"]["fire-opal"]) - pygame.draw.polygon(screen, fire_opal, j_transalation_points, 0) + j_piece = Piece(Piece.J_SHAPE, (100, 100), fire_opal) + j_piece.draw(screen) # update display pygame.display.update()