Handeln#
So far, you have learned how to create worlds and place actors on them. Now we will set things in motion:
The act()
-Methode#
The act()
method controls the behavior of your world and all actors. It is called in every frame (every world.step
time unit) until the program ends.
Example: Moving Actor#
When you create an Actor
, you can register the act()
method with the @register
decorator:
from miniworlds import World, Actor
world = World(500,500)
world.add_background("images/sky.jpg")
player = Actor((90, 90))
player.add_costume("images/ship.png")
@player.register # registriert die Act-Methode. Dies ist notwendig, damit diese regelmäßig aufgerufen wird.
def act(self):
self.y = self.y - 1 # Bewegt den Actor in y-Richtung
world.run()
Explanation#
The
act()
method moves theplayer
one step up in each frame.New here is the command
self
: This allows an object to access itself. The commandself.y = self.y - 1
means that the object player decreases its “own” y-coordinate by 1.
Siehe auch
More details on methods and the use of self
can be found here.