def build_rects(tmxmap, layer, tileset=None, real_gid=None):
    """generate a set of non-overlapping rects that represents the distribution
       of the specified gid.

    useful for generating rects for use in collision detection

    Use at your own risk: this is experimental...will change in future

    GID Note: You will need to add 1 to the GID reported by Tiled.

    :param tmxmap: TiledMap object
    :param layer: int or string name of layer
    :param tileset: int or string name of tileset
    :param real_gid: Tiled GID of the tile + 1 (see note)
    :return: List of pygame Rect objects
    """
    if isinstance(tileset, int):
        try:
            tileset = tmxmap.tilesets[tileset]
        except IndexError:
            msg = "Tileset #{0} not found in map {1}."
            logger.debug(msg.format(tileset, tmxmap))
            raise IndexError

    elif isinstance(tileset, str):
        try:
            tileset = [t for t in tmxmap.tilesets if t.name == tileset].pop()
        except IndexError:
            msg = "Tileset \"{0}\" not found in map {1}."
            logger.debug(msg.format(tileset, tmxmap))
            raise ValueError

    elif tileset:
        msg = "Tileset must be either a int or string. got: {0}"
        logger.debug(msg.format(type(tileset)))
        raise TypeError

    gid = None
    if real_gid:
        try:
            gid, flags = tmxmap.map_gid(real_gid)[0]
        except IndexError:
            msg = "GID #{0} not found"
            logger.debug(msg.format(real_gid))
            raise ValueError

    if isinstance(layer, int):
        layer_data = tmxmap.get_layer_data(layer)
    elif isinstance(layer, str):
        try:
            layer = [l for l in tmxmap.layers if l.name == layer].pop()
            layer_data = layer.data
        except IndexError:
            msg = "Layer \"{0}\" not found in map {1}."
            logger.debug(msg.format(layer, tmxmap))
            raise ValueError

    p = itertools.product(range(tmxmap.width), range(tmxmap.height))
    if gid:
        points = [(x, y) for (x, y) in p if layer_data[y][x] == gid]
    else:
        points = [(x, y) for (x, y) in p if layer_data[y][x]]

    rects = simplify(points, tmxmap.tilewidth, tmxmap.tileheight)
    return rects
def build_rects ( tmxmap , layer , tileset = None , real_gid = None ) : if isinstance ( tileset , int ) : try : tileset = tmxmap . tilesets [ tileset ] except IndexError : msg = "Tileset #{0} not found in map {1}." logger . debug ( msg . format ( tileset , tmxmap ) ) raise IndexError elif isinstance ( tileset , str ) : try : tileset = [ t for t in tmxmap . tilesets if t . name == tileset ] . pop ( ) except IndexError : msg = "Tileset \"{0}\" not found in map {1}." logger . debug ( msg . format ( tileset , tmxmap ) ) raise ValueError elif tileset : msg = "Tileset must be either a int or string. got: {0}" logger . debug ( msg . format ( type ( tileset ) ) ) raise TypeError gid = None if real_gid : try : gid , flags = tmxmap . map_gid ( real_gid ) [ 0 ] except IndexError : msg = "GID #{0} not found" logger . debug ( msg . format ( real_gid ) ) raise ValueError if isinstance ( layer , int ) : layer_data = tmxmap . get_layer_data ( layer ) elif isinstance ( layer , str ) : try : layer = [ l for l in tmxmap . layers if l . name == layer ] . pop ( ) layer_data = layer . data except IndexError : msg = "Layer \"{0}\" not found in map {1}." logger . debug ( msg . format ( layer , tmxmap ) ) raise ValueError p = itertools . product ( range ( tmxmap . width ) , range ( tmxmap . height ) ) if gid : points = [ ( x , y ) for ( x , y ) in p if layer_data [ y ] [ x ] == gid ] else : points = [ ( x , y ) for ( x , y ) in p if layer_data [ y ] [ x ] ] rects = simplify ( points , tmxmap . tilewidth , tmxmap . tileheight ) return rects
generate a set of non-overlapping rects that represents the distribution
       of the specified gid.

    useful for generating rects for use in collision detection

    Use at your own risk: this is experimental...will change in future

    GID Note: You will need to add 1 to the GID reported by Tiled.

    :param tmxmap: TiledMap object
    :param layer: int or string name of layer
    :param tileset: int or string name of tileset
    :param real_gid: Tiled GID of the tile + 1 (see note)
    :return: List of pygame Rect objects