Movement#
In diesem Kapitel setzen wir Elemente in Bewegung. Dabei helfen uns auch Variablen.
Simple Bewegungen#
You can achieve a simple movement by changing the x and y attributes of an object.
from miniworlds import *
import random
world = World((100,100))
c = Circle((0,50), 20)
@world.register
def act(self):
c.x = c.x + 1
world.run()
Output:
Der Modulo-Operator#
The modulo operator is particularly helpful for repetitive movements.
Python kennt 3 Arten von Divisionen:
13 / 3 liefert das Ergebnis 4.3333333333 13 // 3 liefert das ganzzahlige Ergebnis 4 13 % 3 liefert den Rest der Division 13 / 3, also 1 zurück.
That the remainder can never be greater than the dividend can help us with animations:
from miniworlds import *
import random
world = World((100,100))
c = Circle((0,50), 20)
x = 0
@world.register
def act(self):
global x
c.x = x % 100
x = x + 1
world.run()
The variable x keeps counting upwards, since the remainder of the division of x and 100 can never be greater than 100, the point moves back again.