Tilesheets and tilemaps

Tile

The Tile class is the base for all tilesheets. It represents a single tile on the map, and contains the tile’s flags, such as whether the tile is solid (ie, can’t be walked through).

class sappho.tilemap.Tile(id_, image, solid_block=False)[source]

A tile object is a sprite, which is typically a subsurface of a tilesheet.

id_

str – The ID of this tile in its respective tilesheet.

image

pygame.surface.Surface

solid_block

bool – If true, this entire tile is completely impassible.

Tilesheet

A tilesheet is a collection of tiles, usually loaded from a tilesheet image. A tilesheet image looks like this:

_images/tilesheet.png

A tilesheet is represented in Sappho by the Tilesheet class. It holds information about the size of the tiles on the tilesheet, a reference to each tile, and of course, the pygame.Surface that holds the tilesheet itself.

class sappho.tilemap.Tilesheet(surface, tiles, tile_size)[source]

Efficient place to update, subsurface tile graphics.

surface

pygame.surface.Surface

tiles

list[Tile] – The tiles composing this Tilesheet.

tile_size

tuple[int, int] – The size of each tile in pixels (x, y).

classmethod from_file(file_path, tile_width, tile_height)[source]

Creates a tilesheet, and its tiles, from a file, as well as tile width and height specification.

Parameters:
  • file_path (str) – Path to the tilesheet image
  • tile_width (int) – Width of each tile
  • tile_height (int) – Height of each tile
static parse_rules(path_to_rules_file)[source]

Get properties for tile IDs from the path_to_rules_file.

Argument:
path_to_rules_file (str): Path to the rules file to parse
Returns:A dictionary containing an entry for each tile that has a flag set, the value being a list of the flags that are set
Return type:dict

TileMap

The TileMap class represents the ordered collection of tiles that make up a game map. Typically, you won’t create a TileMap instance yourself, instead you would load the TileMap from a TMX file using tmx_file_to_tilemaps(), which creates a TileMap for each layer of the TMX map that is passed to it.

tilemap.tmx_file_to_tilemaps(tmx_file_path, tilesheet)

Read TMX file from path and return a list of TileMaps (one TileMap per layer).

Parameters:
Returns:

Each layer gets its own TileMap!

Return type:

list[TileMap]

Note

Uses CSV layers; TMX allows all kinds, but CSV is the default.

Once the TileMap has been created, there are a number of methods available on it that are useful, such as getting the solid blocks in the map, and rendering it to a surface.

class sappho.tilemap.TileMap(tilesheet, tiles)[source]

A 2D grid arrangement of tiles, accompanied by passability information.

This is generally one layer of a larger map, and ideally should be blitted to a sappho.layers.SurfaceLayers object to handle multiple map layers.

Parameters:
  • tilesheet (Tilesheet) – Tilesheet to use for this map
  • tiles (list[list[Tile]]) – List of rows, each containing a list of Tile objects representing the tiles in this TileMap
get_solid_blocks()[source]

Return the Pygame rect of all the tiles which have the solid_block attribute.

Returns:List of pygame.Rect objects that represent the solid tiles in this TileMap
Return type:list[pygame.Rect]
to_surface()[source]

Blit the TileMap to a surface

Returns:Surface containing the tilemap
Return type:pygame.Surface