Branches#
You need branches whenever you want to check conditions and the program flow should depend on them.
Erstes Beispiel#
If you want to check in your game, for example, whether a certain score has been reached, you can do this with an instruction
if points > 100:
print("You have won!")
General syntax#
In der Regel ist dies die Syntax für Verzweigungen:
if <Bedingung>:
<Code Block>
Boolean-Ausdrücke#
A condition is an expression that can have the value True
or False
- Such expressions are called boolean expressions.
The simplest boolean expressions are True
and False
. You usually get more expressions with comparisons, for example:
10 < 100 # True
110 < 100 # False
x < 10 # True, if x < 10
"a" == "b" # False
3 == 4 # False
"ab" == "ab" # True
Expressions can be arbitrarily complex and contain variables.
Warnung
Warning: Bei Vergleichen verwendet man immer ein doppeltes Gleichheitszeichen anstelle eines einfachen Gleichheitszeichen
Vergleiche#
Here are some comparisons you can use:
<
: Kleiner als<=
: Kleiner oder gleich==
: Gleich>=
: Größer als oder gleich>
Größer als
Code-Blöcke#
If you want to execute multiple instructions depending on the condition, you can do this with the help of code blocks. Code blocks are always indented equally, and all instructions that are indented accordingly are
Example:
if points > 100:
print("You have won!")
print("Congratulations")
print("The game is over")
Regardless of the score, the last line of code will be executed in any case. However, the two indented lines will only be executed if the score is greater than 100.
Elif und Else#
Mit elif und else kannst du Alternativen einbauen. Dies geht z.B. so:
if points > 100:
print("You have won!")
print("Congratulations")
elif points > 50:
print("you lost by a narrow margin")
else:
print("you have clearly lost)
The general syntax is:
if <Bedingung>:
<Code Block>
elif <Bedingung>:
<Code Block>
else <Bedingung>:
<Code Block>
Both elif and else can be omitted. Multiple elif blocks are also possible.
Detailliertes Beispiel#
A rectangle should move from right to left. When it reaches the left side, it should reappear on the far right.
The first variant looks like this:
from miniworlds import *
world = World(300, 200)
rect = Rectangle((280,120), 20, 80)
@rect.register
def act(self):
rect.x -= 1
world.run()
The crucial part is still missing.
This can be formulated as follows:
If the x-coordinate reaches the value 0, move the rectangle back to the right
Dies kann man direkt in Python übersetzen:
from miniworlds import *
world = World(300, 200)
rect = Rectangle((280,120), 20, 80)
@rect.register
def act(self):
rect.x -= 1
if rect.x == 0:
rect.x = 280
world.run()
Another example - A simple Flappy Bird#
We want to program a kind of (simple) Flappy Bird.
Our main character should be a ball that moves upwards when a key is pressed. We can achieve this as follows:
from miniworlds import *
world = World(300, 200)
rect = Rectangle((280,120), 20, 80)
ball = Circle((20,50),20)
velocity = 1
@rect.register
def act(self):
rect.x -= 1
if rect.x == 0:
rect.x = 280
@ball.register
def act(self):
global velocity
self.y += velocity
if world.frame % 10 == 0:
velocity += 1
world.run()
The ball falls and falls faster and faster.
In der Zeile:
if world.frame % 10 == 0:
velocity += 1
the speed at which the ball falls is increased. In the first step, the ball should be able to move upwards when a key is pressed.
from miniworlds import *
world = World(300, 200)
rect = Rectangle((280,120), 20, 80)
ball = Circle((20,50),20)
velocity = 1
@rect.register
def act(self):
rect.x -= 1
if rect.x == 0:
rect.x = 280
@ball.register
def act(self):
global velocity
self.y += velocity
if world.frame % 10 == 0:
velocity += 1
@ball.register
def on_key_down(self, key):
global velocity
velocity = -2
world.run()
Kollisionen#
Now we want not only to compare positions, but also the relative location of objects to each other.
We can use different sensor
methods for that.
This works, for example, like this:
from miniworlds import *
world = World(300, 200)
rect = Rectangle((280,120), 20, 80)
ball = Circle((20,50),20)
velocity = 1
@rect.register
def act(self):
rect.x -= 1
if rect.x == 0:
rect.x = 280
@ball.register
def act(self):
global velocity
self.y += velocity
if world.frame % 10 == 0:
velocity += 1
actor = self.detect_actor()
if actor == rect:
self.world.stop()
@ball.register
def on_key_down(self, key):
global velocity
velocity = -2
world.run()
The logic is in the following lines:
actor = self.detect_actor()
if actor == rect:
self.world.stop()
The first line checks with a sensor which actor was found at its own position (and returns the first found actor). Then the actor found in this way is compared with the rectangle. If these are the same objects, then the game is aborted.
This is what the Flappy Bird game looks like now: