def get_grid_rects(pos, scroll_x, scroll_y, unit_rect):
    """Return screen and level grid rects.

    The position given (usually from mouse cursor) is
    constrained to a grid of whose unit is represented by
    the unit rect provided. The net amount of scrolling in
    x and y axes is also taken into account.

    Screen rect is the one 'touched' by the pos parameter
    on the screen, considering its slight offset caused by
    the scrolling.

    The level rect is obtained by moving the screen rect by
    the reverse scrolling amount, so to reflect its position
    as if the level was never scrolled.

    pos
        2-tuple of integers representing a position in the
        screen (usually from the mouse cursor) in the
        x and y axes, respectively.
    scroll_x, scroll_y
        Integers indicating the amount of scrolling already
        performed in the x and y axes, respectively.
    unit_rect
        pygame.Rect instance indicating an unit in the grid.
    """
    ### store variables
    x, y = pos
    unit_width, unit_height = unit_size = unit_rect.size

    ### calculate topleft position of the grid unit where
    ### the provided position 'touches' the screen;
    ### we store it in a vector object

    horizontal_rest = scroll_x % unit_width
    vertical_rest = scroll_y % unit_height

    x_rest = (x - horizontal_rest) % unit_width
    y_rest = (y - vertical_rest) % unit_height

    grid_x = x - x_rest
    grid_y = y - y_rest

    topleft = grid_x, grid_y

    ### generate and return rects

    screen_rect = Rect(topleft, unit_size)

    level_rect = screen_rect.move(-scroll_x, -scroll_y)

    return screen_rect, level_rect
Return screen and level grid rects.

    The position given (usually from mouse cursor) is
    constrained to a grid of whose unit is represented by
    the unit rect provided. The net amount of scrolling in
    x and y axes is also taken into account.

    Screen rect is the one 'touched' by the pos parameter
    on the screen, considering its slight offset caused by
    the scrolling.

    The level rect is obtained by moving the screen rect by
    the reverse scrolling amount, so to reflect its position
    as if the level was never scrolled.

    pos
        2-tuple of integers representing a position in the
        screen (usually from the mouse cursor) in the
        x and y axes, respectively.
    scroll_x, scroll_y
        Integers indicating the amount of scrolling already
        performed in the x and y axes, respectively.
    unit_rect
        pygame.Rect instance indicating an unit in the grid.
    
nodezator/our3rdlibs/grid/main.py