feat: add rotation logic for piece

This commit is contained in:
2021-06-03 16:47:39 -04:00
parent ae8e379363
commit 13de15c5fd
2 changed files with 31 additions and 16 deletions

View File

@@ -10,24 +10,31 @@ class Piece: # TODO game objects base class / interface?
def __init__(self, shape, position, color): def __init__(self, shape, position, color):
self.color = color self.color = color
self.points = self.__get_points(shape, position) self.center, self.points = self.__get_points(shape, position)
def __get_points(self, shape, position): def __get_points(self, shape, position):
tile_size = ConfigurationManager.configuration["engine"]["tile-size"] tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
center = ()
points = [] points = []
for square in shape: for square in shape:
sub_points = [] sub_points = []
for vertex in square: for vertex in square:
# convert shape vertex into game surface point
point = [vertex[0] * tile_size + position[0], vertex[1] * tile_size + position[1]] point = [vertex[0] * tile_size + position[0], vertex[1] * tile_size + position[1]]
sub_points.append(point) sub_points.append(point)
# store center of gravity
if len(vertex) > 2 and vertex[2]:
center = point
points.append(sub_points) points.append(sub_points)
return points return center, points
def draw(self, surface): def draw(self, surface):
tile_size = ConfigurationManager.configuration["engine"]["tile-size"] tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
hex_color = ConfigurationManager.configuration["color"]["border"] # TODO Should abstract color call? hex_color = ConfigurationManager.configuration["color"]["border"] # TODO Should abstract out color call?
base_color = pygame.Color(self.color) base_color = pygame.Color(self.color)
border_color = pygame.Color(hex_color) border_color = pygame.Color(hex_color)
@@ -44,7 +51,13 @@ class Piece: # TODO game objects base class / interface?
# rotate the piece clockwise # rotate the piece clockwise
def rotate(self): def rotate(self):
pass for sub_points in self.points:
for point in sub_points:
h = point[0] - self.center[0]
k = point[1] - self.center[1]
point[0] = k + self.center[0]
point[1] = (h * -1) + self.center[1]
# shape attributes # shape attributes
I_SHAPE = (((0, 0), (1, 0), (1, 1), (0, 1)), ((1, 0), (2, 0), (2, 1), (1, 1)), ((2, 0), (3, 0), (3, 1), (2, 1)), ((3, 0), (4, 0), (4, 1), (3, 1))) I_SHAPE = (((0, 0), (1, 0), (1, 1), (0, 1)), ((1, 0), (2, 0), (2, 1), (1, 1)), ((2, 0, True), (3, 0), (3, 1), (2, 1)), ((3, 0), (4, 0), (4, 1), (3, 1)))

24
main.py
View File

@@ -24,6 +24,8 @@ def main():
win_icon = ConfigurationManager.configuration["window"]["icon"] win_icon = ConfigurationManager.configuration["window"]["icon"]
win_title = ConfigurationManager.configuration["window"]["title"] win_title = ConfigurationManager.configuration["window"]["title"]
fps = ConfigurationManager.configuration["engine"]["fps"] fps = ConfigurationManager.configuration["engine"]["fps"]
base_one_color = ConfigurationManager.configuration["color"]["base-1"]
tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
screen = pygame.display.set_mode((win_width, win_height)) screen = pygame.display.set_mode((win_width, win_height))
clock = pygame.time.Clock() clock = pygame.time.Clock()
@@ -32,7 +34,6 @@ def main():
pygame.display.set_caption(win_title) pygame.display.set_caption(win_title)
pygame.display.set_icon(loaded_icon) pygame.display.set_icon(loaded_icon)
base_one_color = ConfigurationManager.configuration["color"]["base-1"]
i_piece = Piece(Piece.I_SHAPE, (100, 100), base_one_color) i_piece = Piece(Piece.I_SHAPE, (100, 100), base_one_color)
is_running = True is_running = True
@@ -42,16 +43,17 @@ def main():
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
is_running = False is_running = False
if event.type == pygame.KEYDOWN:
keys = pygame.key.get_pressed() if event.key == pygame.K_SPACE:
if keys[pygame.K_LEFT]: i_piece.rotate()
i_piece.move((-5, 0)) if event.key == pygame.K_LEFT:
if keys[pygame.K_RIGHT]: i_piece.move((-tile_size, 0))
i_piece.move((5, 0)) if event.key == pygame.K_RIGHT:
if keys[pygame.K_UP]: i_piece.move((tile_size, 0))
i_piece.move((0, -5)) if event.key == pygame.K_UP:
if keys[pygame.K_DOWN]: i_piece.move((0, -tile_size))
i_piece.move((0, 5)) if event.key == pygame.K_DOWN:
i_piece.move((0, tile_size))
draw(screen, [i_piece]) draw(screen, [i_piece])