python list 索引超出范围错误
我正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一种方法中存在一个拼写错误,会导致该问题。
当您检查右移块中的每个单元格时,您不会检查它们是否脱离网格。
可能应该是
我也同意CrazyDrummer的观点,制作一个通用的清晰功能
剧透......
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.
probably should be
I also agree with CrazyDrummer, make a generic clear function
Spoilers ...
看看当你遇到异常时有什么不同。尝试打印出程序状态信息来帮助您归零。只有一个位置可以访问具有可变索引的数组,因此您可以稍微缩小搜索半径。
单独的建议:制作一个通用的
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