I am making a game in pygame using a tilemap, and I've encountered the follwing error
"Traceback (most recent call last):
File "C:\Users\Dusty\Dropbox\~MAIN FOLDER~\Projects\Sunless\game.py", line 52, in <module>
surface.blit(textures[tileMap[row][column]], (column*tileSize, row*tileSize, tileSize, tileSize))
KeyError: 174"
With this code:
import pygame
gameIsRunning = True
tileSize = 32
mapHeight = 19
mapWidth = 25
surface = pygame.display.set_mode((tileSize * mapWidth, tileSize * mapHeight))
pygame.display.set_caption("Game")
floor = 109
wall = 166
textures = {
floor : pygame.image.load('data/floor.png'),
wall: pygame.image.load('data/wall.png'),
}
tileMap = [
[109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109],
[109, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 109, 166, 166, 166, 166, 166, 166, 166, 166, 109],
[109, 166, 109, 109, 109, 109, 109, 109, 109, 166, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 166, 109, 109, 109],
[109, 166, 109, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 109, 166, 109],
[109, 166, 109, 166, 109, 109, 109, 109, 109, 166, 109, 109, 109, 109, 109, 109, 166, 109, 166, 109, 109, 166, 109, 166, 109],
[109, 166, 109, 166, 109, 166, 166, 166, 109, 166, 166, 166, 166, 166, 166, 109, 166, 109, 166, 166, 166, 166, 166, 166, 109],
[109, 166, 166, 166, 166, 166, 109, 166, 166, 166, 109, 109, 166, 109, 166, 109, 166, 109, 166, 109, 166, 166, 109, 166, 109],
[109, 166, 109, 166, 109, 166, 109, 109, 109, 166, 166, 166, 166, 109, 166, 109, 166, 166, 166, 166, 109, 166, 109, 166, 109],
[109, 166, 109, 166, 109, 166, 166, 166, 166, 166, 109, 109, 166, 109, 166, 166, 166, 109, 166, 109, 166, 166, 109, 166, 109],
[109, 166, 109, 166, 109, 166, 109, 166, 109, 166, 109, 109, 166, 109, 166, 109, 166, 109, 166, 166, 109, 166, 109, 166, 109],
[109, 166, 109, 166, 109, 166, 166, 166, 109, 166, 166, 166, 166, 166, 166, 109, 166, 109, 166, 109, 166, 166, 109, 166, 109],
[109, 166, 109, 166, 109, 109, 109, 166, 109, 166, 109, 166, 109, 109, 166, 109, 166, 109, 166, 166, 109, 166, 166, 166, 109],
[109, 109, 109, 166, 166, 166, 166, 166, 166, 166, 109, 166, 109, 109, 166, 109, 166, 109, 166, 109, 109, 166, 109, 109, 109],
[109, 166, 109, 166, 109, 109, 166, 109, 109, 166, 109, 166, 109, 166, 166, 109, 166, 109, 166, 109, 109, 166, 109, 166, 109],
[109, 166, 109, 166, 109, 109, 166, 109, 109, 166, 109, 166, 166, 166, 109, 109, 166, 109, 166, 109, 109, 166, 109, 166, 109],
[109, 166, 109, 166, 166, 166, 166, 166, 109, 166, 109, 166, 166, 109, 109, 166, 166, 109, 166, 109, 109, 166, 109, 166, 109],
[109, 166, 109, 109, 109, 109, 109, 166, 109, 166, 166, 166, 166, 166, 166, 166, 166, 109, 166, 109, 109, 174, 166, 166, 109],
[109, 166, 166, 166, 166, 166, 166, 166, 166, 166, 109, 109, 109, 109, 166, 166, 166, 109, 166, 166, 166, 166, 109, 166, 109],
[109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109]
]
def gameQuit():
pygame.quit()
quit()
while gameIsRunning == True:
surface.fill((0, 0, 0))
for row in range(mapHeight):
for column in range(mapWidth):
surface.blit(textures[tileMap[row][column]], (column*tileSize, row*tileSize, tileSize, tileSize))
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameQuit()
Small tip, if you want to make a game using tilemap use Tiled, also use TMX import library by Richard Jones.
Also it appears you are trying to access element that isn't in textures. You have value 174 in
tileMap
(3rd row from end).Right here you access element that doesn't exist in textures dictionary. It's interpretted as:
And since there is no such key in textures you have the error.