This is a tutorial for using the maps module in kivent to load Tiled maps. It covers the steps required to load and display a map on the kivent canvas, but it doesn’t cover how to make a map in Tiled.
Building and installing the module
Make sure you have kivy and its dependencies properly set up and working using these installation instructions.
Clone the kivent repository to obtain the source. The module is currently in a separate branch ‘tiled_gsoc_2016’ so you can clone that branch only.
git clone -b tiled_gsoc_2016 https://github.com/kivy/kivent.git
You can skip the
-b tiled_gsoc_2016 if you want the whole repository.
kivent_core first. Assuming you cloned it in a dir named ‘kivent’
$ cd kivent/modules/core $ python setup.py build_ext install
Then install the maps module similarly
$ cd kivent/modules/maps $ python setup.py build_ext install
It is best to set up kivy and kivent in a virtual environment. Just make sure you use the correct python for the above commands. The module works best with python3, but it works with python2 too.
Setting up the KV file
We need a basic setup of the gameworld and a gameview where we will add the
renderers to be displayed. We also need to add systems which the tiles depend
PositionSystem2D is necessary for any map because it it responsible for the
tile positions. And
MapSystem holds the relevant data for the map hence that
is necessary too, obviously.
ColorSystem is required if there are shapes in
your map which require coloring. And
GameView is the canvas where we will
render the map’s layers.
This is the basic boilerplate KV necessary for rendering the map.
Setting up the Systems
I will start with the basic game app structure of
We now need to load the systems required for each layer. We will have to
specify parameters for them the same way we fo it in KV files. We will make 3
dicts, one each for Renderer, PolyRenderer and AnimationSystem and pass them
load_map_systems util function to create 4 layers.
We will be returned a list of renderers and animators. This list can be added to the gameworld init sequence like so. Renderers and animators require specific states to be set so we have to add these lists while setting states. Modify the corresponding lines with these.
These systems need to be rendered from bottom to top to preserve the
layer order. And the gameview camera handles rendering of these systems. So we
will set the render order for that camera to match layer index. Add this line
Loading the TMX file
Next up, we need to populate our systems with entities and for that we need a TileMap loaded with tile data. This data will be obtained from the TMX file. The util module has a function for loading TMX files and registering them with the map manager
setup_tile_map() should be added to
init_gameworld() so that it is called
after gameworld init.
parse_tmx takes the filename of the TMX, loads it to a
TileMap, registers it
map_manager with name as the filename without the extension, and
returns that same name.
Creating Entities in the GameWorld
All that is left to do is to create entities from this
The function for that is
init_entities_from_map. It requires the
instance and an instance of the gameworld’s
init_entity function. It is used
You can add this to
parse_tmx is called and we have
This is all we require to load a Tiled map in KivEnt.
Download the source files from here!
Thank you and happy tiling!
EDIT (2016-08-24): Added installation instructions.