samworld.py
def draw_tiles(screen, tile_buffer, last_position, position, level, tile_graphics, ground_tile, sky_tile, tile_size):
    """draws the section of level tiles currently visible to the player onto the screen surface.
       screen is the screen surface to draw onto
       tile_buffer is a screen-sized surface onto which the tiles are drawn before being drawn to the screen 
       surface. The buffer should contain the drawn tiles from the previous frame so that they can be reused if
       possible       
       last_position is the x,y position of the player in the level from the previous frame, as a 2-item sequence
       positon is the x,y position of the player in the level this frame, as a 2-item sequence
       level is the nested list of tile types representing the level
       tile_graphics is the list of graphics representing the available tile types
       ground_tile is the tile graphic used to draw tiles below the bottom of the level
       sky_tile is the tile graphic used to draw tiles above, to the left of, and to the right of the level
       tile_size is the size of each tile as an int"""
       
    # truncate the positon and last_position coordinates from floats to integers
    position = list(map(int, position))
    last_position = list(map(int, last_position))
    
    # calculate the x and y amount the camera moved since the last frame
    scrolled_amount = [position[0]-last_position[0], position[1]-last_position[1]]
    
    # tile_buffer is a surface containing the drawn tiles from the previous frame. We can save drawing all the tiles
    # again this frame by simply shifting the previous drawing by the amount the camera moved. The "scroll" method does
    # exactly that.
    tile_buffer.scroll(-int(scrolled_amount[0]), -int(scrolled_amount[1]))
    
    # calculate the x,y coordinates within the level that the top left corner of the viewport is at
    top_left_position = position[0]-screen.get_width()/2, position[1]-screen.get_height()/2
    
    # calculate the column and row of the tile the that this top left position sits within
    top_left_tile = tile_ref(top_left_position, tile_size)
    
    # calculate where on the screen (or just off it) this top-left tile would be drawn
    top_left_draw_pos = -(top_left_position[0] % tile_size), -(top_left_position[1] % tile_size)
    
    # calculate how many rows and colums of tiles must be drawn to cover the screen
    rows_to_draw = int(float(screen.get_height()) / tile_size + 1)
    cols_to_draw = int(float(screen.get_width()) / tile_size + 1)
    
    # calculate the bounds of the screen region we don't need to re-draw the tiles for
    buffered_top_left = [0-scrolled_amount[0], 0-scrolled_amount[1]]
    buffered_bottom_right = [buffered_top_left[0]+screen.get_width(), buffered_top_left[1]+screen.get_height()]
    
    # if this region doesnt start at the left edge, clear the tile buffer's left edge
    if buffered_top_left[0] > 0:
        tile_buffer.fill((0,0,0,0),(0,0,buffered_top_left[0]-0,screen.get_height()))
        
    # if the region doesnt start at the top edge, clear the tile buffer's top edge
    if buffered_top_left[1] > 0:
        tile_buffer.fill((0,0,0,0),(0,0,screen.get_width(),buffered_top_left[1]-0))
        
    # if the region doesnt end at the right edge, clear the tile buffer's right edge
    if buffered_bottom_right[0] < screen.get_width():
        tile_buffer.fill((0,0,0,0),(buffered_bottom_right[0],0,
                                    screen.get_width()-buffered_bottom_right[0], screen.get_height()))
                                    
    # if the region doesnt end at the bottom edge, clear the tile buffer's bottom edge 
    if buffered_bottom_right[1] < screen.get_height():
        tile_buffer.fill((0,0,0,0),(0,buffered_bottom_right[1],
                                    screen.get_width(), screen.get_height()-buffered_bottom_right[1]))
                                    
    # iterate over the rows and columns to draw
    for j in range(rows_to_draw):
        for i in range(cols_to_draw):
        
            # calculate the column and row of the next tile to draw
            tile = top_left_tile[0]+i, top_left_tile[1]+j
            
            # find that tile's type
            type = tile_type(tile, level, ground_tile, sky_tile)
            
            # look up the tile's graphic
            tile_graphic = tile_graphics[type]
            
            # if the tile graphic isn't None (i.e. not an invisible tile)
            if tile_graphic is not None:
            
                # calculate where to draw the tile
                draw_pos = top_left_draw_pos[0]+i*tile_size, top_left_draw_pos[1]+j*tile_size
                
                # if the tile to draw isn't completely within the already-drawn region, draw it to the buffer
                if max(draw_pos[0],0) < buffered_top_left[0] \
                        or min(draw_pos[0]+tile_size,screen.get_width()) > buffered_bottom_right[0] \
                        or max(draw_pos[1],0) < buffered_top_left[1] \
                        or min(draw_pos[1]+tile_size,screen.get_height()) > buffered_bottom_right[1]:
                    tile_buffer.blit(tile_graphic, draw_pos)
                    
    # finally draw the buffer to the screen
    screen.blit(tile_buffer, (0,0))
draws the section of level tiles currently visible to the player onto the screen surface.
       screen is the screen surface to draw onto
       tile_buffer is a screen-sized surface onto which the tiles are drawn before being drawn to the screen 
       surface. The buffer should contain the drawn tiles from the previous frame so that they can be reused if
       possible       
       last_position is the x,y position of the player in the level from the previous frame, as a 2-item sequence
       positon is the x,y position of the player in the level this frame, as a 2-item sequence
       level is the nested list of tile types representing the level
       tile_graphics is the list of graphics representing the available tile types
       ground_tile is the tile graphic used to draw tiles below the bottom of the level
       sky_tile is the tile graphic used to draw tiles above, to the left of, and to the right of the level
       tile_size is the size of each tile as an int
draws the section of level tiles currently visible to the player onto the screen surface.
screen is the screen surface to draw onto
tile_buffer is a screen-sized surface onto which the tiles are drawn before being drawn to the screen
surface. The buffer should contain the drawn tiles from the previous frame so that they can be reused if
possible
last_position is the x,y position of the player in the level from the previous frame, as a 2-item sequence
positon is the x,y position of the player in the level this frame, as a 2-item sequence
level is the nested list of tile types representing the level
tile_graphics is the list of graphics representing the available tile types
ground_tile is the tile graphic used to draw tiles below the bottom of the level
sky_tile is the tile graphic used to draw tiles above, to the left of, and to the right of the level
tile_size is the size of each tile as an int
draws the section of level tiles currently visible to the player onto the screen surface . screen is the screen surface to draw onto tile_buffer is a screen - sized surface onto which the tiles are drawn before being drawn to the screen surface . The buffer should contain the drawn tiles from the previous frame so that they can be reused if possible last_position is the x y position of the player in the level from the previous frame as a 2 - item sequence positon is the x y position of the player in the level this frame as a 2 - item sequence level is the nested list of tile types representing the level tile_graphics is the list of graphics representing the available tile types ground_tile is the tile graphic used to draw tiles below the bottom of the level sky_tile is the tile graphic used to draw tiles above to the left of and to the right of the level tile_size is the size of each tile as an int
def draw_tiles(screen, tile_buffer, last_position, position, level, tile_graphics, ground_tile, sky_tile, tile_size):
    position = list(map(int, position))
    last_position = list(map(int, last_position))
    scrolled_amount = [position[0]-last_position[0], position[1]-last_position[1]]
    tile_buffer.scroll(-int(scrolled_amount[0]), -int(scrolled_amount[1]))
    top_left_position = position[0]-screen.get_width()/2, position[1]-screen.get_height()/2
    top_left_tile = tile_ref(top_left_position, tile_size)
    top_left_draw_pos = -(top_left_position[0] % tile_size), -(top_left_position[1] % tile_size)
    rows_to_draw = int(float(screen.get_height()) / tile_size + 1)
    cols_to_draw = int(float(screen.get_width()) / tile_size + 1)
    buffered_top_left = [0-scrolled_amount[0], 0-scrolled_amount[1]]
    buffered_bottom_right = [buffered_top_left[0]+screen.get_width(), buffered_top_left[1]+screen.get_height()]
    if buffered_top_left[0] > 0:
        tile_buffer.fill((0,0,0,0),(0,0,buffered_top_left[0]-0,screen.get_height()))
    if buffered_top_left[1] > 0:
        tile_buffer.fill((0,0,0,0),(0,0,screen.get_width(),buffered_top_left[1]-0))
    if buffered_bottom_right[0] < screen.get_width():
        tile_buffer.fill((0,0,0,0),(buffered_bottom_right[0],0,
                                    screen.get_width()-buffered_bottom_right[0], screen.get_height()))
    if buffered_bottom_right[1] < screen.get_height():
        tile_buffer.fill((0,0,0,0),(0,buffered_bottom_right[1],
                                    screen.get_width(), screen.get_height()-buffered_bottom_right[1]))
    for j in range(rows_to_draw):
        for i in range(cols_to_draw):
            tile = top_left_tile[0]+i, top_left_tile[1]+j
            type = tile_type(tile, level, ground_tile, sky_tile)
            tile_graphic = tile_graphics[type]
            if tile_graphic is not None:
                draw_pos = top_left_draw_pos[0]+i*tile_size, top_left_draw_pos[1]+j*tile_size
                if max(draw_pos[0],0) < buffered_top_left[0] \
                        or min(draw_pos[0]+tile_size,screen.get_width()) > buffered_bottom_right[0] \
                        or max(draw_pos[1],0) < buffered_top_left[1] \
                        or min(draw_pos[1]+tile_size,screen.get_height()) > buffered_bottom_right[1]:
                    tile_buffer.blit(tile_graphic, draw_pos)
    screen.blit(tile_buffer, (0,0))
def draw_tiles ( screen , tile_buffer , last_position , position , level , tile_graphics , ground_tile , sky_tile , tile_size ) : position = list ( map ( int , position ) ) last_position = list ( map ( int , last_position ) ) scrolled_amount = [ position [ 0 ] - last_position [ 0 ] , position [ 1 ] - last_position [ 1 ] ] tile_buffer . scroll ( - int ( scrolled_amount [ 0 ] ) , - int ( scrolled_amount [ 1 ] ) ) top_left_position = position [ 0 ] - screen . get_width ( ) / 2 , position [ 1 ] - screen . get_height ( ) / 2 top_left_tile = tile_ref ( top_left_position , tile_size ) top_left_draw_pos = - ( top_left_position [ 0 ] % tile_size ) , - ( top_left_position [ 1 ] % tile_size ) rows_to_draw = int ( float ( screen . get_height ( ) ) / tile_size + 1 ) cols_to_draw = int ( float ( screen . get_width ( ) ) / tile_size + 1 ) buffered_top_left = [ 0 - scrolled_amount [ 0 ] , 0 - scrolled_amount [ 1 ] ] buffered_bottom_right = [ buffered_top_left [ 0 ] + screen . get_width ( ) , buffered_top_left [ 1 ] + screen . get_height ( ) ] if buffered_top_left [ 0 ] > 0 : tile_buffer . fill ( ( 0 , 0 , 0 , 0 ) , ( 0 , 0 , buffered_top_left [ 0 ] - 0 , screen . get_height ( ) ) ) if buffered_top_left [ 1 ] > 0 : tile_buffer . fill ( ( 0 , 0 , 0 , 0 ) , ( 0 , 0 , screen . get_width ( ) , buffered_top_left [ 1 ] - 0 ) ) if buffered_bottom_right [ 0 ] < screen . get_width ( ) : tile_buffer . fill ( ( 0 , 0 , 0 , 0 ) , ( buffered_bottom_right [ 0 ] , 0 , screen . get_width ( ) - buffered_bottom_right [ 0 ] , screen . get_height ( ) ) ) if buffered_bottom_right [ 1 ] < screen . get_height ( ) : tile_buffer . fill ( ( 0 , 0 , 0 , 0 ) , ( 0 , buffered_bottom_right [ 1 ] , screen . get_width ( ) , screen . get_height ( ) - buffered_bottom_right [ 1 ] ) ) for j in range ( rows_to_draw ) : for i in range ( cols_to_draw ) : tile = top_left_tile [ 0 ] + i , top_left_tile [ 1 ] + j type = tile_type ( tile , level , ground_tile , sky_tile ) tile_graphic = tile_graphics [ type ] if tile_graphic is not None : draw_pos = top_left_draw_pos [ 0 ] + i * tile_size , top_left_draw_pos [ 1 ] + j * tile_size if max ( draw_pos [ 0 ] , 0 ) < buffered_top_left [ 0 ] or min ( draw_pos [ 0 ] + tile_size , screen . get_width ( ) ) > buffered_bottom_right [ 0 ] or max ( draw_pos [ 1 ] , 0 ) < buffered_top_left [ 1 ] or min ( draw_pos [ 1 ] + tile_size , screen . get_height ( ) ) > buffered_bottom_right [ 1 ] : tile_buffer . blit ( tile_graphic , draw_pos ) screen . blit ( tile_buffer , ( 0 , 0 ) )
draws the section of level tiles currently visible to the player onto the screen surface.
draws the section of level tiles currently visible to the player onto the screen surface .
"""draws the section of level tiles currently visible to the player onto the screen surface.
       screen is the screen surface to draw onto
       tile_buffer is a screen-sized surface onto which the tiles are drawn before being drawn to the screen 
       surface. The buffer should contain the drawn tiles from the previous frame so that they can be reused if
       possible       
       last_position is the x,y position of the player in the level from the previous frame, as a 2-item sequence
       positon is the x,y position of the player in the level this frame, as a 2-item sequence
       level is the nested list of tile types representing the level
       tile_graphics is the list of graphics representing the available tile types
       ground_tile is the tile graphic used to draw tiles below the bottom of the level
       sky_tile is the tile graphic used to draw tiles above, to the left of, and to the right of the level
       tile_size is the size of each tile as an int""" # truncate the positon and last_position coordinates from floats to integers # calculate the x and y amount the camera moved since the last frame # tile_buffer is a surface containing the drawn tiles from the previous frame. We can save drawing all the tiles # again this frame by simply shifting the previous drawing by the amount the camera moved. The "scroll" method does # exactly that. # calculate the x,y coordinates within the level that the top left corner of the viewport is at # calculate the column and row of the tile the that this top left position sits within # calculate where on the screen (or just off it) this top-left tile would be drawn # calculate how many rows and colums of tiles must be drawn to cover the screen # calculate the bounds of the screen region we don't need to re-draw the tiles for # if this region doesnt start at the left edge, clear the tile buffer's left edge # if the region doesnt start at the top edge, clear the tile buffer's top edge # if the region doesnt end at the right edge, clear the tile buffer's right edge # if the region doesnt end at the bottom edge, clear the tile buffer's bottom edge  # iterate over the rows and columns to draw # calculate the column and row of the next tile to draw # find that tile's type # look up the tile's graphic # if the tile graphic isn't None (i.e. not an invisible tile) # calculate where to draw the tile # if the tile to draw isn't completely within the already-drawn region, draw it to the buffer # finally draw the buffer to the screen
screen
tile_buffer
last_position
position
level
tile_graphics
ground_tile
sky_tile
tile_size