Contents

Appearance

Contents

Appearance

Appearance is the base class for Costume and Background

The Appearance class contains all the logic common to both, e.g. scaling and rotating images. The child classes contain the actions that are specific to these classes (e.g. certain overlays).

All actions performed on the images can be found in the class ImageRenderer

Warning

You do not need instances of this class.

  • If you work with tokens, then you use the child class Costume.

  • If you work with the board, you use the child class Background.

classDiagram Appearance <|-- Costume Appearance <|-- Background Token o-- Appearance class Token{ +add_costume() } class Appearance{ +add_image(str) } class Costume{ } class Background{ }

Appearance

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

Base class of token costumes and board backgrounds

The class contains all methods and attributes to display and animate images of the objects, render text on the images or display overlays.

Public Data Attributes:

font_size

is_textured

If True, the image is tiled over the background.

is_rotatable

If True, costume will be rotated with token direction

is_centered

orientation

If True, the image will be rotated by parent orientation before it is rotated.

is_flipped

Flips the costume or background.

is_scaled

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

is_upscaled

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

is_scaled_to_width

is_scaled_to_height

fill_color

coloring

Defines a colored layer.

transparency

Defines a transparency.

alpha

transparent, 255: visible If value < 1, it will be multiplied with 255.

is_animated

If True, the costume will be animated.

color

->See fill color

is_filled

Is token filled with color?

stroke_color

see border color

border_color

border color of token

border

The border-size of token.

Inherited from : py: class:AppearanceBase

counter

RELOAD_ACTUAL_IMAGE

LOAD_NEW_IMAGE

dirty

image

Performs all actions in image pipeline

text

Examples:

images

Public Methods:

set_font(font, font_size)

set_animation_speed(value)

flip(value)

get_text_width()

remove_last_image()

add_image([source])

Adds an image to the appearance

add_images(sources)

Adds multiple images to background/costume.

count_pixels_by_color(rect, color[, threshold])

Counts the number of pixels of a color under the appearance.

animate()

Animates the costume

after_animation()

the method is overwritten in subclasses costume and appearance

reset()

set_image(source)

Sets the displayed image of costume/background to selected index

to_colors_array()

Create an array from costume or background.

from_array(arr)

Create a background or costume from array.

fill(value)

Set default fill color for borders and lines

get_color(position)

find_color_in_rect(rect, color[, threshold])

draw(source, position, width, height)

draw_on_image(path, position, width, height)

draw_color_on_image(color, position, width, ...)

Inherited from : py: class:AppearanceBase

__init__()

after_init()

draw_shape_append(shape, arguments)

draw_shape_set(shape, arguments)

draw_image_append(surface, rect)

draw_image_set(surface, rect)

set_dirty([value, status])

get_image()

If dirty, the image will be reloaded.

add_images(sources)

Adds multiple images to background/costume.

add_image([source])

Adds an image to the appearance

set_image(source)

Sets the displayed image of costume/background to selected index

update()

Loads the next image, called 1/frame

__str__()

Return str(self).

register(method)

Register method for decorator.

Private Methods:

Inherited from : py: class:AppearanceBase

_before_transformation_pipeline()

_after_transformation_pipeline()


add_image(source=None)[source]

Adds an image to the appearance

Returns:

Index of the created image.

Examples

from miniworldmaker import *

board = Board()
token = Token()
costume = token.add_costume("images/1.png")
costume.add_image("images/2.png")

board.run()
Return type:

int

add_images(sources)[source]

Adds multiple images to background/costume.

Each source in sources paramater must be a valid parameter for Appearance.add_image

after_animation()[source]

the method is overwritten in subclasses costume and appearance

Examples

The token will be removed after the animation - This can be used for explosions.

from miniworldmaker import *

board = Board()
token = Token()
costume = token.add_costume("images/1.png")
costume.add_image("images/2.png")
costume.animate()
@costume.register
def after_animation(self):
    self.parent.remove()

board.run()
property alpha

transparent, 255: visible If value < 1, it will be multiplied with 255.

Examples

from miniworldmaker import *

board = Board(800,400)

t = Token((600,250))
t.add_costume("images/alien1.png")
t.costume.alpha = 50
t.width = 40
t.border = 1

board.run()
Textured image
Type:

defines transparency of Token

Type:

0

animate()[source]

Animates the costume

from miniworldmaker import *

board = Board(80,40)

robo = Token()
robo.costume.add_images(["images/1.png"])
robo.costume.add_images(["images/2.png","images/3.png","images/4.png"])
robo.costume.animation_speed = 20
robo.costume.is_animated = True
board.run()
property border

The border-size of token.

The value is 0, if token has no border

Returns:

int

Return type:

_type_

property border_color

border color of token

property color

->See fill color

property coloring

Defines a colored layer. Coloring is True or false. The color is defined by the attribute appearance.color

count_pixels_by_color(rect, color, threshold=(0, 0, 0, 0))[source]

Counts the number of pixels of a color under the appearance.

Parameters:
  • color – The color

  • threshold – The allowed deviation from the color splitted into r,g,b and alpha values.

Returns: The number of matching pixes

draw(source, position, width, height)[source]
draw_color_on_image(color, position, width, height)[source]
draw_on_image(path, position, width, height)[source]
fill(value)[source]

Set default fill color for borders and lines

property fill_color
find_color_in_rect(rect, color, threshold=(20, 20, 20, 20))[source]
flip(value)[source]
property font_size
from_array(arr)[source]

Create a background or costume from array. The array must be a numpy.ndarray, which can be created with ``.to_colors_array

Examples

Convert grey default-background to gradient

from miniworldmaker import *

board = Board()
arr = board.background.to_colors_array()
print(arr)
for x in range(len(arr)):
    for y in range(len(arr[0])):
        arr[x][y][0] = ((x +1 ) / board.width) * 255
        arr[x][y][1] = ((y +1 ) /board.width) * 255
board.background.from_array(arr)
board.run()


board.background.from_array(arr)
board.run()

Output:

converted image
get_color(position)[source]
get_text_width()[source]
property is_animated

If True, the costume will be animated.

from miniworldmaker import *

board = Board(80,40)

robo = Token()
robo.costume.add_images(["images/1.png"])
robo.costume.add_images(["images/2.png","images/3.png","images/4.png"])
robo.costume.animation_speed = 20
robo.costume.is_animated = True
board.run()
property is_centered
property is_filled

Is token filled with color?

property is_flipped

Flips the costume or background. The image is mirrored over the y-axis of costume/background.

Examples

Flips actor:

from miniworldmaker import *

board = Board()
token = Token()
token.add_costume("images/alien1.png")
token.height= 400
token.width = 100
token.is_rotatable = False
@token.register
def act(self):
    if self.board.frame % 100 == 0:
        print("flip")
        if self.costume.is_flipped:
            self.costume.is_flipped = False
        else:
            self.costume.is_flipped = True
board.run()
Textured image Textured image
property is_rotatable

If True, costume will be rotated with token direction

Examples

Costume of token t ignoriert aspect-ratio:

from miniworldmaker import *

board = Board(800,400)

t = Token((600,50))
t.add_costume("images/alien1.png")
t.costume.is_scaled = True
t.size = (140,80)
t.border = 1

board.run()

Output:

property is_scaled

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

Examples

Costume of token t ignoriert aspect-ratio:

from miniworldmaker import *

board = Board(800,400)

t = Token((600,50))
t.add_costume("images/alien1.png")
t.costume.is_scaled = True
t.size = (140,80)
t.border = 1

board.run()
Textured image
property is_scaled_to_height
property is_scaled_to_width
property is_textured

If True, the image is tiled over the background.

Examples

Texture the board with the given image:

from miniworldmaker import *

board = Board()
background = board.add_background("images/stone.png")
background.is_textured = True
board.run()
Textured image>

Set texture soize

from miniworldmaker import *

board = Board()
background = board.add_background("images/stone.png")
background.is_textured = True
background.texture_size = (15,15)
board.run()
Textured image
Type:

bool

property is_upscaled

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

Examples

Costume of token t ignoriert aspect-ratio:

from miniworldmaker import *

board = Board(800,400)

t = Token((600,50))
t.add_costume("images/alien1.png")
t.costume.is_scaled = True
t.size = (140,80)
t.border = 1

board.run()
property orientation

If True, the image will be rotated by parent orientation before it is rotated.

Examples

Both tokens are moving up. The image of t2 is correctly algined. t1 is looking in the wrong direction.

from miniworldmaker import *

board = TiledBoard()

t1 = Token((4,4))
t1.add_costume("images/player.png")
t1.move()

t2 = Token((4,5))
t2.add_costume("images/player.png")
t2.orientation = - 90
t2.move()

@t1.register
def act(self):
    self.move()

@t2.register
def act(self):
    self.move()

board.run()
Textured image
Type:

bool

remove_last_image()[source]
reset()[source]
set_animation_speed(value)[source]
set_font(font, font_size)[source]
set_image(source)[source]

Sets the displayed image of costume/background to selected index

Parameters:

source – The image index or an image.

Returns:

True, if image index exists

Examples

Add two images two background and switch to image 2

from miniworldmaker import *

board = Board()
background = board.add_background("images/1.png")
background.add_image("images/2.png")
background.set_image(1)
board.run()
Return type:

bool

property stroke_color

see border color

to_colors_array()[source]

Create an array from costume or background. The array can be re-written to appearance with .from_array

Examples

Convert a background image to grayscale

from miniworldmaker import *

board = Board(600,400)
board.add_background("images/sunflower.jpg")
arr = board.background.to_colors_array()

def brightness(r, g, b):
    return (int(r) + int(g) + int(b)) / 3

for x in range(len(arr)):
    for y in range(len(arr[0])):
        arr[x][y] = brightness(arr[x][y][0], arr[x][y][1], arr[x][y][2])

board.background.from_array(arr)
board.run()

Output:

converted image
Return type:

ndarray

property transparency

Defines a transparency.

If transparency``is ``True, the che transparency value is defined by the attribute appearance.alpha