joriszwart.nl

I code dreams™

bucket

Download
from blocks import CLEAR, blocks, colors


create_row = lambda column: [CLEAR for column in range(WIDTH)]


HEIGHT = 20
WIDTH = 10


class Bucket:

    def __init__(self):
        self.bucket = [create_row(_) for _ in range(HEIGHT)]

    def draw_tetromino(self, row, column, block, rotation, clear=False):
        for color in blocks[block][rotation]:
            (x, y) = (color[0] + column, color[1] + row)

            self.bucket[y][x] = CLEAR if clear else colors[block]

    def clear_tetromino(self, row, column, block, rotation):
        self.draw_tetromino(row, column, block, rotation, True)

    def test_tetromino(self, row, column, block, rotation):
        for color in blocks[block][rotation]:
            (x, y) = (color[0] + column, color[1] + row)

            if x < 0 or x >= WIDTH or y >= HEIGHT:
                return False

            if self.bucket[y][x] != CLEAR:
                return False

        return True

    def clear_lines(self):
        # remove full lines
        self.bucket = [row for row in self.bucket if CLEAR in row]
        lines_cleared = HEIGHT - len(self.bucket)

        # add rows on top
        self.bucket = [create_row(row) for row in range(lines_cleared)] + self.bucket

        return lines_cleared