Appearance

Contents

Appearance#

Appearance is the parent class of Background and Costume.

Backgrounds and costumes are appearances as well, so they inherit all attributes and methods defined in this class.

API Reference#

class miniworlds.appearances.appearance.Appearance(*args, **kwargs)[source]#

Base class for actor costumes and world backgrounds.

Appearance is the parent class of both Costume and Background. You normally access it through actor.costume or world.background.

Examples

actor.costume.add_image("images/player.png")
actor.costume.fill_color = (255, 0, 0)
actor.costume.border = 2
actor.costume.is_animated = True
actor.costume.alpha = 128
LOAD_NEW_IMAGE = 2#
RELOAD_ACTUAL_IMAGE = 1#
add_image(source)[source]#

Add an image source and return its index.

Return type:

int

add_images(sources)[source]#

Add multiple image sources.

Each source in sources must be a valid input for add_image.

Examples

actor.costume.add_images(["images/1.png", "images/2.png"])
after_animation()[source]#

Hook called after a non-looping animation finishes.

Examples

@costume.register
def after_animation(self):
    self.parent.remove()
after_init()[source]#

Finalize initialization after the metaclass constructor hook.

property alpha#

Transparency value of the appearance.

Use values from 0 to 255: - 0 means fully transparent - 255 means fully visible

If the value is between 0 and 1, it is interpreted as a normalized opacity and converted to the 0..255 range.

animate(loop=False)[source]#

Start appearance animation.

Parameters:

loop – Whether the animation should repeat.

Examples

actor.costume.add_images(["images/1.png", "images/2.png"])
actor.costume.animate(loop=True)
animation_length#
property animation_speed#

Frames between animation steps.

property border#

Border width in pixels.

A value of 0 means no border.

Type:

int

property border_color#

Border color.

Type:

tuple

property color#

Alias for fill_color.

Type:

tuple

property coloring#

Optional color layer.

Examples

actor.costume.coloring = (255, 0, 0)
Type:

tuple | None

counter = 0#
draw(source, position, width, height)[source]#

Draw an image source at a local position.

draw_color_on_image(color, position, width, height)[source]#

Queue drawing a colored rectangle onto the appearance image.

draw_image_append(surface, rect)[source]#

Append a pre-rendered surface draw command.

draw_image_set(surface, rect)[source]#

Replace image draw commands with one surface draw command.

draw_images#
draw_on_image(path, position, width, height)[source]#

Queue drawing an image file onto the appearance image.

draw_shape_append(shape, arguments)[source]#

Append a shape draw command to the render queue.

draw_shape_set(shape, arguments)[source]#

Replace shape draw commands with a single command.

draw_shapes#
fill(value)[source]#

Set default fill color for borders and lines

property fill_color#

Primary fill color for shape-based rendering.

flip(value)[source]#

Convenience wrapper to set is_flipped.

font_manager#
property font_size#

Current font size used for text rendering.

from_array(arr)[source]#

Replace the appearance image from a color array.

Parameters:

arr – NumPy color array, usually created with to_colors_array().

Examples

arr = world.background.to_colors_array()
arr[0][0] = (255, 0, 0)
world.background.from_array(arr)
get_color(position)[source]#

Return the color at a local pixel position.

get_image()[source]#

If dirty, the image will be reloaded. The image pipeline will be processed, defined by “set_dirty”

abstract get_manager()[source]#

Implemented in subclasses Costume and Background

get_modes()[source]#

Return all mode flags as a dictionary.

get_rect()[source]#

Return the local rectangle of the rendered image.

id#
image_manager: ImageManager#
property images#

List of image surfaces managed by this appearance.

initialized#
property is_animated#

Whether the appearance animates through its images.

Examples

actor.costume.add_images(["images/1.png", "images/2.png"])
actor.costume.animation_speed = 20
actor.costume.is_animated = True
Type:

bool

property is_centered#

Whether drawing operations are centered on the parent position.

property is_filled#

Whether shapes are rendered filled instead of outlined.

property is_flipped#

Whether the image is mirrored horizontally.

Examples

actor.costume.is_flipped = True
Type:

bool

property is_rotatable#

Whether the image rotates with the parent direction.

Type:

bool

property is_scaled#

Scales the token to parent-size without remaining aspect-ratio.

property is_scaled_to_height#

Whether the image is scaled to parent height and keeps aspect ratio.

property is_scaled_to_width#

Whether the image is scaled to parent width and keeps aspect ratio.

property is_textured#

Whether the image is tiled over the parent area.

Examples

background = world.add_background("images/stone.png")
background.is_textured = True
background.texture_size = (15, 15)
Type:

bool

property is_upscaled#

If True, the image will be upscaled remaining aspect-ratio.

last_image#
loop#
property orientation#

Orientation offset applied before parent rotation.

Examples

actor.costume.orientation = -90
Type:

float

parent#
register(method)[source]#

Register method for decorator. Registers method to actor or background.

remove_last_image()[source]#

Remove the most recently added image.

set_animated(value)[source]#

Enable or disable frame-based animation.

set_dirty(value='all', status=1)[source]#

Mark pipeline stages as dirty so the image is re-rendered.

set_filled(value)[source]#

Set whether shapes are rendered filled.

set_image(source)[source]#

Set the displayed image.

Return type:

bool

Parameters:

source – Image index, appearance, or color tuple.

Returns:

True if the image index exists.

Examples

background.add_image("images/1.png")
background.add_image("images/2.png")
background.set_image(1)
set_mode(**kwargs)[source]#

Set multiple appearance mode flags at once.

Supported keyword arguments include mode, texture_size, and animation_speed.

property stroke_color#

Alias for border_color.

Type:

tuple

surface_loaded#
property texture_size#

Texture tile size used when is_textured is enabled.

to_colors_array()[source]#

Return the appearance image as a color array.

Return type:

ndarray

Returns:

A NumPy array containing color data.

Examples

arr = world.background.to_colors_array()
arr[0][0] = (255, 0, 0)
world.background.from_array(arr)
transformations_manager#
property transparency#

Whether alpha transparency is enabled.

The actual opacity is controlled by alpha.

Type:

bool

abstract property world: World#

Implemented in subclasses Costume and Background