Funktionen#

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:

from miniworlds import *

world = 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()

4 rote Kreise

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:

from miniworlds import *

world = World(80, 80)

def create_circle(x, y):
    c = 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.

  • Die Funktion hat den Namen create_circle

  • The function receives the values x and y 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: Defining Functions#

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 is the name and parameters. The name of this function is create_actor, the parameters are x and y. Parameters are needed to pass additional information to the function. In the example, the information is passed about where the object should be created.

  • 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>)

This can look like this, for example:

create_actor(4,2)

Here an actor is created at position x=4 and y=2.

Draw complex figures#

With the help of functions, you can draw complex figures:

from miniworlds import *

world = World(400, 220)

def create_face(x, y):
    c = Circle((x, y), 40)
    c.color = (255,255,0)
    c = Circle((x + 15, y - 10), 10)
    c = 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 created at different locations.

Gesichter

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.

from miniworlds import *
import random

world = World()
world.add_background((80,180,255))
def raindrop(x,y):
    c = 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_not_detecting_world(self):
        self.remove()
        
@world.register
def act(self):
    if world.frame % 5 == 0:
        raindrop(random.randint(0,400),0)
    

world.run()