tiledtmxloader Package

tiledtmxloader Package

TileMap loader for python for Tiled, a generic tile map editor from http://mapeditor.org/ . It loads the *.tmx files produced by Tiled.

helperspygame Module

TileMap loader for python for Tiled, a generic tile map editor from http://mapeditor.org/ . It loads the *.tmx files produced by Tiled.

This is the code that helps using the tmx files using pygame. In this module there is a pygame specific loader and renderer.

TODO: mini example and usage of the classes from this module TODO: link to homepage TODO: examples

class tiledtmxloader.helperspygame.RendererPygame[source]

Bases: object

A renderer for pygame. Should be fast enough for most purposes.

Example:

# init
sprite_layers = get_layers_from_map(resources)
renderer = RendererPygame()

# in main loop
while running:

    # move camera
    renderer.set_camera_position(x, y)

    # draw layers
    for sprite_layer in sprite_layers:
        renderer.render_layer(screen, sprite_layer, clip_sprites)
get_world_pos(layer, screen_x, screen_y)[source]

Returns the world coordinates for the given screen location and layer.

Note :

this is important so one can check which entity is there in the model (knowing which sprite is there does not help much)

Parameters :
layer : SpriteLayer

the layer to pick from

screen_x : int

The screen position in x direction.

screen_y : int

The screen position in y direction.

Returns :

Tuple of world coordinates: (world_x, world_y)

pick_layer(layer, screen_x, screen_y)[source]

Returns the sprite at the given screen position or None regardless of the layers visibility.

Note :

This does not work wir object group layers.

Parameters :
layer : SpriteLayer

the layer to pick from

screen_x : int

The screen position in x direction.

screen_y : int

The screen position in y direction.

Returns :

None if there is no sprite or the sprite (SpriteLayer.Sprite instance).

pick_layers_sprites(layer, screen_x, screen_y)[source]

Returns the sprites at the given screen positions or an empty list. The sprites are the same order as in the layers.sprites list.

Note :

This does not work wir object group layers.

Parameters :
layer : SpriteLayer

the layer to pick from

screen_x : int

The screen position in x direction.

screen_y : int

The screen position in y direction.

Returns :

A list of sprites or an empty list.

render_layer(surf, layer, clip_sprites=True, sort_key=<function <lambda> at 0x017F4370>)[source]

Renders a layer onto the given surface.

Parameters :
surf : Surface

Surface to render onto.

layer : SpriteLayer

The layer to render. Invisible layers will be skipped.

clip_sprites : boolean

Optional, defaults to True. Clip the sprites of this layer to only draw the ones intersecting the visible part of the world.

sort_key : function

Optional: The sort function for the parameter ‘key’ of the sort method of the list.

set_camera_margin(margin_left, margin_right, margin_top, margin_bottom)[source]

Set the margin around the camera (in pixels).

Parameters :
margin_left : int

number of pixels of the left side marging

margin_right : int

number of pixels of the right side marging

margin_top : int

number of pixels of the top side marging

margin_bottom : int

number of pixels of the left bottom marging

set_camera_position(world_pos_x, world_pos_y, alignment='center')[source]

Set the camera position in the world.

Parameters :
world_pos_x : int

position in x in world coordinates

world_pos_y : int

position in y in world coordinates

alignment : string

defines to which part of the cam rect the position belongs, can be any pygame.Rect attribute: ‘center’, ‘topleft’, ‘topright’, ...

set_camera_position_and_size(world_pos_x, world_pos_y, width, height, alignment='center')[source]

Set the camera position and size in the world.

Parameters :
world_pos_x : int

Position in x in world coordinates.

world_pos_y : int

Position in y in world coordinates.

witdh : int

With of the camera rect (the rendered area).

height : int

The height of the camera rect (the rendered area).

alignment : string

Defines to which part of the cam rect the position belongs, can be any pygame.Rect attribute: ‘center’, ‘topleft’, ‘topright’, ...

set_camera_rect(cam_rect_world_coord)[source]

Set the camera position and size using a rect in world coordinates.

Parameters :
cam_rect_world_coord : pygame.Rect

A rect describing the cameras position and size in the world.

class tiledtmxloader.helperspygame.ResourceLoaderPygame[source]

Bases: tiledtmxloader.tiledtmxloader.AbstractResourceLoader

Resource loader for pygame. Loads the images as pygame.Surfaces and saves them in the variable indexed_tiles.

Example:

res_loader = ResourceLoaderPygame()
# tile_map loaded the the TileMapParser.parse() method
res_loader.load(tile_map)
load(tile_map)[source]
class tiledtmxloader.helperspygame.SpriteLayer(tile_layer_idx, resource_loader)[source]

Bases: object

The SpriteLayer class. This class is used by the RendererPygame.

class Sprite(image, rect, source_rect=None, flags=0, key=None)[source]

Bases: object

The Sprite class used by the SpriteLayer class and the RendererPygame.

get_draw_cond()[source]

Defines if the sprite lays on the floor or if it is up-right.

Returns:The bottom y coordinate so the sprites can be sorted in right draw order.
SpriteLayer.add_sprite(sprite)[source]

Add dynamic sprite to this layer.

Parameters :
sprite : SpriteLayer.Sprite

sprite to add

SpriteLayer.add_sprites(sprites)[source]

Add multiple dynamic sprites to this layer.

Parameters :
sprites : list

list of SpriteLayer.Sprite to add

static SpriteLayer.collapse(layer)[source]

Makes 1 tile out of 4. The idea behind is that fewer tiles are faster to render, but that is not always true. Grouping them together into one bigger sprite is one way to get fewer sprites.

Not :

This only works for static layers without any dynamic sprites.

Note :

use with caution

Parameters :
laser : SpriteLayer

The layer to collapse

Returns:

new SpriteLayer with fewer sprites but double the size.

SpriteLayer.contains_sprite(sprite)[source]

Check if the given sprites is already in this layer.

Parameters :
sprite : SpriteLayer.Sprite

sprite to check

Returns :

bool, true if sprite is in this layer

SpriteLayer.get_collapse_level()[source]

The level of collapsing.

Returns:The collapse level.
SpriteLayer.get_layer_paralax_factor_x()[source]

Retrieve the current x paralax factor.

Returns :returns the current x paralax factor.
SpriteLayer.get_layer_paralax_factor_y()[source]

Retrieve the current y paralax factor.

Returns :returns the current y paralax factor.
SpriteLayer.has_sprites()[source]

Checks if this layer has dynamic sprites at all.

Returns :bool, true if it contains at least 1 dynamic sprite.
static SpriteLayer.merge(layers)[source]

Merges multiple Sprite layers into one. Only SpriteLayers are supported. All layers need to be equal in tile size, number of tiles and layer position. Otherwise a SpriteLayerNotCompatibleError is raised.

Parameters :
layers : list

The SpriteLayer to be merged

Returns:

new SpriteLayer with merged tiles

SpriteLayer.remove_sprite(sprite)[source]

Removes a dynamic sprite from this layer.

Parameters :
sprite : SpriteLayer.Sprite

sprite to remove

SpriteLayer.remove_sprites(sprites)[source]

Remove multiple sprites at once.

Parameters :
sprites : list

list of SpriteLayer.Sprite to remove

static SpriteLayer.scale(layer_orig, scale_w, scale_h)[source]

Scales a layer and returns a new, scaled SpriteLayer.

Parameters :
scale_w : float

Width scale factor in range (0, ...]

scale_h : float

Height scale factor in range (0, ...]

SpriteLayer.set_layer_paralax_factor(factor_x=1.0, factor_y=None)[source]

Set the paralax factor. This is for paralax scrolling this layer. Values x < 0.0 will make the layer scroll in opposite direction Value x == 0.0 makes the layer fix to the screen (wont scroll) Values 0.0 < x < 1.0 will make scroll the layer slower. Value x == 1.0 is default and make scroll the layer normal. Values x > 1.0 make scroll the layer faster than normal

Parameters :
factor_x : float

Paralax factor in x direction. Defaults to 1.0

factor_y : float

Paralax factor in y direction. If this is None then it will have the same value as the factor_x argument.

exception tiledtmxloader.helperspygame.SpriteLayerNotCompatibleError[source]

Bases: exceptions.Exception

tiledtmxloader.helperspygame.demo_pygame(file_name)[source]
tiledtmxloader.helperspygame.get_layer_at_index(layer_idx, resource_loader)[source]

Creates one SpriteLayer from index out of the map.

Parameters :
layer_idx : int

Index of the layer to create.

resource_loader : ResourceLoaderPygame

a resource loader instance

Returns :

a SpriteLayer instance

tiledtmxloader.helperspygame.get_layers_from_map(resource_loader)[source]

Creates SpriteLayers out of the map.

Parameters :
resource_loader : ResourceLoaderPygame

a resource loader instance

Returns :

list of SpriteLayers

tiledtmxloader.helperspygame.main()[source]

helperspyglet Module

> Overview This program contains a sample implementation for loading a map produced by Tiled in pyglet. The script can be run on its own to demonstrate its capabilities, or the script can be imported to use its functionality. Users will hopefully use the ResourceLoaderPyglet already provided in this. Tiled may be found at http://mapeditor.org/

> Demo Controls Holding the arrow keys scrolls the map. Holding the left shift key makes you scroll faster. Pressing the Esc key closes the program.

> Demo Features The map is fully viewable by scrolling. You can scroll outside of the bounds of the map. All visible layers are loaded and displayed. Transparency is supported. (Nothing needed to be done for this.) Minimal OpenGL used. (Less of a learning curve.)

class tiledtmxloader.helperspyglet.ResourceLoaderPyglet[source]

Bases: tiledtmxloader.tiledtmxloader.AbstractResourceLoader

Loads all tile images and lays them out on a grid.

Unlike the AbstractResourceLoader this class derives from, no overridden methods use a colorkey parameter. A colorkey is only useful for pygame. This loader adds its own pyglet-specific parameter to deal with pyglet.image.load’s capability to work with file-like objects.

load(tile_map)[source]
tiledtmxloader.helperspyglet.demo_pyglet(file_name)[source]

Demonstrates loading, rendering, and traversing a Tiled map in pyglet.

TODO: Maybe use this to put topleft as origin:

glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (double)mTarget->w, (double)mTarget->h, 0.0, -1.0, 1.0);

tiledtmxloader Module

TileMap loader for python for Tiled, a generic tile map editor from http://mapeditor.org/ . It loads the *.tmx files produced by Tiled.

class tiledtmxloader.tiledtmxloader.AbstractResourceLoader[source]

Bases: object

Abstract base class for the resource loader.

FLIP_X = 2147483648L
FLIP_Y = 1073741824
load(tile_map)[source]
class tiledtmxloader.tiledtmxloader.MapObject[source]

Bases: object

A single object on the map.

Ivariables :
x : int

x position relative to group x position

y : int

y position relative to group y position

width : int

width of this object

height : int

height of this object

type : string

the type of this object

image_source : string

source path of the image for this object

image : :class:TileImage

after loading this is the pygame surface containing the image

class tiledtmxloader.tiledtmxloader.MapObjectGroupLayer[source]

Bases: object

Group of objects on the map.

Ivariables :
x : int

the x position

y : int

the y position

width : int

width of the bounding box (usually 0, so no use)

height : int

height of the bounding box (usually 0, so no use)

name : string

name of the group

objects : list

list of the map objects

convert()[source]
class tiledtmxloader.tiledtmxloader.Tile[source]

Bases: object

A single tile.

Ivariables :
id : int

id of the tile gid = TileSet.firstgid + Tile.id

images : list of :class:TileImage

list of TileImage, either its ‘id’ or ‘image data’ will be set

properties : dict of name:value

the propertis set in the editor, name-value pairs

class tiledtmxloader.tiledtmxloader.TileImage[source]

Bases: object

An image of a tile or just an image.

Ivariables :
id : int

id of this image (has nothing to do with gid)

format : string

the format as string, only ‘png’ at the moment

source : string

filename of the image. either this is set or the content

encoding : string

encoding of the content

trans : tuple of (r,g,b)

the colorkey color, raw as hex, after calling convert just a (r,g,b) tuple

properties : dict

the propertis set in the editor, name-value pairs

image : TileImage

after calling load the pygame surface

class tiledtmxloader.tiledtmxloader.TileLayer[source]

Bases: object

A layer of the world.

Ivariables :
x : int

position of layer in the world in number of tiles (not pixels)

y : int

position of layer in the world in number of tiles (not pixels)

width : int

number of tiles in x direction

height : int

number of tiles in y direction

pixel_width : int

width of layer in pixels

pixel_height : int

height of layer in pixels

name : string

name of this layer

opacity : float

float from 0 (full transparent) to 1.0 (opaque)

decoded_content : list

list of graphics id going through the map:

e.g [1, 1, 1, ]
where decoded_content[0]   is (0,0)
      decoded_content[1]   is (1,0)
      ...
      decoded_content[w]   is (width,0)
      decoded_content[w+1] is (0,1)
      ...
      decoded_content[w * h]  is (width,height)

usage: graphics id = decoded_content[tile_x + tile_y * width]
content2D : list

list of list, usage: graphics id = content2D[x][y]

convert()[source]
decode(tile_map)[source]

Converts the contents in a list of integers which are the gid of the used tiles. If necessairy it decodes and uncompresses the contents.

pretty_print()[source]
class tiledtmxloader.tiledtmxloader.TileMap[source]

Bases: object

The TileMap holds all the map data.

Ivariables :
orientation : string

orthogonal or isometric or hexagonal or shifted

tilewidth : int

width of the tiles (for all layers)

tileheight : int

height of the tiles (for all layers)

width : int

width of the map (number of tiles)

height : int

height of the map (number of tiles)

version : string

version of the map format

tile_sets : list

list of TileSet

properties : dict

the propertis set in the editor, name-value pairs, strings

pixel_width : int

width of the map in pixels

pixel_height : int

height of the map in pixels

layers : list

list of TileLayer

map_file_name : dict

file name of the map

named_layers : dict of string:TledLayer

dict containing {name : TileLayer}

named_tile_sets : dict

dict containing {name : TileSet}

convert()[source]

Converts numerical values from strings to numerical values. It also calculates or set additional data: pixel_width pixel_height named_layers named_tile_sets

decode()[source]

Decodes the TileLayer encoded_content and saves it in decoded_content.

class tiledtmxloader.tiledtmxloader.TileMapParser[source]

Bases: object

Allows to parse and decode map files for ‘Tiled’, a open source map editor written in java. It can be found here: http://mapeditor.org/

parse(file_name)[source]

Parses the given map. Does no decoding nor loading of the data. :return: instance of TileMap

parse_decode(file_name)[source]

Parses the map but additionally decodes the data. :return: instance of TileMap

class tiledtmxloader.tiledtmxloader.TileSet[source]

Bases: object

A tileset holds the tiles and its images.

Ivariables :
firstgid : int

the first gid of this tileset

name : string

the name of this TileSet

images : list

list of TileImages

tiles : list

list of Tiles

indexed_images : dict

after calling load() it is dict containing id: image

spacing : int

the spacing between tiles

marging : int

the marging of the tiles

properties : dict

the propertis set in the editor, name-value pairs

tilewidth : int

the actual width of the tile, can be different from the tilewidth of the map

tilehight : int

the actual hight of th etile, can be different from the tilehight of the map

exception tiledtmxloader.tiledtmxloader.VersionError[source]

Bases: exceptions.Exception

tiledtmxloader.tiledtmxloader.decode_base64(in_str)[source]

Decodes a base64 string and returns it.

Parameters :
in_str : string

base64 encoded string

Returns:

decoded string

tiledtmxloader.tiledtmxloader.decompress_gzip(in_str)[source]

Uncompresses a gzip string and returns it.

Parameters :
in_str : string

gzip compressed string

Returns:

uncompressed string

tiledtmxloader.tiledtmxloader.decompress_zlib(in_str)[source]

Uncompresses a zlib string and returns it.

Parameters :
in_str : string

zlib compressed string

Returns:

uncompressed string

tiledtmxloader.tiledtmxloader.printer(obj, ident='')[source]

Helper function, prints a hirarchy of objects.

Table Of Contents

Previous topic

Welcome to pytmxloader’s documentation!

Next topic

test Package

This Page