tuxemon.graphics module

General “tools” code for pygame graphics operations that don’t have a home in any specific place.

class tuxemon.graphics.LoaderProtocol(*args, **kwargs)[source]

Bases: Protocol

tuxemon.graphics.animation_frame_files(directory, name)[source]

Return list of filenames from directory for use in animation.

  • each filename will have the format: animation_name[0-9]*..*

  • will be returned in sorted order

For example, water00.png, water01.png, water02.png.

Parameters:
  • directory (str) – Directory where the frames are located.

  • name (str) – Name of the animation (common prefix of the frames).

Returns:

Sequence of filenames.

Return type:

Sequence[str]

tuxemon.graphics.capture_screenshot(game)[source]

Capture a screenshot of the current map.

Parameters:

game (LocalPygameClient) – The game object.

Returns:

The captured screenshot.

Return type:

pygame.surface.Surface

tuxemon.graphics.convert_alpha_to_colorkey(surface, colorkey=(255, 0, 255))[source]

Convert image with per-pixel alpha to normal surface with colorkey.

This is a crude hack that only works well with images that do not have alpha blended antialiased edges. Using this function on such images will result in discoloration of edges.

Parameters:
  • surface (Surface) – Image with per-pixel alpha.

  • colorkey (Color | str | Tuple[int, int, int] | Tuple[int, int, int, int]) – Colorkey to use for transparency.

Returns:

New surface with colorkey.

Return type:

Surface

tuxemon.graphics.create_animation(frames, duration, loop)[source]

Create animation from frames, a list of surfaces.

Parameters:
  • frames (Iterable[Surface]) – Surfaces used to create the animation.

  • duration (float) – Duration in seconds.

  • loop (bool) – Whether the animation should loop or not.

Returns:

Created animation.

Return type:

SurfaceAnimation

tuxemon.graphics.cursor_from_image(image)[source]

Take a valid image and create a mouse cursor.

Parameters:

image (Surface) –

Return type:

Sequence[str]

tuxemon.graphics.get_avatar(session, avatar)[source]

Gets the avatar sprite of a monster or NPC.

Used to parse the string values for dialog event actions. If avatar is a number, we’re referring to a monster slot in the player’s party. If avatar is a string, we’re referring to a monster by name. TODO: If the monster name isn’t found, we’re referring to an NPC on the map.

Parameters:
  • session (Session) – Game session.

  • avatar (str) – The identifier of the avatar to be used.

Returns:

The surface of the monster or NPC avatar sprite.

Return type:

Sprite | None

tuxemon.graphics.load_and_scale(filename)[source]

Load an image and scale it according to game settings.

  • Filename will be transformed to be loaded from game resource folder

  • Will be converted if needed

  • Scale factor will match game setting

Parameters:

filename (str) – Path of the image file.

Returns:

Loaded and scaled image.

Return type:

Surface

tuxemon.graphics.load_animated_sprite(filenames, delay)[source]

Load a set of images and return an animated sprite.

Image name will be transformed and converted. Rect attribute will be set.

Any keyword arguments will be passed to the get_rect method of the image for positioning the rect.

Parameters:
  • filenames (Iterable[str]) – Filenames to load.

  • delay (float) – Frame interval; time between each frame.

Returns:

Loaded animated sprite.

Return type:

Sprite

tuxemon.graphics.load_animation_from_frames(directory, name, duration, loop=False)[source]

Load animation from a collection of frame files.

Parameters:
  • directory (str) – Directory where the frames are located.

  • name (str) – Name of the animation (common prefix of the frames).

  • duration (float) – Duration in seconds.

  • loop (bool) – Whether the animation should loop or not.

Returns:

Created animation.

Return type:

SurfaceAnimation

tuxemon.graphics.load_frames_files(directory, name)[source]

Load frames from filenames.

For example, water00.png, water01.png, water03.png.

Parameters:
  • directory (str) – Directory where the frames are located.

  • name (str) – Name of the animation (common prefix of the frames).

Yields:

Loaded and scaled frames.

Return type:

Generator[Surface, None, None]

tuxemon.graphics.load_image(filename)[source]

Load image from the resources folder

  • Filename will be transformed to be loaded from game resource folder

  • Will be converted if needed.

This is a “smart” loader, and will convert files in the best way, but is slightly slower than just loading. Its important that this is not called too often (like once per draw!)

Parameters:

filename (str) – Path of the image file.

Returns:

Loaded image.

Return type:

Surface

tuxemon.graphics.load_sprite(filename, **rect_kwargs)[source]

Load an image from disk and return a sprite.

Image name will be transformed and converted. Rect attribute will be set.

Any keyword arguments will be passed to the get_rect method of the image for positioning the rect.

Parameters:
  • filename (str) – Filename to load.

  • rect_kwargs (Any) – Parameters for get_rect.

Returns:

Loaded sprite.

Return type:

Sprite

tuxemon.graphics.scale_sprite(sprite, ratio)[source]

Scale a sprite’s image in place.

Parameters:
  • sprite (Sprite) – Sprite to rescale.

  • ratio (float) – Amount to scale by.

Return type:

None

tuxemon.graphics.scale_surface(surface, factor)[source]

Scale a surface. Just a shortcut.

Parameters:
  • surface (Surface) –

  • factor (float) –

Return type:

Surface

tuxemon.graphics.scale_tile(surface, tile_size)[source]

Scales a map tile based on resolution.

Parameters:
  • surface (Surface) – Surface to rescale.

  • tile_size (int) – Desired size.

Returns:

The rescaled surface.

Return type:

Surface

tuxemon.graphics.scaled_image_loader(filename, colorkey, *, pixelalpha=True, **kwargs)[source]

Pytmx image loader for pygame.

Modified to load images at a scaled size.

Parameters:
  • filename (str) – Path of the image.

  • colorkey (str | None) – Hex values of the transparency color.

  • pixelalpha (bool) – Whether to use per-pixel alpha transparency or not.

  • kwargs (Any) – Ignored parameters passed in the loader.

Returns:

The loader to use.

Return type:

LoaderProtocol

tuxemon.graphics.strip_coords_from_sheet(sheet, coords, size)[source]

Strip specific coordinates from a sprite sheet.

Parameters:
  • sheet (Surface) – Sprite sheet.

  • coords (Sequence[Tuple[int, int]]) – Locations in the sheet.

  • size (Tuple[int, int]) – Size of the sprite.

Returns:

Sequence of stripped frames.

Return type:

Sequence[Surface]

tuxemon.graphics.strip_from_sheet(sheet, start, size, columns, rows=1)[source]

Strips individual frames from a sprite sheet.

Parameters:
  • sheet (Surface) – Sprite sheet.

  • start (Tuple[int, int]) – Start location in the sheet.

  • size (Tuple[int, int]) – Size of the sprite.

  • columns (int) – Number of columns.

  • rows (int) – Number of rows.

Returns:

Sequence of stripped frames.

Return type:

Sequence[Surface]