summaryrefslogtreecommitdiff
path: root/cameltris/piece.py
blob: d290815d4c8e0f0011294c332fd13903769a2f57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
""" 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):
    """ Represents a Z piece that has two positions """
    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):
        """ Rotate the piece. For a Z piece, there is no difference between
        clockwise and counter-clockwise. """
        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):
    """ Represents a S piece that has technically three different positions.
    After flipping the piece for the first time, it will move up one tile.
    """
    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):
        """ Rotate the piece. For a S piece, there is no difference between
        clockwise and counter-clockwise. """
        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):
    """ Represents a square piece (or O piece) that keeps the same position no
    matter how may times it is rotated """
    def __init__(self):
        super().__init__()

        self.square.fill(Color.BROWN.value)
        self.set_elements_from_description([
            (1, 1),
            (1, 1),
        ])


class IPiece(Piece):
    """ Represents a I piece (vertical bar) that can be in two different
    positions """
    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):
        """ Rotate the piece. For an I piece, there is no difference between
        clockwise and counter-clockwise. """
        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):
    """ Represents a L piece that can take four different positions """
    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):
    """ Represents a J piece that can take four different positions """
    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):
    """ Represents a T piece that can take four different positions """
    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),
        ])