feat: add J piece and adjust center logic
This commit is contained in:
@@ -10,27 +10,28 @@ class Piece: # TODO game objects base class / interface?
|
||||
|
||||
def __init__(self, shape, position, color):
|
||||
self.color = color
|
||||
self.center, self.points = self.__get_points(shape, position)
|
||||
self.points = self.__get_points(shape, position)
|
||||
self.center = self.__get_center(shape, position)
|
||||
|
||||
def __get_points(self, shape, position):
|
||||
tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
|
||||
|
||||
center = ()
|
||||
points = []
|
||||
for square in shape:
|
||||
for square in shape[:-1]:
|
||||
sub_points = []
|
||||
for vertex in square:
|
||||
# convert shape vertex into game surface point
|
||||
point = [vertex[0] * tile_size + position[0], vertex[1] * tile_size + position[1]]
|
||||
sub_points.append(point)
|
||||
|
||||
# store center of gravity
|
||||
if len(vertex) > 2 and vertex[2]:
|
||||
center = point
|
||||
|
||||
points.append(sub_points)
|
||||
|
||||
return center, points
|
||||
return points
|
||||
|
||||
def __get_center(self, shape, position):
|
||||
tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
|
||||
center = shape[-1]
|
||||
|
||||
# cast to int and avoid exception from pygame
|
||||
return [int(center[0] * tile_size + position[0]), int(center[1] * tile_size + position[1])]
|
||||
|
||||
def draw(self, surface):
|
||||
tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
|
||||
@@ -44,6 +45,9 @@ class Piece: # TODO game objects base class / interface?
|
||||
pygame.draw.polygon(surface, border_color, sub_points, max(tile_size // 6, 1))
|
||||
|
||||
def move(self, vector):
|
||||
self.center[0] += vector[0]
|
||||
self.center[1] += vector[1]
|
||||
|
||||
for sub_points in self.points:
|
||||
for point in sub_points:
|
||||
point[0] += vector[0]
|
||||
@@ -59,9 +63,9 @@ class Piece: # TODO game objects base class / interface?
|
||||
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]
|
||||
point[0] = (k * -1) + self.center[0]
|
||||
point[1] = h + self.center[1]
|
||||
|
||||
# shape attributes
|
||||
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)))
|
||||
O_PIECE = ()
|
||||
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)), (2, 0))
|
||||
J_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)), ((2, 1), (3, 1), (3, 2), (2, 2)), (1.5, 0.5))
|
||||
|
||||
14
main.py
14
main.py
@@ -1,3 +1,8 @@
|
||||
'''
|
||||
Tetris 101:
|
||||
https://strategywiki.org/wiki/Tetris/Getting_Started
|
||||
'''
|
||||
|
||||
import pygame
|
||||
|
||||
from entity.Piece import Piece
|
||||
@@ -25,6 +30,7 @@ def main():
|
||||
win_title = ConfigurationManager.configuration["window"]["title"]
|
||||
fps = ConfigurationManager.configuration["engine"]["fps"]
|
||||
base_one_color = ConfigurationManager.configuration["color"]["base-1"]
|
||||
base_two_color = ConfigurationManager.configuration["color"]["base-2"]
|
||||
tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
|
||||
|
||||
screen = pygame.display.set_mode((win_width, win_height))
|
||||
@@ -35,6 +41,7 @@ def main():
|
||||
pygame.display.set_icon(loaded_icon)
|
||||
|
||||
i_piece = Piece(Piece.I_SHAPE, (100, 100), base_one_color)
|
||||
j_piece = Piece(Piece.J_SHAPE, (250, 100), base_two_color)
|
||||
|
||||
is_running = True
|
||||
while is_running:
|
||||
@@ -46,16 +53,21 @@ def main():
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_SPACE:
|
||||
i_piece.rotate()
|
||||
j_piece.rotate()
|
||||
if event.key == pygame.K_LEFT:
|
||||
i_piece.move((-tile_size, 0))
|
||||
j_piece.move((-tile_size, 0))
|
||||
if event.key == pygame.K_RIGHT:
|
||||
i_piece.move((tile_size, 0))
|
||||
j_piece.move((tile_size, 0))
|
||||
if event.key == pygame.K_UP:
|
||||
i_piece.move((0, -tile_size))
|
||||
j_piece.move((0, -tile_size))
|
||||
if event.key == pygame.K_DOWN:
|
||||
i_piece.move((0, tile_size))
|
||||
j_piece.move((0, tile_size))
|
||||
|
||||
draw(screen, [i_piece])
|
||||
draw(screen, [i_piece, j_piece])
|
||||
|
||||
pygame.quit()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user