From ca100629127cabf9c00fcad4de6e97546c9ced23 Mon Sep 17 00:00:00 2001 From: Giovani Date: Fri, 4 Jun 2021 00:23:35 -0400 Subject: [PATCH] feat: create class for well --- entity/Well.py | 43 +++++++++++++++++++++++++++++++++++++++++++ main.py | 5 ++++- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 entity/Well.py diff --git a/entity/Well.py b/entity/Well.py new file mode 100644 index 0000000..e40f6bb --- /dev/null +++ b/entity/Well.py @@ -0,0 +1,43 @@ +import pygame + +from util.ConfigurationManager import ConfigurationManager + +class Well: + + WIDTH = 10 # standard tetris well width, should not be changed + HEIGHT = 20 # standard tetris well height, should not be changed + + def __init__(self, position): + self.points = self.__get_points(position) + + def draw(self, surface): + well_color_hex = ConfigurationManager.configuration["color"]["border"] # TODO Should abstract out color call? + well_color = pygame.Color(well_color_hex) + + for sub_points in self.points: + pygame.draw.polygon(surface, well_color, sub_points, 0) + + def __get_points(self, position): + tile_size = ConfigurationManager.configuration["engine"]["tile-size"] + + shape = [] + for i in range(self.WIDTH + 2): + for j in range(self.HEIGHT + 2): + if i == 0 or i == self.WIDTH + 1: + shape.append(((i, j), (i + 1, j), (i + 1, j + 1), (i, j + 1))) + elif j == 0 or j == self.HEIGHT + 1: + shape.append(((i, j), (i + 1, j), (i + 1, j + 1), (i, j + 1))) + + points = [] + for square in shape: + sub_points = [] + for vertex in square: + point = [vertex[0] * tile_size + position[0], vertex[1] * tile_size + position[1]] + sub_points.append(point) + points.append(sub_points) + + return points + + + + \ No newline at end of file diff --git a/main.py b/main.py index 0e7a5e1..2495ccd 100644 --- a/main.py +++ b/main.py @@ -1,12 +1,14 @@ ''' Tetris 101: https://strategywiki.org/wiki/Tetris/Getting_Started + https://tetris.com/play-tetris ''' import pygame from util.ConfigurationManager import ConfigurationManager from util.PieceGenerator import PieceGenerator +from entity.Well import Well def draw(screen, objects): # draw window bg @@ -39,6 +41,7 @@ def main(): pygame.display.set_icon(loaded_icon) pieces = [] + well = Well((280, 80)) x = 50 y = 50 for _ in range(6): @@ -65,7 +68,7 @@ def main(): if event.key == pygame.K_DOWN: [piece.move((0, tile_size)) for piece in pieces] - draw(screen, pieces) + draw(screen, pieces + [well]) pygame.quit()