Deutsch English |
YOU LEARN HERE... |
|
PROGRAM STRUCTURE SEQUENCE |
A computer program consists of a sequence of programme lines that are executed one after the other (as a sequence). So that you can use the beetle commands, you must import the mbglow module with from mbglow import *.
The commands are always written in English and always end with a parameter bracket. This can contain further information for the command. You must adhere exactly to upper/lower case. With forward() the beetle moves one step forwards in the current direction of movement. When the programme starts, the direction of movement points upwards. With left(90) the beetle turns 90 degrees to the left and with right(90) 90° to the right. However, this only becomes noticeable with the next forward() command. With your programme, the beetle draws the track shown next to it:
|
REPEAT |
Program: from mbglow import * makeGlow() repeat 4: forward() left(90) |
Make sure the indentation is correct! Program: from mbglow import * makeGlow() setPos(-2, -2) repeat 4: repeat 4: forward() right(90) |
FUNCTIONS (NAMED PROGRAMME BLOCKS) |
With named program blocks, called functions in Python, you can make your programmes clearer. You can also call functions several times. The use of functions is very important because you avoid having to write the same code several times in the programme (code duplication) and you can break problems down into smaller problems.
For the function name, choose an identifier that says something about what the function does. You may not use any special characters (spaces, umlauts, accents, punctuation marks, etc.). Always start the name with a small letter. In the following programme, you define a function square() that draws a square and then execute it. Program: from mbglow import * makeGlow() def square(): repeat 4: forward() forward() left(90) square()
Program: from mbglow import * makeGlow() def square(): repeat 4: forward() forward() left(90) setSpeed(80) repeat 4: square() right(90) |
VARIABLES |
To do this, you use the command v = v + 20 , which at first glance looks like a nonsensical equation, but tells the computer to get the existing value of v and add 20 to it, and then store the result in v again. The command showTrace(False) causes the beetle to leave no trace. Program: from mbglow import * makeGlow() def square(): repeat 4: forward() forward() right(90) v = 30 showTrace(False) setPos(-1, -1) repeat 4: setSpeed(v) square() v = v + 20 |
WHILE LOOP |
The while loop is one of the most important programme structures of all. It can generally be used for any type of repetition and is found in practically all programming languages. A while loop is introduced with the keyword while followed by a condition and a colon. As long as the condition is fulfilled, the commands in the following programme block are repeated. Colloquially, this would be expressed as follows: As long as the condition is true, execute ... The comparison operators < (smaller), <= (smaller-equal), > (greater) >= (greater-equal), == (equal), != (different) are usually used in the condition. Note the doubling of the equals sign when testing for equality (so that it is not confused with an assignment).
Program: from mbglow import * makeGlow() setSpeed(90) showTrace(False) a = 0 while a <= 360: forward() forward() back() back() left(45) a = a + 45
Program: from mbglow import * makeGlow() def square(): repeat 4: forward() forward() left(90) clear() setSpeed(90) showTrace(False) setPos(2, 0) left(45) while True: square() |
FOR IN RANGE LOOP |
With for x in range(-2, 3) , x runs through the values -2, -1, 0, 1, 2. The LEDs in the top row are switched on one after the other. With sleep(500) you can stop the programme execution for 500 milliseconds so that you can observe the process better. Program: from mbglow import * makeGlow() clear() for x in range(-2, 3): setPos(x, 2) sleep(500) |
IF - ELSE (SELECTION) |
You can use the selection to ensure that certain programme blocks are only executed under certain conditions. The selection is introduced with the keyword if, followed by a condition. The statements in the if are only executed if the condition is true, otherwise the statements after else are executed. The comparison operators >, >= , < , <= , == , != are usually used in the if condition. The statements in the if or else block must be indented. The else part can also be omitted.
Program: from mbglow import * makeGlow() right(90) x = 0 showTrace(False) while True: forward() x = x + 1 if x == 3: setPos(-2, 0) x = -2 In the next programme, the beetle should randomly move two steps forwards or two steps backwards, then stop briefly, delete the track and return to the starting position. It should repeat this movement 10 times.
Program: from mbglow import * from random import randint makeGlow() setSpeed(80) repeat 10: r = randint(0, 1) if r == 1: forward() forward() else: back() back() sleep(300) clear() setPos(0, 0) |
REMEMBER YOU... |
The basic programme structures are sequence, repetition and selection. In a sequence, the commands are processed one after the other. With a repetition, programme blocks are executed several times. In TigerJython you can use the repeat; while or for loops for this purpose. The selection with if and else means that certain instructions are only executed under certain conditions. The else part can also be omitted. |
TO SOLVE BY YOURSELF |
|