Kostüme und Animationen#

Each actor has one or more costumes. These costumes consist of several images that can be used for animations.

Bemerkung

If you search for the attributes and methods of the Costume class in the API, you will find them under the Appearance class. Appearance is the superclass of Costume and Background, as backgrounds and costumes share many properties.

Add a costume#

With the following function, you can add a new costume to an actor:

self.add_costume("images/image.jpg")

If no costume is available yet, this will automatically become the actor’s first costume.


Add more pictures to a costume#

To expand a costume, you can add additional images using the add_image method:

self.costume.add_image("images/image_2.jpg")

Alternatively, you can also add a list of images at the same time:

self.costume.add_images(["images/image_1.jpg", "images/image_2.jpg"])

Animationen#

2D-Animationen funktionieren ähnlich wie ein Daumenkino:

By rapidly changing the images of an actor one after the other, the impression of movement is created.

Kostüme für den Actor

To create an animation, you must first add several images to a costume (see above).

Then you can start the animation with the command costume.animate(). With the parameter loop, you can specify whether the animation should be repeated:

my_actor.costume.animate()
robo.costume.animate(loop = True) # Endlossanimation

Example:#

import miniworlds 

world = miniworlds.World(80, 80)

robot = miniworlds.Actor()
robot.size = (80, 80)
robot.add_costume("images/drive1.png")
robot.costume.add_image("images/drive2.png")
robot.costume.animate()  # Animation aktivieren
robot.costume.loop = True         # Endlos-Schleife der Animation
world.run()

Switch between costumes#

To switch between different costumes, you can use the switch_costume method:

self.switch_costume()

This method switches to the next costume in the list. You can optionally also provide a number as a parameter to jump directly to a specific costume:

self.switch_costume(1)  # Wechselt zum ersten Kostüm