""" Module that defines all types of pieces """ from typing import Optional 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() class ZPiece(Piece): def __init__(self): super().__init__() self.square.fill(Color.BLUE.value) self.elements = (self.square, self.square, None), (None, self.square, self.square), (None, None, None) self.vertical = False def rotate_clockwise(self): self.rotate() def rotate_counter_clockwise(self): self.rotate() def rotate(self): if self.vertical: self.elements = (self.square, self.square, None), (None, self.square, self.square), (None, None, None) else: self.elements = (None, None, self.square), (None, self.square, self.square), (None, self.square, None) self.vertical = not self.vertical class SPiece(Piece): def __init__(self): super().__init__() self.square.fill(Color.GREEN.value) self.elements = (None, None, None), (None, self.square, self.square), (self.square, self.square, None) self.vertical = False def rotate_clockwise(self): self.rotate() def rotate_counter_clockwise(self): self.rotate() def rotate(self): if self.vertical: self.elements = (None, self.square, self.square), (self.square, self.square, None), (None, None, None) else: self.elements = (None, self.square, None), (None, self.square, self.square), (None, None, self.square) self.vertical = not self.vertical class SquarePiece(Piece): def __init__(self): super().__init__() self.square.fill(Color.BROWN.value) self.elements = ((self.square, self.square), (self.square, self.square)) class IPiece(Piece): def __init__(self): super().__init__() self.square.fill(Color.RED.value) self.elements = (None, None, None, None), (None, None, None, None), (self.square, self.square, self.square, self.square), (None, None, None, None) self.vertical = False def rotate_clockwise(self): self.rotate() def rotate_counter_clockwise(self): self.rotate() def rotate(self): if self.vertical: self.elements = (None, None, None, None), (None, None, None, None), (self.square, self.square, self.square, self.square), (None, None, None, None) else: self.elements = (None, None, self.square, None), (None, None, self.square, None), (None, None, self.square, None), (None, None, self.square, None) self.vertical = not self.vertical class LPiece(Piece): def __init__(self): super().__init__() self.square.fill(Color.CYAN.value) self.elements = (None, None, None), (self.square, self.square, self.square), (None, None, self.square) class JPiece(Piece): def __init__(self): super().__init__() self.square.fill(Color.PURPLE.value) self.elements = (None, None, None), (self.square, self.square, self.square), (self.square, None, None) class TPiece(Piece): def __init__(self): super().__init__() self.square.fill(Color.YELLOW.value) self.elements = (None, None, None), (self.square, self.square, self.square), (None, self.square, None)