So, I was making a simple 2D game in pygame, and was making the map (following a tutorial video, don’t judge me, I’m new to pygame) and using replit.com, and that error showed up in the console.
here’s my code for it:
import pygame, sys W = 0 S = 1 G = 2 F = 3 BLUE = (0,0,255) SAND = (194,176,128) GRASS = (124,252,0) FOREST = (0,100,0) TileColor = {W : BLUE, S : SAND, G : GRASS, F : FOREST } map1 = {(W, S, G, F, F, F, G, S, W, W, W, W), [W, S, S, G, G, G, G, F, F, W, W, W], [W, S, G, G, G, F, F, F, F, F, W, W], [W, S, S, G, G, G, G, G, F, F, F, W] } TILESIZE = 40 MAPWIDTH = 12 MAPHEIGHT = 4 pygame.init() DISPLAY = pygame.display.set_mode((MAPWIDTH * TILESIZE,MAPHEIGHT * TILESIZE)) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() for row in range(MAPHEIGHT): for col in range(MAPWIDTH): pygame.draw.rect(DISPLAY,TileColor[map1[row][col]],(col * TILESIZE,row * TILESIZE,TILESIZE,TILESIZE)) pygame.display.update
if anyone has any suggestions for changing my code, please say, and I am sticking with replit.com
Advertisement
Answer
You’re pretty much correct already. I’m just guessing at how your code is supposed to work, but using a python dictionary for the tile colours with integer-variables as keys is what’s causing the issue.
If you change your tile keys to a letter:
TileColor = { 'W' : BLUE, 'S' : SAND, 'G' : GRASS, 'F' : FOREST }
And then in your map, use letters too:
map1 = [ "WSGFFFGSWWWW", "WSSGGGGFFWWW", "WSGGGFFFFFWW", "WSSGGGGGFFFW" ]
It works pretty much perfectly:
Full code:
import pygame, sys BLUE = (0,0,255) SAND = (194,176,128) GRASS = (124,252,0) FOREST = (0,100,0) TileColor = {'W' : BLUE, 'S' : SAND, 'G' : GRASS, 'F' : FOREST } map1 = [ "WSGFFFGSWWWW", "WSSGGGGFFWWW", "WSGGGFFFFFWW", "WSSGGGGGFFFW" ] TILESIZE = 40 MAPWIDTH = 12 MAPHEIGHT = 4 pygame.init() DISPLAY = pygame.display.set_mode((MAPWIDTH * TILESIZE,MAPHEIGHT * TILESIZE)) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() for row in range(MAPHEIGHT): for col in range(MAPWIDTH): pygame.draw.rect(DISPLAY,TileColor[map1[row][col]],(col * TILESIZE,row * TILESIZE,TILESIZE,TILESIZE)) pygame.display.update()