python list 索引超出范围错误

发布于 2024-08-30 15:21:47 字数 986 浏览 3 评论 0原文

我正在开发一个 python 俄罗斯方块游戏,我的教授为编程课程概念的最终项目分配了该游戏。此时我已经得到了他想要做的几乎所有内容,但我在其中一部分遇到了一些小问题。每当我开始左右移动棋子时,我都会收到“索引超出范围错误”。只有当它对抗一块棋子时才会发生这种情况。这是让我悲伤的罪魁祸首。

def clearRight(block=None):
    global board, activeBlock, stackedBlocks
    isClear = True
    if(block == None):
        block = activeBlock
    if(block != None):

        for square in block['squares']:
            row = square[1]
            col = square[0]+1
            if(col >= 0 and stackedBlocks[row][col] !=None):
                isClear=False
    return isClear


def clearLeft(block=None):
    global board, activeBlock, stackedBlocks
    isClear = True
    if(block == None):
        block = activeBlock
    if(block != None):

        for square in block['squares']:
            row = square[1]
            col = square[0]-1
            if(col >= 0 and stackedBlocks[row][col] !=None):
                isClear=False
    return isClear

我不想让任何人帮我修复它,我只是在寻找如何自己修复它的提示。预先感谢您提供的任何帮助。

I am working on a python tetris game that my proffessor assigned for the final project of a concepts of programming class. I have got just about everything he wanted to work on it at this point but I am having a slight problem with one part of it. Whenever I start moving pieces left and right I keep getting "index out of range error". This only happens when it is up against a piece. Here are the culprits that are giving me grief.

def clearRight(block=None):
    global board, activeBlock, stackedBlocks
    isClear = True
    if(block == None):
        block = activeBlock
    if(block != None):

        for square in block['squares']:
            row = square[1]
            col = square[0]+1
            if(col >= 0 and stackedBlocks[row][col] !=None):
                isClear=False
    return isClear


def clearLeft(block=None):
    global board, activeBlock, stackedBlocks
    isClear = True
    if(block == None):
        block = activeBlock
    if(block != None):

        for square in block['squares']:
            row = square[1]
            col = square[0]-1
            if(col >= 0 and stackedBlocks[row][col] !=None):
                isClear=False
    return isClear

I am not looking to get anyone to fix it for me, I'm only looking for tips on how to fix it myself. Thanks in advance for any help that is given.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野味少女 2024-09-06 15:21:47

第一种方法中存在一个拼写错误,会导致该问题。

当您检查右移块中的每个单元格时,您不会检查它们是否脱离网格。

if (col >= 0 and ...)

可能应该是

if (col < num_cols and ...)

我也同意CrazyDrummer的观点,制作一个通用的清晰功能


剧透......

def clear(x_offset, block=None):
    if not block: 
        block = activeBlock
        if not block: return True
    for x,y in block:
        x += x_offset
        if not (0 <= x < num_cols) or stackedBlocks[x, y]:
            return False
    return True

There a typo that would cause that problem in the first method.

When you're checking each cell in the block shifted one right, you don't check if they are off the grid.

if (col >= 0 and ...)

probably should be

if (col < num_cols and ...)

I also agree with CrazyDrummer, make a generic clear function


Spoilers ...

def clear(x_offset, block=None):
    if not block: 
        block = activeBlock
        if not block: return True
    for x,y in block:
        x += x_offset
        if not (0 <= x < num_cols) or stackedBlocks[x, y]:
            return False
    return True
背叛残局 2024-09-06 15:21:47

看看当你遇到异常时有什么不同。尝试打印出程序状态信息来帮助您归零。只有一个位置可以访问具有可变索引的数组,因此您可以稍微缩小搜索半径。

单独的建议:制作一个通用的 clear 来确定您想要通过参数清除的方向。

我强烈推荐调试规则!这本书,它将帮助您查找并正确解决问题。 :D

Look at what's different when you're getting the exception. Try printing out program state information to help you zero in. There's only one place where you access an array with variable indexes, so you can narrow your search radius a bit.

Separate suggestion: Make a generic clear that takes determines what direction you want to clear from by the parameters.

I highly recommend the book debugging rules!, it will aid you in searching out and properly fixing problems. :D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文