我正在创建一个简单的角色扮演游戏作为一种学习体验。在我的代码中,我有一个在 25x25 网格上显示得很好的图 block 数组,以及一个单独的数组,其中包含与图 block 是否实体相关的 True/False 值。后者不起作用;在我下面的代码中,我在它没有到达的地方准确地放置了一个打印语句,我不太确定问题出在哪里。
此外,该关卡的数据只是一个文本文件,带有代表方 block 的 25x25 字符网格。
def loadLevel(self, level):
fyle = open("levels/" + level,'r')
count = 0
for lyne in fyle:
if lyne.startswith("|"):
dirs = lyne.split('|')
self.north = dirs[1]
self.south = dirs[2]
self.east = dirs[3]
self.west = dirs[4]
continue
for t in range(25):
tempTile = Tiles.Tile()
tempTile.value = lyne[t]
tempTile.x = t
tempTile.y = count
self.levelData.append(tempTile)
count += 1
rowcount = 0
colcount = 0
for rows in fyle:
print('Doesnt get here!')
for col in rows:
if col == 2:
self.collisionLayer[rowcount][colcount] = False
else:
self.collisionLayer[rowcount][colcount] = True
colcount += 1
print(self.collisionLayer[rowcount[colcount]])
if rows == 2:
self.collisionLayer[rowcount][colcount] = False
else:
self.collisionLayer[rowcount][colcount] = True
rowcount += 1
print(self.collisionLayer)
具体问题出在哪里?我觉得这是一个快速修复,但我根本看不到它。谢谢!
请您参考如下方法:
您使用第一个 for 循环读取了文件一次,因此没有任何内容可供第二个循环读取。在开始第二个循环之前回到文件的开头:
fyle.seek(0)
虽然我只是将这些行缓存为一个列表,如果可能的话:
with open('filename.txt', 'r') as handle:
lines = list(handle)
此外,您可以替换它:
if rows == 2:
self.collisionLayer[rowcount][colcount] = False
else:
self.collisionLayer[rowcount][colcount] = True
与:
self.collisionLayer[rowcount][colcount] = rows != 2
