Tiled Map Reader
- class arcade.TileMap(map_file: str | Path = '', scaling: float = 1.0, layer_options: dict[str, dict[str, Any]] | None = None, use_spatial_hash: bool = False, hit_box_algorithm: HitBoxAlgorithm | None = None, tiled_map: pytiled_parser.TiledMap | None = None, offset: Vec2 = (0, 0), texture_atlas: TextureAtlasBase | None = None, lazy: bool = False, texture_cache_manager: arcade.TextureCacheManager | None = None)[source]
Bases:
Class that represents a fully parsed and loaded map from Tiled. For examples on how to use this class, see: Step 12 - Loading a Map From a Map Editor
- Parameters:
map_file – A JSON map file for a Tiled map to initialize from
scaling – Global scaling to apply to all Sprites.
layer_options – Extra parameters for each layer.
use_spatial_hash – If set to True, this will make moving a sprite in the SpriteList slower, but it will speed up collision detection with items in the SpriteList. Great for doing collision detection with static walls/platforms.
hit_box_algorithm – The hit box algorithm to use for the Sprite’s in this layer.
tiled_map – An already parsed pytiled-parser map object. Passing this means that the
map_file
argument will be ignored, and the pre-parsed map will instead be used. This can be helpful for working with Tiled World files.offset – Can be used to offset the position of all sprites and objects within the map. This will be applied in addition to any offsets from Tiled. This value can be overridden with the layer_options dict.
texture_atlas – A default texture atlas to use for the SpriteLists created by this map. If not supplied the global default atlas will be used.
lazy – SpriteLists will be created lazily.
texture_cache_manager – The texture cache manager to use for loading textures.
The
layer_options
parameter can be used to specify per layer arguments. The available options for this are:use_spatial_hash
- A boolean to enable spatial hashing on this layer’s SpriteList.scaling
- A float providing layer specific Sprite scaling.hit_box_algorithm
- The hit box algorithm to use for the Sprite’s in this layer.offset
- A tuple containing X and Y position offsets for the layercustom_class
- All objects in the layer are created from this class instead of Sprite. Must be subclass of Sprite.custom_class_args
- Custom arguments, passed into the constructor of the custom_classtexture_atlas
- A texture atlas to use for the SpriteList from this layer, if none is supplied then the one defined at the map level will be used.Example configuring layer options for a layer named “Platforms”:
layer_options = { "Platforms": { "use_spatial_hash": True, "scaling": 2.5, "offset": (-128, 64), "custom_class": Platform, "custom_class_args": { "health": 100 } }, }
The keys and their values in each layer are passed to the layer processing functions using the ** operator on the dictionary.
- get_cartesian(x: float, y: float) tuple[float, float] [source]
Given a set of coordinates in pixel units, this returns the cartesian coordinates.
This assumes the supplied coordinates are pixel coordinates, and bases the cartesian grid off of the Map’s tile size.
If you have a map with 128x128 pixel Tiles, and you supply coordinates 500, 250 to this function you’ll receive back 3, 2
- Parameters:
x – The X Coordinate to convert
y – The Y Coordinate to convert
- get_tilemap_layer(layer_path: str) Layer | None [source]
Given a path to a layer, this will attempt to return the layer.
- Parameters:
layer_path – A string representing the path to the layer. For example, “Layer Group 1/Layer Group 2/Tile Layer 1”
- object_lists: dict[str, list[TiledObject]]
A dictionary mapping TiledObjects to their layer names. This is used for all object layers of the map.
- sprite_lists: dict[str, SpriteList]
A dictionary mapping SpriteLists to their layer names. This is used for all tile layers of the map.
- tiled_map: TiledMap
The pytiled-parser map object. This can be useful for implementing features that aren’t supported by this class by accessing the raw map data directly.
- arcade.load_tilemap(map_file: str | Path, scaling: float = 1.0, layer_options: dict[str, dict[str, Any]] | None = None, use_spatial_hash: bool = False, hit_box_algorithm: HitBoxAlgorithm | None = None, offset: Vec2 = (0, 0), texture_atlas: DefaultTextureAtlas | None = None, lazy: bool = False) TileMap [source]
Given a .json map file, loads in and returns a TileMap object.
A TileMap can be created directly using the classes __init__ function. This function exists for ease of use.
For more clarification on the layer_options key, see the __init__ function of the TileMap class
- Parameters:
map_file – The JSON map file.
scaling – The global scaling to apply to all Sprite’s within the map.
use_spatial_hash – If set to True, this will make moving a sprite in the SpriteList slower, but it will speed up collision detection with items in the SpriteList. Great for doing collision detection with static walls/platforms.
hit_box_algorithm – The hit box algorithm to use for collision detection.
layer_options – Layer specific options for the map.
offset – Can be used to offset the position of all sprites and objects within the map. This will be applied in addition to any offsets from Tiled. This value can be overridden with the layer_options dict.
texture_atlas – A default texture atlas to use for the SpriteLists created by this map. If not supplied the global default atlas will be used.
lazy – SpriteLists will be created lazily.