我正在尝试将不同的纹理应用于块,具体取决于它周围的块,但是我在执行代码时遇到“列表索引超出范围”错误。
我一直在寻找并试图解决这个问题几个小时,我想我做得不对,但我无法找到正确的方法。
level = [
# 1 1 1 1 1 1 1 1 1 1 2 2 2
# 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2
[ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],#0
[ 0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,1,1,1,0,0,0],#1
[ 0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,0,1,1,1,0,0,0],#2
[ 0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,1,0,0,0],#3
[ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],#4
[ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],#5
[ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],#6
[ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],#7
[ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],#8
]
for i in range(9):
for j in range(23):
if level[i][j] == 1:
# LEFT TOP RIGHT BOTTOM
if level[i][j-1] == 1 and level[i-1][j] == 1 and level[i][j+1] == 1 and level[i+1][j] == 1:
block = Block( j*48, i*48, 48, 48, grey, "constructor_.png")
if level[i][j-1] == 1 and level[i][j+1] != 1 and level[i+1][j] == 1 and level[i-1][j] != 1:
block = Block( j*48, i*48, 48, 48, grey, "constructor_t.png")
if level[i][j-1] == 1 and level[i-1][j] == 1 and level[i][j+1] == 1 and level[i+1][j] != 1:
block = Block( j*48, i*48, 48, 48, grey, "constructor_b.png")
if level[i][j-1] != 1 and level[i-1][j] == 1 and level[i][j+1] == 1 and level[i+1][j] == 1:
block = Block( j*48, i*48, 48, 48, grey, "constructor_l.png")
if level[i][j-1] == 1 and level[i-1][j] == 1 and level[i][j+1] != 1 and level[i+1][j] == 1:
block = Block( j*48, i*48, 48, 48, grey, "constructor_r.png")
if level[i][j-1] != 1 and level[i-1][j] != 1 and level[i][j+1] == 1 and level[i+1][j] == 1:
block = Block( j*48, i*48, 48, 48, grey, "constructor_tl.png")
if level[i][j-1] == 1 and level[i-1][j] != 1 and level[i][j+1] != 1 and level[i+1][j] == 1:
block = Block( j*48, i*48, 48, 48, grey, "constructor_tr.png")
if level[i][j-1] != 1 and level[i-1][j] == 1 and level[i][j+1] == 1 and level[i+1][j] != 1:
block = Block( j*48, i*48, 48, 48, grey, "constructor_bl.png")
if level[i][j-1] == 1 and level[i-1][j] == 1 and level[i][j+1] != 1 and level[i+1][j] != 1:
block = Block( j*48, i*48, 48, 48, grey, "constructor_br.png")
if level[i][j-1] != 1 and level[i-1][j] != 1 and level[i][j+1] == 1 and level[i+1][j] != 1:
block = Block( j*48, i*48, 48, 48, grey, "constructor_tbl.png")
if level[i][j-1] == 1 and level[i-1][j] != 1 and level[i][j+1] != 1 and level[i+1][j] != 1:
block = Block( j*48, i*48, 48, 48, grey, "constructor_tbr.png")
if level[i][j-1] != 1 and level[i-1][j] != 1 and level[i][j+1] != 1 and level[i+1][j] == 1:
block = Block( j*48, i*48, 48, 48, grey, "constructor_tlr.png")
if level[i][j-1] != 1 and level[i-1][j] == 1 and level[i][j+1] != 1 and level[i+1][j] != 1:
block = Block( j*48, i*48, 48, 48, grey, "constructor_blr.png")
if level[i][j-1] == 1 and level[i-1][j] != 1 and level[i][j+1] == 1 and level[i+1][j] != 1:
block = Block( j*48, i*48, 48, 48, grey, "constructor_tb.png")
if level[i][j-1] != 1 and level[i-1][j] == 1 and level[i][j+1] != 1 and level[i+1][j] == 1:
block = Block( j*48, i*48, 48, 48, grey, "constructor_lr.png")
else:
block = Block( j*48, i*48, 48, 48, grey, "constructor_tblr.png")
self.object_list_c.add(block)
我期待着你的帮助。谢谢^。^
答案 0 :(得分:0)
您的level
矩阵有9行和23列。当你有像
if level[i][j-1] == 1 and level[i-1][j] == 1 and level[i][j+1] == 1 and level[i+1][j] == 1:
在j
= 22或i
= 9的情况下,您尝试访问level[i][j+1]
不存在的索引,回想一下Python中的列表索引从0开始。我不清楚你的代码在做什么,但一个明显的补救措施是减少你的range
,这样你就不会尝试在最后的迭代中访问不存在的索引。