我正在研究基于web2py生活游戏的生活模拟游戏。我有两个对象,其中包含几个数组,用于网格中每个空间中有机体的各种属性。我收到此错误LifeSpace实例在包含此代码的行上没有属性“ getitem ”nxt [i] [j] = processNeighborsEmpty(i,j,cur)
我是否正确引用了对象实例?
ROWS = 60
COLS = 60
GENERATIONS = 100
DELAY = 0.5
#---------------------------------------------------------------------------
class LifeSpace :
def __init__(self):
self.genus = []
self.species = []
self.tribe = []
self.hunger = []
self.wounds = []
self.age = []
self.genus = initGrid (COLS, ROWS, self.genus)
self.species = initGrid (COLS, ROWS, self.species)
self.tribe = initGrid (COLS, ROWS, self.tribe)
self.hunger = initGrid(COLS, ROWS, self.hunger)
self.wounds = initGrid(COLS, ROWS, self.wounds)
self.age = initGrid(COLS, ROWS, self.age)
#---------------------------------------------------------------------------
def initGrid(cols, rows, array):
for i in range(rows):
arrayRow = []
for j in range(cols):
arrayRow+= [0]
array += [arrayRow]
return array
#---------------------------------------------------------------------------
def printGen(cols, rows, array, genNo):
return("Game of Life -- Generation " + str(genNo + 1))
for i in range(rows):
for j in range(cols):
if array.species[i][j] == 0:
return(" ")
elif array.species[i][j] == 'weed':
return("3")
return("\n")
#---------------------------------------------------------------------------
def processNeighborsEmpty(x, y, array):
for j in range(y-1,y+1):
for i in range(x-1,x+1):
if not(i == x and j == y):
if array.genus[i][j] == "plant" :
if array.age[i][j] == "adult" :
array.species[x][y] = array.species[i][j]
array.genus[x][y] = array.genus[i][j]
array.age[x][y] = "seed"
return array[x][y]
#---------------------------------------------------------------------------
def processNeighborsPlant(x, y, array):
if array.age[x][y] != "adult" :
if array.species[x][y] == "tree" :
if array.age[x][y] == "sapling" :
array.age[x][y] = "adult"
elif array.age[x][y] == "seed" :
array.age[x][y] = "sapling"
elif array.age[x][y] == "seed" :
array.age[x][y] = "adult"
#---------------------------------------------------------------------------
def processNextGen(cols, rows, cur, nxt):
for i in range(0,rows):
for j in range(0,cols):
if cur.genus[i][j] == 0 :
nxt[i][j] = processNeighborsEmpty (i, j, cur)
elif cur.genus[i][j] == "plant" :
nxt[i][j] = processNeighborsPlant(i, j, cur)
#---------------------------------------------------------------------------
############################################################################
#---------------------------------------------------------------------------
def index():
thisGen = LifeSpace()
nextGen = LifeSpace()
thisGen.genus[25][25] = "plant"
thisGen.species[25][25] = "weed"
thisGen.age[25][25] = "adult"
for gens in range(GENERATIONS):
printGen(COLS, ROWS, thisGen, gens)
processNextGen(COLS, ROWS, thisGen, nextGen)
time.sleep(DELAY)
thisGen, nextGen = nextGen, thisGen
input("Finished. Press <return> to quit.")
答案 0 :(得分:0)
nxt
是LifeSpace对象,因此您无法使用[]
表示法引用它。
您需要使用具有正确属性的nxt.attribute[x][y]
。也许nxt.genux[x][y]
是您想要使用的。