Beispiel #1
0
    def slide_right(self) -> bool:
        update = False

        new_board = generate_matrix((self._col, self._row), 0)

        for y in range(self._row):
            new_board[-1][y] = self._board[-1][y]

            x_idx = self._col - 1

            for x in reversed(range(self._col - 1)):
                if self._board[x][y]:
                    if not new_board[x_idx][y]:
                        # Slide into empty tile
                        new_board[x_idx][y] = self._board[x][y]
                        update = True
                    elif self._board[x][y] == new_board[x_idx][y]:
                        # Slide and add
                        new_board[x_idx][y] *= 2
                        x_idx -= 1
                        update = True
                    else:
                        # Slide and pile
                        x_idx -= 1
                        new_board[x_idx][y] = self._board[x][y]

                        # If it isn't just a copy
                        if x_idx != x:
                            update = True

        if update:
            self._board = new_board

        return update
Beispiel #2
0
    def slide_up(self) -> bool:
        update = False

        new_board = generate_matrix((self._col, self._row), 0)

        for x in range(self._col):
            new_board[x][-1] = self._board[x][-1]

            y_idx = self._row - 1

            for y in reversed(range(self._row - 1)):
                if self._board[x][y]:
                    if not new_board[x][y_idx]:
                        # Slide into empty tile
                        new_board[x][y_idx] = self._board[x][y]
                        update = True
                    elif self._board[x][y] == new_board[x][y_idx]:
                        # Slide and add
                        new_board[x][y_idx] *= 2
                        y_idx -= 1
                        update = True
                    else:
                        # Slide and pile
                        y_idx -= 1
                        new_board[x][y_idx] = self._board[x][y]

                        # If isn't only copying
                        if y_idx != y:
                            update = True

        if update:
            self._board = new_board

        return update
Beispiel #3
0
    def slide_left(self) -> bool:
        update = False

        new_board = generate_matrix((self._col, self._row), 0)

        for y in range(self._row):
            new_board[0][y] = self._board[0][y]

            x_idx = 0

            for x in range(1, self._col):
                if self._board[x][y]:
                    if not new_board[x_idx][y]:
                        # Slide into empty tile
                        new_board[x_idx][y] = self._board[x][y]
                        update = True
                    elif self._board[x][y] == new_board[x_idx][y]:
                        # Slide and add
                        new_board[x_idx][y] *= 2
                        x_idx += 1
                        update = True
                    else:
                        # Slide and pile
                        x_idx += 1
                        new_board[x_idx][y] = self._board[x][y]

                        # If it isn't only copying
                        if x_idx != x:
                            update = True

        if update:
            self._board = new_board

        return update
Beispiel #4
0
    def generate_tiles(self):
        self._tiles_batch = pyglet.graphics.Batch()
        self._tiles_grid = generate_matrix(self._board.size)

        for x, y in self._board.indexes:
            ax, ay = self.get_tile_position(x, y)

            self._tiles_grid[x][y] = pyglet.shapes.Rectangle(
                x=ax,
                y=ay,
                width=self._tile_width,
                height=self._tile_height,
                color=c.TILE_COLOR_DICT[self._board[x, y]],
                batch=self._tiles_batch)
Beispiel #5
0
    def generate_labels(self):
        self._labels_batch = pyglet.graphics.Batch()
        self._labels_grid = generate_matrix(self._board.size)

        font_size = self.get_labels_font_size()

        for x, y in self._board.indexes:
            ax, ay = self.get_label_position(x, y)

            self._labels_grid[x][y] = pyglet.text.Label(
                text=self.get_label_text(x, y),
                x=ax,
                y=ay,
                anchor_x='center',
                anchor_y='center',
                color=c.LABEL_COLOR,
                font_name=c.LABEL_FONT,
                font_size=font_size,
                bold=c.LABEL_BOLD,
                batch=self._labels_batch)
Beispiel #6
0
    def __init__(self, size: (int, int)):
        self._col, self._row = size

        self._board = generate_matrix(size, 0)