Pygame 俄罗斯方块填满整列

Pygame Tetris Filling Entire Column

好的,所以我正在使用 Pygame 来制作俄罗斯方块,我的问题是,当块到达底部时,程序将块绘制到整列,而不是仅绘制它要绘制的行.我正在使用带有二维块对象列表的板对象。

class Block:
    def __init__(self, exists, color):
        self.exists = exists
        self.color = color

class Board:
    def __init__(self, state, bottom, points, level, alive):
        self.state = state
        self.alive = alive
        self.bottom = bottom
        self.points = points
        self.level = level

# Different Script
board = Board.Board([[Board.Block(False, (0, 0, 0))] * 10] * 20, [670, 670, 670, 670, 670, 670, 670, 670, 670, 670], 0, 1, True)

我觉得问题应该出在这里。似乎程序正在用 exists = True 的块填充整个列,但我不知道为什么。我对 if/when 文章的逻辑触底:

    bottom = [None] * len(piece.shape)
    for col in range(len(piece.bottom())):
        index = int(((piece.position[0] - 10) / 30) - 8 + col)
        bottom[col] = board.bottom[index]

    for col in range(len(bottom)):
        if(piece.bottom()[col] == bottom[col]):
            new_piece = True
            break

    if(new_piece):
        for row in range(len(piece.shape)):
            for col in range(len(piece.shape[row])):
                if(piece.shape[row][col] == 1):
                    block = Board.Block(True, piece.color)
                    index_col = int(((piece.position[0] - 10) / 30) - 8 + col)
                    index_row = int(((piece.position[1] - 10) / 30) - 3 + row)
                    board.state[index_row][index_col] = block
        piece = PieceSelector.selector()

我对 index_row/index_col 进行如此奇怪的计算的原因是因为棋盘是一个 pygame.rect 对象,尺寸为 (250, 100, 300, 600)。这就是将像素值转换为列表索引的简单算法。了解 Piece 可能会有所帮助 class:

class Piece:
    def __init__(self, position, shape, index, color):
        self.shape = shape
        self.color = color
        self.index = index
        self.position = position

# Example Piece
long_piece = Piece([250, 100], [[1, 1, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], 0, [255, 128, 0])

这也是实际绘制方块的函数。我不认为这是问题所在,但我可能仍然应该包括它。

    def draw_blocks(self, screen):
        for row in range(len(self.state)):
            for col in range(len(self.state[row])):
                if(self.state[row][col].exists):
                    row_pxl = row * 30 + 100
                    col_pxl = col * 30 + 250
                    py.draw.rect(screen, self.state[row][col].color, (col_pxl, row_pxl, 29, 29))

Example of problem

我还没有检查你所有的代码,但明显的问题是当你做 bottom = [None] * len(piece.shape) 时,这基本上会创建对同一对象的多个引用,因此当一个更改时 - 所有更改。

尝试将其更改为:bottom = [None for _ in range(len(piece.shape))]