pytmx.util_pygame module

Copyright (C) 2012-2023, Leif Theden <leif.theden@gmail.com>

This file is part of pytmx.

pytmx is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

pytmx is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with pytmx. If not, see <http://www.gnu.org/licenses/>.

pytmx.util_pygame.load_pygame(filename: str, *args, **kwargs) → pytmx.pytmx.TiledMap[source]

Load a TMX file, images, and return a TiledMap class

PYGAME USERS: Use me.

this utility has ‘smart’ tile loading. by default any tile without transparent pixels will be loaded for quick blitting. if the tile has transparent pixels, then it will be loaded with per-pixel alpha. this is a per-tile, per-image check.

if a color key is specified as an argument, or in the tmx data, the per-pixel alpha will not be used at all. if the tileset’s image has colorkey transparency set in Tiled, the util_pygam will return images that have their transparency already set.

TL;DR: Don’t attempt to convert() or convert_alpha() the individual tiles. It is already done for you.

Parameters:filename – filename to load
Returns:new pytmx.TiledMap object
pytmx.util_pygame.pygame_image_loader(filename: str, colorkey: Union[Tuple[int, int, int, int], Tuple[int, int, int], int, str, None], **kwargs)[source]

pytmx image loader for pygame

Parameters:
  • filename – filename, including path, to load
  • colorkey – colorkey for the image
Returns:

function to load tile images

pytmx.util_pygame.simplify(all_points: List[Union[Tuple[int, int], pygame.math.Vector2, pytmx.pytmx.Point]], tilewidth: int, tileheight: int) → List[pygame.rect.Rect][source]

Given a list of points, return list of rects that represent them kludge:

“A kludge (or kluge) is a workaround, a quick-and-dirty solution, a clumsy or inelegant, yet effective, solution to a problem, typically using parts that are cobbled together.”

– wikipedia

turn a list of points into a rects adjacent rects will be combined.

plain english:

the input list must be a list of tuples that represent the areas to be combined into rects the rects will be blended together over solid groups

so if data is something like:

0 1 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1

you’ll have the 4 rects that mask the area like this:

..######…… ..####…….. ……….##.. ……….##.. ………….. ….##########

pretty cool, right?

there may be cases where the number of rectangles is not as low as possible, but I haven’t found that it is excessively bad. certainly much better than making a list of rects, one for each tile on the map!

pytmx.util_pygame.build_rects(tmxmap: pytmx.pytmx.TiledMap, layer: Union[int, str], tileset: Union[int, str, None], real_gid: Optional[int]) → List[pygame.rect.Rect][source]

Generate a set of non-overlapping rects that represents the distribution of the specified gid.

Useful for generating rects for use in collision detection

GID Note: You will need to add 1 to the GID reported by Tiled.

Parameters:
  • tmxmap – TiledMap object
  • layer – int or string name of layer
  • tileset – int or string name of tileset
  • real_gid – Tiled GID of the tile + 1 (see note)
Returns:

list of pygame Rect objects