refactor: move existing piece logic into class
This commit is contained in:
@@ -6,6 +6,7 @@ window:
|
||||
|
||||
engine:
|
||||
fps: 60
|
||||
tile-size: 20
|
||||
|
||||
# https://coolors.co/6495ed-ee6352-59cd90-fac05e-f79d84
|
||||
color:
|
||||
|
||||
@@ -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
|
||||
J_SHAPE = ((0, 0), (1, 0), (2, 0), (3, 0), (3, 1), (3, 2), (2, 2), (2, 1), (1, 1), (0, 1))
|
||||
|
||||
def draw(screen):
|
||||
pass
|
||||
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)
|
||||
21
main.py
21
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()
|
||||
|
||||
Reference in New Issue
Block a user