コード例 #1
0
def _load_tiled_tmx_map(tmx_map, gummworld_map, load_invisible=True):
    """Load an orthogonal TMX map file that was created by the Tiled Map Editor.
    
    If load_invisible is False, layers where visible!=0 will be empty. Their
    tiles will not be loaded.
    
    Thanks to DR0ID for his nice tiledtmxloader module:
        http://www.pygame.org/project-map+loader+for+%27tiled%27-1158-2951.html
    
    And the creators of Tiled Map Editor:
        http://www.mapeditor.org/
    """
    
    # Taken pretty much verbatim from the (old) tiledtmxloader module.
    
    from pygame.sprite import Sprite
    
    resource = ResourceLoaderPygame()
    resource.load(tmx_map)
    tile_size = (tmx_map.tilewidth, tmx_map.tileheight)
    map_size = (tmx_map.width, tmx_map.height)
    
    for layeri,layer in enumerate(tmx_map.layers):
        gummworld_layer = TiledLayer(gummworld_map, layer, layeri)
        gummworld_map.layers.append(gummworld_layer)
        if not layer.visible and not load_invisible:
            continue
        if layer.is_object_group:
            for obj in layer.objects:
                sprite = Sprite()
                sprite.image = obj.image
                sprite.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                sprite.type = obj.type
                sprite.image_source = obj.image_source
                sprite.name = obj.name
                sprite.properties = obj.properties
                if hasattr(obj,'gid'): sprite.gid=obj.gid
                gummworld_layer.add(sprite)
        else:
            for ypos in xrange(0, layer.height):
                for xpos in xrange(0, layer.width):
                    x = (xpos + layer.x) * layer.tilewidth
                    y = (ypos + layer.y) * layer.tileheight
                    img_idx = layer.content2D[xpos][ypos]
                    if img_idx == 0:
                        continue
                    try:
                        offx,offy,tile_img = resource.indexed_tiles[img_idx]
                        screen_img = tile_img
                    except KeyError:
                        print 'KeyError',img_idx,(xpos,ypos)
                        continue
                    sprite = Sprite()
                    ## Note: alpha conversion can actually kill performance.
                    ## Do it only if there's a benefit.
#                    if convert_alpha:
#                        if screen_img.get_alpha():
#                            screen_img = screen_img.convert_alpha()
#                        else:
#                            screen_img = screen_img.convert()
#                            if layer.opacity > -1:
#                                screen_img.set_alpha(None)
#                                alpha_value = int(255. * float(layer.opacity))
#                                screen_img.set_alpha(alpha_value)
#                                screen_img = screen_img.convert_alpha()
                    sprite.image = screen_img
                    sprite.rect = screen_img.get_rect(topleft=(x + offx, y + offy))
                    sprite.name = xpos,ypos
                    #myhack to detect which is idx of image  in the sprite
                    sprite.img_idx=img_idx
                    #end of hack
                    gummworld_layer.add(sprite)
    gummworld_map.mio_resource=resource