def drawMap(mapObj, gameStateObj, goals): """Draws the map to a Surface object, including the player and stars. This function does not call pygame.display.update(), nor does it draw the "Level" and "Steps" text in the corner.""" # mapSurf will be the single Surface object that the tiles are drawn # on, so that it is easy to position the entire map on the DISPLAYSURF # Surface object. First, the width and height must be calculated. mapSurfWidth = len(mapObj) * TILEWIDTH mapSurfHeight = (len(mapObj) - 1) * TILEFLOORHEIGHT + TILEHEIGHT mapSurf = pygame.Surface((mapSurfWidth, mapSurfHeight)) mapSurf.fill(BGCOLOR) # start with a blank color on the surface. # Draw the tile sprites onto this surface. for x in range(len(mapObj)): for y in range(len(mapObj[x])): spaceRect = pygame.Rect((x * TILEWIDTH, y * TILEFLOORHEIGHT, TILEWIDTH, TILEHEIGHT)) if mapObj[x][y] in TILEMAPPING: baseTile = TILEMAPPING[mapObj[x][y]] elif mapObj[x][y] in OUTSIDEDECOMAPPING: baseTile = TILEMAPPING[' '] # First draw the base ground/wall tile. mapSurf.blit(baseTile, spaceRect) if mapObj[x][y] in OUTSIDEDECOMAPPING: # Draw any tree/rock decorations that are on this tile. mapSurf.blit(OUTSIDEDECOMAPPING[mapObj[x][y]], spaceRect) elif (x, y) in gameStateObj['stars']: if (x, y) in goals: # A goal AND star are on this space, draw goal first. mapSurf.blit(IMAGESDICT['covered goal'], spaceRect) # Then draw the star sprite. mapSurf.blit(IMAGESDICT['star'], spaceRect) elif (x, y) in goals: # Draw a goal without a star on it. mapSurf.blit(IMAGESDICT['uncovered goal'], spaceRect) # Last draw the player on the board. if (x, y) == gameStateObj['player']: # Note: The value "currentImage" refers # to a key in "PLAYERIMAGES" which has the # specific player image we want to show. mapSurf.blit(PLAYERIMAGES[currentImage], spaceRect) return mapSurf
drawMap() function will return a Surface object with the entire map (and the player and stars) drawn on it. The width and height needed for this Surface have to be calculated from
mapObj (which is done on line 9  and 10 ). The Surface object that everything will be drawn on is created on line 11 . To begin with, the entire Surface object is painted to the background color on line 12 .
The set of nested
for loops on line 15  and 16  will go through every possible XY coordinate on the map and draw the appropriate tile image at that location.
baseTile variable is set to the Surface object of the tile image to be drawn at the iteration’s current XY coordinate. If the single-character string is in the
OUTSIDEDECOMAPPING dictionary, then
TILEMAPPING[' '] (the single-character string for the basic outdoor floor tile) will be used.
Additionally, if the tile was listed in the
OUTSIDEDECOMAPPING dictionary, the corresponding tree or rock image should be drawn on top of the tile that was just drawn at that XY coordinate.
If there is a star located at this XY coordinate on the map (which can be found out by checking for
(x, y) in the list at
gameStateObj['stars']), then a star should be drawn at this XY coordinate (which is done on line 34 ). Before the star is drawn, the code should first check if there is also a goal at this location, in which case, the "covered goal" tile should be drawn first.
If there is a goal at this XY coordinate on the map, then the "uncovered goal" should be drawn on top of the tile. The uncovered goal is drawn because if execution has reached the
elif statement on line 35 , we know that the
elif statement’s condition on line 29  was
False and there is no star that is also at this XY coordinate.
drawMap() function checks if the player is located at this XY coordinate, and if so, the player’s image is drawn over the tile. Line 46  is outside of the nested for loops that began on line 15  and 16 , so by the time the Surface object is returned, the entire map has been drawn on it.