Reputation: 25
I know questions on subjects like this have been posted before but I have an error I have not seen in any other questions.
I am trying to make a tiled background from a 2d array, but when I run it I get this error:
current_tile = textures[tilemap[x,y]]
TypeError: list indices must be integers, not tuple
This is my code:
import pygame, sys
from pygame.locals import *
pygame.init()
tilesize = 32
tileswidth = 11
tilesheight = 12
screenwidth = tilesize*tileswidth
screenheight = tilesize*tilesheight
screen = pygame.display.set_mode((screenwidth,screenheight))
tilemap = [
[9,3,2,2,2,4,2,2,2,3,9],
[8,11,10,10,10,10,10,10,10,11,5],
[8,1,6,9,9,9,9,9,8,1,5],
[8,1,5,9,9,9,9,9,7,1,5],
[8,1,6,9,9,9,9,9,7,1,5],
[8,1,5,9,9,9,9,9,8,1,5],
[8,1,12,4,2,3,2,4,13,1,5],
[7,11,10,10,10,11,10,10,10,11,6],
[9,9,9,9,7,1,6,9,9,9,9],
[9,9,9,9,7,1,6,9,9,9,9],
[9,9,9,9,7,1,6,9,9,9,9]
]
textures = {
1 : pygame.image.load("data/tiles/1.jpg").convert_alpha(),
2 : pygame.image.load("data/tiles/2.jpg").convert_alpha(),
3 : pygame.image.load("data/tiles/3.jpg").convert_alpha(),
4 : pygame.image.load("data/tiles/4.jpg").convert_alpha(),
5 : pygame.image.load("data/tiles/5.jpg").convert_alpha(),
6 : pygame.image.load("data/tiles/6.jpg").convert_alpha(),
7 : pygame.image.load("data/tiles/7.jpg").convert_alpha(),
8 : pygame.image.load("data/tiles/8.jpg").convert_alpha(),
9 : pygame.image.load("data/tiles/9.jpg").convert_alpha(),
10 : pygame.image.load("data/tiles/10.jpg").convert_alpha(),
11 : pygame.image.load("data/tiles/11.jpg").convert_alpha(),
12 : pygame.image.load("data/tiles/12.jpg").convert_alpha(),
13 : pygame.image.load("data/tiles/13.jpg").convert_alpha()
}
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
for x in range(tilesheight):
for y in range(tileswidth):
current_tile = textures[tilemap[x,y]]
screen.blit(current_tile, (x*tilesize, y*tilesize))
screen.fill(0,0,0)
pygame.display.update()
Any help would be appreciated.
Upvotes: 1
Views: 120
Reputation: 180401
You cannot access a list with a tuple which is what tilemap[x,y]
is doing, you want:
tilemap[x][y]
Which is taking the element at index x from sublist at index y:
In [7]: tilemap = [
...: [9,3,2,2,2,4,2,2,2,3,9],
...: [8,11,10,10,10,10,10,10,10,11,5],
...: [8,1,6,9,9,9,9,9,8,1,5],
...: [8,1,5,9,9,9,9,9,7,1,5],
...: [8,1,6,9,9,9,9,9,7,1,5],
...: [8,1,5,9,9,9,9,9,8,1,5],
...: [8,1,12,4,2,3,2,4,13,1,5],
...: [7,11,10,10,10,11,10,10,10,11,6],
...: [9,9,9,9,7,1,6,9,9,9,9],
...: [9,9,9,9,7,1,6,9,9,9,9],
...: [9,9,9,9,7,1,6,9,9,9,9]
...: ]
In [8]: tilemap[0][0]
Out[8]: 9
In [9]: tilemap[1][0]
Out[9]: 8
You can also just iterate over the tilemap list:
for ind, sub in enumerate(tilemap):
for i, y in enumerate(sub):
current_tile = textures[y]
screen.blit(current_tile, (ind * tilesize, i * tilesize))
Upvotes: 0
Reputation:
TypeError: list indices must be integers, not tuple
To fix this error, you have to use integers (numbers) and not tuple (1,2) as list indices. From what I see, you are trying to do 2-dimensional list[1,2] which is wrong because 2 dimensional arrays/lists are lists of lists. That is why you have to use tilemap[x][y]. Tilemap[x] returns sublist from the tilemap and tilemap[x][y] is from sublist at x, return element y.
Upvotes: 1