Concept: Funktionen, Parameter und Rückgabewert#
You have used methods so far that belong to a World or an Actor, e.g., on_setup
, act
.
Now you are learning to create your own functions. Functions are subprograms
that allow you to reuse code instead of writing it over and over again.
Erstes Beispiel#
You want to create four red circles, as shown in the following image:
Das geht zum Beispiel so:
import miniworlds
world = miniworlds.World(80, 80)
c1 = Circle((20,20), 20)
c1.color = (255, 0, 0)
c2 = Circle((60,20), 20)
c2.color = (255, 0, 0)
c3 = Circle((60,60), 20)
c3.color = (255, 0, 0)
c4 = Circle((20,60), 20)
c4.color = (255, 0, 0)
world.run()
The program is correct. However, if all four circles are supposed to be green, then you need to change the program in four places.
Instead, you can outsource the creation of the circle and the setting of the color to subroutines:
import miniworlds
world = World(80, 80)
def create_circle(x, y):
c = miniworlds.Circle((x, y), 20)
c.color = (255, 0, 0)
create_circle(20, 20)
create_circle(60, 20)
create_circle(60, 60)
create_circle(20, 60)
world.run()
Here, a function create_circle
was defined.
The function is called create_circle
The function receives the values
x
andy
as parameters - At this point, the circle should be created.Im Methodenkörper wird zuerst ein Kreis erstellt und diesem anschließend eine Farbe zugewiesen.
The function is called using its name and passing the arguments (e.g. 20, 20).
General: Definieren von Funktionen#
Often you want to automate complicated things, such as creating actors.
This is done by defining functions yourself - This is generally done like this:
def function_name(<parameter>):
<code_block>
z.B.:
def create_circle(x, y):
c = Circle((x, y), 20)
c.color = (255, 0, 0)
Your function consists of a signature and a function body.
The signature is the first line of the function. It contains all the information you need to call the function. This includes the name and parameters. The name of this function is
create_actor
, and the parameters arex
andy
. Parameters are needed to pass additional information to the function. In the example, the information about where the object should be created is passed.The function body is a block of code. It contains all the commands that are executed sequentially when the function is called. Here, a circle is first created when the function is called, and then the color of the circle is set.
The commands are processed from top to bottom when the function is called.
Calling functions#
A function is called using the name. You pass the arguments defined as parameters to the function.
In der Regel schreibt man: method_name(<parameter>)
Dies kann z.B. so aussehen:
create_actor(4,2)
Here, an actor is created at position x=4
and y=2
.
Draw complex figures#
Mit Hilfe von Funktionen kannst du komplexe Figuren zeichnen:
import miniworlds
world = miniworlds.World(400, 220)
def create_face(x, y):
c = miniworlds.Circle((x, y), 40)
c.color = (255,255,0)
c = miniworlds.Circle((x + 15, y - 10), 10)
c = miniworlds.Circle((x - 15, y - 10), 10)
a = Arc((x, y + 20), 40, 20, 180, 360)
a.center = a.position
a.color = (255, 0, 0)
create_face(60, 60)
create_face(260, 60)
create_face(160, 160)
world.run()
In the function create_face
, a face is created. This can then be generated in different places.
Actors automatisch erstellen#
Mit Hilfe von Funktionen kannst du das Erstellen von Actors abkürzen:
10 Akteure werden hier mit 10 Befehlen angelegt. Ohne Funktionen hättest du 30 Befehle benötigt.
import miniworlds
world = miniworlds.TiledWorld()
world.rows = 8
def create_actor(x, y):
t = Actor()
t.position = (x,y)
t.add_costume("images/player.png")
def create_wall(x, y):
t = Actor()
t.position = (x,y)
t.add_costume("images/wall.png")
create_actor(4,2)
create_wall(4,4)
create_wall(5,4)
create_wall(6,4)
create_wall(6,3)
create_wall(6,2)
create_wall(6,1)
create_wall(5,1)
create_wall(4,1)
create_wall(3,1)
world.run()
Ausgabe:
Register properties and methods.#
The following program automatically creates “raindrops”.
In the raindrop function, not only are properties set for each drop, but methods are also registered.
import miniworlds
import random
world = miniworlds.World()
world.add_background((80,180,255))
def raindrop(x,y):
c = miniworlds.Circle((x, y), random.randint(10, 20))
speed = random.randint(1,5)
c.color = (0,0,random.randint(100,255), 100)
c.static = True
@c.register
def act(self):
self.move_down(random.randint(1,3))
@c.register
def on_detecting_not_on_world(self):
self.remove()
@world.register
def act(self):
if world.frame % 5 == 0:
raindrop(random.randint(0,400),0)
world.run()
Return values#
The previous functions executed a subroutine and possibly received information via parameters.
With the help of return values, you can also return information.
In the simple case, it looks like this:
def is_even(x):
if x % 2 == 0:
return True
else:
return False
print(is_even(4))
In der letzten Zeile passiert folgendes: Bevor die Funktion print
ausgeführt werden kann, muss die Funktion is_even
ausgewertet werden. Das Ergebnis der Funktion (Hier: True
) wird dann anstelle des Funktionsaufrufs eingesetzt.
The following program randomly creates circles. If they are to the left of the center, they are colored red, otherwise white.
import miniworlds
import random
world = miniworlds.World(400, 50)
def is_left(obj):
if obj.x <= 200:
return True
else:
return False
for i in range(20):
x = random.randint(0,400)
y = 25
c = miniworlds.Circle((x, y), 10)
if is_left(c):
c.color = (255,0,0)
world.run()