Movements#

Basics#

Before we dive deeper into the movement functions, here is a brief review of the key concepts:

  • With self.direction, self.x, self.y, and self.position, you can directly control the position and orientation of an actor.

Additionally, there are special methods that allow you to move an actor straight or in specific directions.

Die Funktion move()#

The move() method moves your actor in the direction they are currently facing (based on the current direction). If you change the direction beforehand, the movement automatically adjusts to the new orientation.

@player.register
def act(self):
    self.direction = "right"  # Alternativ kann auch ein Winkel, z.B. 90, verwendet werden
    self.move()

turn_left() und turn_right()#

With turn_left() and turn_right(), you can rotate the actor by a certain number of degrees to the left or right.

  • player.turn_left(degrees): Dreht den Spieler um degrees Grad nach links.

  • player.turn_right(degrees): Dreht den Spieler um degrees Grad nach rechts.

Example:#

import miniworlds 

world = miniworlds.World(400, 400)
world.add_background("images/grass.jpg")
player = miniworlds.Actor((100, 100))
player.add_costume("images/player.png")

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

@player.register
def on_key_down_a(self):
    self.turn_left(30)

@player.register
def on_key_down_d(self):
    self.turn_right(30)

world.run()

move_in_direction()#

As an alternative to the standard movement, you can move the actor in any direction with move_in_direction() by specifying an angle.

Example: Diagonal movement upwards#

import miniworlds 

world = miniworlds.World()
world.add_background("images/grass.jpg")
player = miniworlds.Actor((100, 100))
player.add_costume("images/player.png")

@player.register
def act(self):
    self.move_in_direction(45)

world.run()

Example: Movement towards the mouse pointer#

The following example shows how the actor follows the position of the mouse pointer using move_in_direction():

import miniworlds 

world = miniworlds.World(400, 400)
world.add_background("images/grass.jpg")
player = miniworlds.Actor()
player.add_costume("images/player.png")
player.orientation = -90

@player.register
def act(self):
    self.move_in_direction(self.world.get_mouse_position())

world.run()