【发布时间】:2017-01-26 18:44:51
【问题描述】:
我只是想将一堆块存储在一堆块中。这是一个非常简单的体素世界。目前测试代码中有三个类级别(我打算玩一下pickle模块和序列化):世界、世界中的块和块中的块。
这是踪迹:
Traceback (most recent call last): File "C:/Crayder/Scripts/pickle
test/pickle1.py", line 27, in <module> aWorld = world(); File
"C:/Crayder/Scripts/pickle test/pickle1.py", line 25, in __init__
self.chunks[cX][cY] = chunk(cX, cY); File "C:/Crayder/Scripts/pickle
test/pickle1.py", line 18, in __init__ self.blocks[bX][bY][bZ] =
block((self.x * 16) + bX, (self.y * 16) + bY, bZ); IndexError: list
index out of range
这里是代码:
class block:
def __init__(self, x, y, z, data = 0):
self.x = x;
self.y = y;
self.z = z;
self.data = data;
class chunk:
def __init__(self, x, y):
self.x = x;
self.y = y;
self.blocks = [];
for bX in range(16):
for bY in range(16):
for bZ in range(64):
self.blocks[bX][bY][bZ] = block((self.x * 16) + bX, (self.y * 16) + bY, bZ);
class world:
def __init__(self):
self.chunks = [];
for cX in range(16):
for cY in range(16):
self.chunks[cX][cY] = chunk(cX, cY);
aWorld = world();
print(aWorld.chunks[2][2].blocks[2][2][2]);
我在这里做错了什么?
【问题讨论】:
-
您使用的是
list,而不是数组。
标签: python class multidimensional-array pickle indexoutofrangeexception