From 9d8db92ea74cccf4b9b3a595a8debe2bb99e667b Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Tue, 22 Dec 2020 19:22:43 +0100 Subject: Inherit the colors Signed-off-by: Olivier Gayot --- cameltris.py | 65 +++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 38 insertions(+), 27 deletions(-) (limited to 'cameltris.py') diff --git a/cameltris.py b/cameltris.py index c97f7c7..f130408 100755 --- a/cameltris.py +++ b/cameltris.py @@ -13,6 +13,9 @@ class WouldCollide(Exception): class Piece(): + def __init__(self): + self.square = square_template.copy() + def rotate_clockwise(self): self.elements = list(zip(*self.elements[::-1])) @@ -24,7 +27,10 @@ class Piece(): class ZPiece(Piece): def __init__(self): - self.elements = (blue_square, blue_square, None), (None, blue_square, blue_square), (None, None, None) + super().__init__() + + self.square.fill(blue) + self.elements = (self.square, self.square, None), (None, self.square, self.square), (None, None, None) self.vertical = False def rotate_clockwise(self): @@ -35,16 +41,19 @@ class ZPiece(Piece): def rotate(self): if self.vertical: - self.elements = (blue_square, blue_square, None), (None, blue_square, blue_square), (None, None, None) + self.elements = (self.square, self.square, None), (None, self.square, self.square), (None, None, None) else: - self.elements = (None, None, blue_square), (None, blue_square, blue_square), (None, blue_square, None) + 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): - self.elements = (None, None, None), (None, green_square, green_square), (green_square, green_square, None) + super().__init__() + + self.square.fill(green) + self.elements = (None, None, None), (None, self.square, self.square), (self.square, self.square, None) self.vertical = False def rotate_clockwise(self): @@ -55,21 +64,27 @@ class SPiece(Piece): def rotate(self): if self.vertical: - self.elements = (None, green_square, green_square), (green_square, green_square, None), (None, None, None) + self.elements = (None, self.square, self.square), (self.square, self.square, None), (None, None, None) else: - self.elements = (None, green_square, None), (None, green_square, green_square), (None, None, green_square) + 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): - self.elements = ((white_square, white_square), (white_square, white_square)) + super().__init__() + + self.square.fill(white) + self.elements = ((self.square, self.square), (self.square, self.square)) class IPiece(Piece): def __init__(self): - self.elements = (None, None, None, None), (None, None, None, None), (red_square, red_square, red_square, red_square), (None, None, None, None) + super().__init__() + + self.square.fill(red) + 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): @@ -80,26 +95,35 @@ class IPiece(Piece): def rotate(self): if self.vertical: - self.elements = (None, None, None, None), (None, None, None, None), (red_square, red_square, red_square, red_square), (None, None, None, None) + 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, red_square, None), (None, None, red_square, None), (None, None, red_square, None), (None, None, red_square, None) + 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): - self.elements = (None, None, None), (red_square, red_square, red_square), (None, None, red_square) + super().__init__() + + self.square.fill(blue) + self.elements = (None, None, None), (self.square, self.square, self.square), (None, None, self.square) class JPiece(Piece): def __init__(self): - self.elements = (None, None, None), (blue_square, blue_square, blue_square), (blue_square, None, None) + super().__init__() + + self.square.fill(red) + self.elements = (None, None, None), (self.square, self.square, self.square), (self.square, None, None) class TPiece(Piece): def __init__(self): - self.elements = [[yellow_square, yellow_square, yellow_square], [None, yellow_square, None], [None, None, None]] + super().__init__() + + self.square.fill(yellow) + self.elements = [[self.square, self.square, self.square], [None, self.square, None], [None, None, None]] def refresh_game_canvas(): @@ -255,20 +279,7 @@ green = (0x20, 0xbb, 0x10) red = (0xbb, 0x10, 0x20) yellow = (0xab, 0xd0, 0x20) -white_square = pygame.Surface((48, 48)) -white_square.fill(white) - -blue_square = pygame.Surface((48, 48)) -blue_square.fill(blue) - -green_square = pygame.Surface((48, 48)) -green_square.fill(green) - -red_square = pygame.Surface((48, 48)) -red_square.fill(red) - -yellow_square = pygame.Surface((48, 48)) -yellow_square.fill(yellow) +square_template = pygame.Surface((48, 48)) screen = pygame.display.set_mode((801, 1000)) -- cgit v1.2.3