I'm writing a Legend of Zelda like game in pygame.
I have this code for loading a map from a text file.
#Load maps
f = open ("map1","r")#This file is a text file containing only: 1
map01 = f.read()
terrains = [{"sprite": dirt}, {"sprite": mountian}, {"sprite": water}]
themap = [[-1 for x in range(150)] for y in range (150)]
for x in range (0,150):
for y in range (0,150):
themap[x][y] = terrains[map01]#if I replace map01 here with a number or random.randint(0,2) it works as expected
for x in range (0, 150):
for y in range (0, 150):
screen.blit(themap[x][y]["sprite"], [x * 16, y * 16])
But that gives me:
$ python map.py
Traceback (most recent call last):
File "map.py", line 17, in <module>
themap[x][y] = terrains[map01]
TypeError: list indices must be integers, not str
Ok I'm making this a bit clearer I want to make a file called map1 that looks like this
1, 2, 1, 0 etc.
I can make a random screen with random.randint:
and I can make it a solid texture:
but I can't figure out how to predefine a map.