python - Python Pygame Tetris 克隆中的重复行

标签 python pygame tetris

我已经研究这个问题一段时间了,并且已经画了一个空白,所以希望得到一些帮助。
我正在使用 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)]

关于python - Python Pygame Tetris 克隆中的重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67254552/

相关文章:

Python os.path.join() - 无法正确组合

python - 使用 Pygame, Python 3 Blitting 非矩形 Sprite

python - 为什么我的简单 pygame 会滞后?

python - a*star 寻路可视化非常慢/python

java - java中的旋转字符串数组

python - 除周末 Python 之外的日期时间序列

python - 如何在 virtualenv 中配置 pip 以将软件包安装到当前目录根目录?

javascript - 使用 django 过滤器和带有 Angular 表达式的标签

c - c 中 4x4 矩阵的 getter 问题

c++ - 俄罗斯方 block 旋转 C++