""" Module that defines all types of pieces """ from typing import Optional, Iterable import pygame from .color import Color from .coordinates import Coordinates square_template = pygame.Surface((48, 48)) class Piece: """ A piece class that provides default methods for performing rotations """ def __init__(self): """ Initialize a piece """ self.coord: Optional[Coordinates] = None self.square = square_template.copy() def rotate_clockwise(self): """ Rotate the piece clockwise """ self.elements = list(zip(*self.elements[::-1])) def rotate_counter_clockwise(self): """ Rotate the piece counter-clockwise """ self.rotate_clockwise() self.rotate_clockwise() self.rotate_clockwise() def set_elements_from_description(self, desc: Iterable[Iterable]): """ Set each element according to the description given as an iterable of iterable. Each value from description that evaluates to True produces a square. Each value from description that evaluates to False produces a None value. """ self.elements = list(map(lambda row: list(map(lambda x: self.square if x else None, row)), desc)) class ZPiece(Piece): def __init__(self): super().__init__() self.square.fill(Color.BLUE.value) self.set_elements_from_description([ (1, 1, 0), (0, 1, 1), (0, 0, 0), ]) self.vertical = False def rotate_clockwise(self): self.rotate() def rotate_counter_clockwise(self): self.rotate() def rotate(self): if self.vertical: self.set_elements_from_description([ (1, 1, 0), (0, 1, 1), (0, 0, 0), ]) else: self.set_elements_from_description([ (0, 0, 1), (0, 1, 1), (0, 1, 0), ]) self.vertical = not self.vertical class SPiece(Piece): def __init__(self): super().__init__() self.square.fill(Color.GREEN.value) self.set_elements_from_description([ (0, 0, 0), (0, 1, 1), (1, 1, 0), ]) self.vertical = False def rotate_clockwise(self): self.rotate() def rotate_counter_clockwise(self): self.rotate() def rotate(self): if self.vertical: self.set_elements_from_description([ (0, 1, 1), (1, 1, 0), (0, 0, 0), ]) else: self.set_elements_from_description([ (0, 1, 0), (0, 1, 1), (0, 0, 1), ]) self.vertical = not self.vertical class SquarePiece(Piece): def __init__(self): super().__init__() self.square.fill(Color.BROWN.value) self.set_elements_from_description([ (1, 1), (1, 1), ]) class IPiece(Piece): def __init__(self): super().__init__() self.square.fill(Color.RED.value) self.set_elements_from_description([ (0, 0, 0, 0), (0, 0, 0, 0), (1, 1, 1, 1), (0, 0, 0, 0), ]) self.vertical = False def rotate_clockwise(self): self.rotate() def rotate_counter_clockwise(self): self.rotate() def rotate(self): if self.vertical: self.set_elements_from_description([ (0, 0, 0, 0), (0, 0, 0, 0), (1, 1, 1, 1), (0, 0, 0, 0), ]) else: self.set_elements_from_description([ (0, 0, 1, 0), (0, 0, 1, 0), (0, 0, 1, 0), (0, 0, 1, 0), ]) self.vertical = not self.vertical class LPiece(Piece): def __init__(self): super().__init__() self.square.fill(Color.CYAN.value) self.set_elements_from_description([ (0, 0, 0), (1, 1, 1), (0, 0, 1), ]) class JPiece(Piece): def __init__(self): super().__init__() self.square.fill(Color.PURPLE.value) self.set_elements_from_description([ (0, 0, 0), (1, 1, 1), (1, 0, 0), ]) class TPiece(Piece): def __init__(self): super().__init__() self.square.fill(Color.YELLOW.value) self.set_elements_from_description([ (0, 0, 0), (1, 1, 1), (0, 1, 0), ])