From 4711bbcae96a1d9b27112577e06122f693dfff42 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Tue, 22 Dec 2020 18:57:22 +0100 Subject: implement default rotation mechanism and add L and J pieces Signed-off-by: Olivier Gayot --- cameltris.py | 51 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 18 deletions(-) (limited to 'cameltris.py') diff --git a/cameltris.py b/cameltris.py index 5ee7984..c97f7c7 100755 --- a/cameltris.py +++ b/cameltris.py @@ -13,14 +13,13 @@ class WouldCollide(Exception): class Piece(): - def rotate(self): - pass - def rotate_clockwise(self): - self.rotate() + self.elements = list(zip(*self.elements[::-1])) def rotate_counter_clockwise(self): - self.rotate() + self.rotate_clockwise() + self.rotate_clockwise() + self.rotate_clockwise() class ZPiece(Piece): @@ -28,6 +27,12 @@ class ZPiece(Piece): self.elements = (blue_square, blue_square, None), (None, blue_square, blue_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 = (blue_square, blue_square, None), (None, blue_square, blue_square), (None, None, None) @@ -42,6 +47,12 @@ class SPiece(Piece): self.elements = (None, None, None), (None, green_square, green_square), (green_square, green_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, green_square, green_square), (green_square, green_square, None), (None, None, None) @@ -61,6 +72,12 @@ class IPiece(Piece): self.elements = (None, None, None, None), (None, None, None, None), (red_square, red_square, red_square, red_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), (red_square, red_square, red_square, red_square), (None, None, None, None) @@ -70,21 +87,19 @@ class IPiece(Piece): self.vertical = not self.vertical -class TPiece(Piece): +class LPiece(Piece): def __init__(self): - self.elements = [[yellow_square, yellow_square, yellow_square], [None, yellow_square, None], [None, None, None]] + self.elements = (None, None, None), (red_square, red_square, red_square), (None, None, red_square) - def rotate_clockwise(self): - # Set the corners - self.elements[0][0], self.elements[0][2], self.elements[2][2], self.elements[2][0] = self.elements[2][0], self.elements[0][0], self.elements[0][2], self.elements[2][2] - # Set the middle squares - self.elements[0][1], self.elements[1][2], self.elements[2][1], self.elements[1][0] = self.elements[1][0], self.elements[0][1], self.elements[1][2], self.elements[2][1] - def rotate_counter_clockwise(self): - # Set the corners - self.elements[2][0], self.elements[0][0], self.elements[0][2], self.elements[2][2] = self.elements[0][0], self.elements[0][2], self.elements[2][2], self.elements[2][0] - # Set the middle squares - self.elements[1][0], self.elements[0][1], self.elements[1][2], self.elements[2][1] = self.elements[0][1], self.elements[1][2], self.elements[2][1], self.elements[1][0] +class JPiece(Piece): + def __init__(self): + self.elements = (None, None, None), (blue_square, blue_square, blue_square), (blue_square, None, None) + + +class TPiece(Piece): + def __init__(self): + self.elements = [[yellow_square, yellow_square, yellow_square], [None, yellow_square, None], [None, None, None]] def refresh_game_canvas(): @@ -193,7 +208,7 @@ def rotate_piece_clockwise(): def generate_piece(): - piece = random.choice((TPiece, SPiece, IPiece, ZPiece, SquarePiece))() + piece = random.choice((TPiece, SPiece, IPiece, ZPiece, SquarePiece, LPiece, JPiece))() for row_id, row in enumerate(piece.elements): if list(filter(lambda x: x is not None, row)): -- cgit v1.2.3