feat: create class that generates pieces
This commit is contained in:
@@ -13,26 +13,6 @@ class Piece: # TODO game objects base class / interface?
|
|||||||
self.points = self.__get_points(shape, position)
|
self.points = self.__get_points(shape, position)
|
||||||
self.center = self.__get_center(shape, position)
|
self.center = self.__get_center(shape, position)
|
||||||
|
|
||||||
def __get_points(self, shape, position):
|
|
||||||
tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
|
|
||||||
|
|
||||||
points = []
|
|
||||||
for square in shape[:-1]:
|
|
||||||
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
|
|
||||||
|
|
||||||
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):
|
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 out color call?
|
hex_color = ConfigurationManager.configuration["color"]["border"] # TODO Should abstract out color call?
|
||||||
@@ -66,6 +46,26 @@ class Piece: # TODO game objects base class / interface?
|
|||||||
point[0] = (k * -1) + self.center[0]
|
point[0] = (k * -1) + self.center[0]
|
||||||
point[1] = h + self.center[1]
|
point[1] = h + self.center[1]
|
||||||
|
|
||||||
|
def __get_points(self, shape, position):
|
||||||
|
tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
|
||||||
|
|
||||||
|
points = []
|
||||||
|
for square in shape[:-1]:
|
||||||
|
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
|
||||||
|
|
||||||
|
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])]
|
||||||
|
|
||||||
# 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)), (2, 0))
|
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))
|
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))
|
||||||
|
|||||||
30
main.py
30
main.py
@@ -5,8 +5,8 @@
|
|||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
from entity.Piece import Piece
|
|
||||||
from util.ConfigurationManager import ConfigurationManager
|
from util.ConfigurationManager import ConfigurationManager
|
||||||
|
from util.PieceGenerator import PieceGenerator
|
||||||
|
|
||||||
def draw(screen, objects):
|
def draw(screen, objects):
|
||||||
# draw window bg
|
# draw window bg
|
||||||
@@ -29,8 +29,6 @@ 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"]
|
|
||||||
base_two_color = ConfigurationManager.configuration["color"]["base-2"]
|
|
||||||
tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
|
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))
|
||||||
@@ -40,8 +38,13 @@ 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)
|
||||||
|
|
||||||
i_piece = Piece(Piece.I_SHAPE, (100, 100), base_one_color)
|
pieces = []
|
||||||
piece = Piece(Piece.Z_SHAPE, (250, 100), base_two_color)
|
x = 50
|
||||||
|
y = 50
|
||||||
|
for _ in range(6):
|
||||||
|
pieces.append(PieceGenerator.get_piece((x, y)))
|
||||||
|
x += 120
|
||||||
|
y += 100
|
||||||
|
|
||||||
is_running = True
|
is_running = True
|
||||||
while is_running:
|
while is_running:
|
||||||
@@ -52,22 +55,17 @@ def main():
|
|||||||
is_running = False
|
is_running = False
|
||||||
if event.type == pygame.KEYDOWN:
|
if event.type == pygame.KEYDOWN:
|
||||||
if event.key == pygame.K_SPACE:
|
if event.key == pygame.K_SPACE:
|
||||||
i_piece.rotate()
|
[piece.rotate() for piece in pieces]
|
||||||
piece.rotate()
|
|
||||||
if event.key == pygame.K_LEFT:
|
if event.key == pygame.K_LEFT:
|
||||||
i_piece.move((-tile_size, 0))
|
[piece.move((-tile_size, 0)) for piece in pieces]
|
||||||
piece.move((-tile_size, 0))
|
|
||||||
if event.key == pygame.K_RIGHT:
|
if event.key == pygame.K_RIGHT:
|
||||||
i_piece.move((tile_size, 0))
|
[piece.move((tile_size, 0)) for piece in pieces]
|
||||||
piece.move((tile_size, 0))
|
|
||||||
if event.key == pygame.K_UP:
|
if event.key == pygame.K_UP:
|
||||||
i_piece.move((0, -tile_size))
|
[piece.move((0, -tile_size)) for piece in pieces]
|
||||||
piece.move((0, -tile_size))
|
|
||||||
if event.key == pygame.K_DOWN:
|
if event.key == pygame.K_DOWN:
|
||||||
i_piece.move((0, tile_size))
|
[piece.move((0, tile_size)) for piece in pieces]
|
||||||
piece.move((0, tile_size))
|
|
||||||
|
|
||||||
draw(screen, [i_piece, piece])
|
draw(screen, pieces)
|
||||||
|
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import yaml
|
|||||||
|
|
||||||
CONFIG_FILE_LOCATION = "config.yaml"
|
CONFIG_FILE_LOCATION = "config.yaml"
|
||||||
|
|
||||||
|
# TODO add getter for configuration?
|
||||||
class ConfigurationManager:
|
class ConfigurationManager:
|
||||||
|
|
||||||
configuration = []
|
configuration = []
|
||||||
|
|||||||
34
util/PieceGenerator.py
Normal file
34
util/PieceGenerator.py
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
|
from entity.Piece import Piece
|
||||||
|
from util.ConfigurationManager import ConfigurationManager
|
||||||
|
|
||||||
|
class PieceGenerator:
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_piece(cls, position):
|
||||||
|
piece = Piece(cls.__get_piece_shape(), position, cls.__get_piece_color())
|
||||||
|
[piece.rotate() for _ in range(random.randint(0, 3))] # randomize rotated position
|
||||||
|
return piece
|
||||||
|
|
||||||
|
def __get_piece_shape():
|
||||||
|
random_number = random.randint(0, 6)
|
||||||
|
|
||||||
|
if random_number == 0:
|
||||||
|
return Piece.I_SHAPE
|
||||||
|
if random_number == 1:
|
||||||
|
return Piece.J_SHAPE
|
||||||
|
if random_number == 2:
|
||||||
|
return Piece.L_SHAPE
|
||||||
|
if random_number == 3:
|
||||||
|
return Piece.O_SHAPE
|
||||||
|
if random_number == 4:
|
||||||
|
return Piece.S_SHAPE
|
||||||
|
if random_number == 5:
|
||||||
|
return Piece.T_SHAPE
|
||||||
|
if random_number == 6:
|
||||||
|
return Piece.Z_SHAPE
|
||||||
|
|
||||||
|
def __get_piece_color():
|
||||||
|
random_number = random.randint(1, 3)
|
||||||
|
return ConfigurationManager.configuration["color"]["base-" + str(random_number)]
|
||||||
Reference in New Issue
Block a user