【问题标题】:Duplicate rows in Python Pygame Tetris clonePython Pygame俄罗斯方块克隆中的重复行
【发布时间】:2021-04-25 22:53:17
【问题描述】:

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

我正在使用 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

感谢您的宝贵时间:)

【问题讨论】:

  • 基本上有一个名为 GRID 的二维列表,其中包含堆栈数据。通过堆栈,我的意思是一旦形状停止,它的数据就会添加到 GRID 中,因此可以进行基本的碰撞检测。即使使用了 newGRID.insert(0, row[:]) ,即使没有与原始文件纠缠在一起,那是否仍会创建一个克隆?
  • 这是完整代码pastebin.com/W1xXjMCU。上面的代码从第 362 行开始

标签: python pygame tetris


【解决方案1】:

声明

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)]

【讨论】:

    猜你喜欢
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多