Python Pygame 俄罗斯方块克隆中的重复行

Duplicate rows in Python Pygame Tetris clone

我研究这个问题已经有一段时间了,但一直空白,希望能得到一些帮助。

我正在使用 Python 和 Pygame 制作一个简单的俄罗斯方块克隆。问题是检测已完成行的过程有时会将克隆行添加到新 GRID,因此任何影响原始行的内容也会影响克隆行。

这里是删除完整行的代码:

# create an empty grid
newGRID = []

fullRowsCount = 0
for row in reversed(GRID):
    # count number of non-zero blocks in row
    rowBlockCount = sum(col > 0 for col in row)
    
    # if number of non-zero blocks is less than the width of grid then copy to newGRID
    if rowBlockCount < GRID_SIZE.width:

        # insert non-full row into newGRID
        newGRID.insert(0, row)

    else:
        # increment this counter in order to know number of blank rows to add to top of newGRID
        fullRowsCount += 1

# if there are any full rows then they're not included in newGRID so blank rows need adding to the top
if fullRowsCount:
    newGRID = [[0] * GRID_SIZE.width] * fullRowsCount + newGRID
    # update GRID to the newGRID
    GRID = newGRID

感谢您的宝贵时间:)

声明

newGRID = [[0] * GRID_SIZE.width] * fullRowsCount + newGRID

没有达到您的预期。它创建 1 列,由网格中的所有行共享。

注意,下面代码的输出

newGRID = [[0] * 3] * 3
newGRID[0][0] = 1
print(newGRID)

[[1, 0, 0], [1, 0, 0], [1, 0, 0]]

您必须为每一行创建一个新列:

newGRID = [[0] * GRID_SIZE.width for _ in range(fullRowsCount + newGRID)]