refactor: move existing piece logic into class
This commit is contained in:
@@ -6,6 +6,7 @@ window:
|
|||||||
|
|
||||||
engine:
|
engine:
|
||||||
fps: 60
|
fps: 60
|
||||||
|
tile-size: 20
|
||||||
|
|
||||||
# https://coolors.co/6495ed-ee6352-59cd90-fac05e-f79d84
|
# https://coolors.co/6495ed-ee6352-59cd90-fac05e-f79d84
|
||||||
color:
|
color:
|
||||||
|
|||||||
@@ -1,13 +1,29 @@
|
|||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
|
from util.ConfigurationManager import ConfigurationManager
|
||||||
|
|
||||||
'''
|
'''
|
||||||
For information on the Tetris piece Tetromino go here:
|
For information on the Tetris piece Tetromino go here:
|
||||||
https://tetris.fandom.com/wiki/Tetromino
|
https://tetris.fandom.com/wiki/Tetromino
|
||||||
'''
|
'''
|
||||||
class Piece:
|
class Piece:
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def draw(screen):
|
J_SHAPE = ((0, 0), (1, 0), (2, 0), (3, 0), (3, 1), (3, 2), (2, 2), (2, 1), (1, 1), (0, 1))
|
||||||
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 entity.Piece import Piece
|
||||||
from util.ConfigurationManager import ConfigurationManager
|
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):
|
def draw(screen):
|
||||||
# draw window bg
|
# draw window bg
|
||||||
bg_color = pygame.Color(ConfigurationManager.configuration["color"]["cornflower-blue"])
|
bg_color = pygame.Color(ConfigurationManager.configuration["color"]["cornflower-blue"])
|
||||||
screen.fill(bg_color)
|
screen.fill(bg_color)
|
||||||
|
|
||||||
# TODO move piece logic into class
|
fire_opal = ConfigurationManager.configuration["color"]["fire-opal"]
|
||||||
# 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)]
|
|
||||||
|
|
||||||
# scale piece
|
j_piece = Piece(Piece.J_SHAPE, (100, 100), fire_opal)
|
||||||
j_scaled_points = []
|
j_piece.draw(screen)
|
||||||
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)
|
|
||||||
|
|
||||||
# update display
|
# update display
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|||||||
Reference in New Issue
Block a user