Skip to main content
Engineering LibreTexts

10.17: Drawing the Map

  • Page ID
    14678
    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    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[0]) - 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
    

    The 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 [543] and 10 [544]). The Surface object that everything will be drawn on is created on line 11 [545]. To begin with, the entire Surface object is painted to the background color on line 12 [546].

    The set of nested for loops on line 15 [549] and 16 [550] will go through every possible XY coordinate on the map and draw the appropriate tile image at that location.

    The 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 [568]). 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 [569], we know that the elif statement’s condition on line 29 [563] was False and there is no star that is also at this XY coordinate.

    Finally, the 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 [580] is outside of the nested for loops that began on line 15 [549] and 16 [550], so by the time the Surface object is returned, the entire map has been drawn on it.


    This page titled 10.17: Drawing the Map is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Al Sweigart via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.