Blocks
Block reference
Every block in the toolbox, what it does, and the Python it writes.
This page is for looking something up: every block, in toolbox order, with the line it writes. The pages before it explain the same blocks at length, and each section here says which one.
The toolbox has two halves. APEX GD240 at the top is the drone’s own category — everything that makes the aircraft move, look or listen. Everything below it, under PROGRAMMING, is ordinary programming: Logic, Loops, Timing, Math, Text, Lists, Variables and Functions. These are the standard blocks, the same ones used by Scratch-style editors everywhere, so a student who has met them before already knows them.
The shape of a program
Before the tables, the map. Nearly every flight has this shape, and “which block do I need?” is usually a question about where you are on it.
The two ends never change: active fly then take off at the top, land at
the bottom. The middle is yours.
APEX GD240
In toolbox order. Explained at length in The drone’s blocks.
| Block | What it does | What it writes |
|---|---|---|
| active fly | Wakes the motors and centres the controls. Nothing else works until this has run. | drone.active_fly() |
| take off | Lifts off to about a metre and holds. | drone.takeoff() |
| land | Comes down and settles. | drone.land() |
| fly Front / Back / Left / Right speed 1 / 2 / Stop | Starts flying that way and keeps going until something stops it. Speed 1 is 0.5 m/s, speed 2 is 1 m/s. | drone.fly('forward', 1)drone.fly_stop() |
| rotate left / right speed 1 / 2 / Stop | Starts turning on the spot and keeps turning until Stop. Speed 1 is about 29° a second, speed 2 about 57° — a right-angle corner is a little over three seconds at speed 1. | drone.rotate('right', 1)drone.rotate_stop() |
| throttle 0–6 | How hard the motors push. 0 is idle, 3 holds height, 6 climbs hard. Arrives set to 0. | drone.set_throttle(3) |
| set headless On / Off | With headless on, “forward” means away from the pilot instead of out of the drone’s nose. Silent in the simulator. | drone.set_headless(True) |
| emergency stop | Cuts the motors instantly. The drone drops. | drone.emergency_stop() |
| take photo | One photo from the drone camera. Silent in the simulator. | drone.take_photo() |
| start recording | Starts recording video. Silent in the simulator. | drone.start_recording() |
| stop recording | Stops recording. Silent in the simulator. | drone.stop_recording() |
| get battery level | The charge left, right now. A value block — drop it into a hole. | drone.get_battery() |
Four of the twelve are silent in the simulator: they are real commands on an APEX GD240, and in a simulator session they write a line into the terminal and do nothing else. Beyond the twelve explains why, and what else is in that position.
Timing
| Block | What it does | What it writes |
|---|---|---|
| wait n seconds | Holds the program still. Whatever the drone was already doing, it keeps doing. 0.1 to 60. | time.sleep(2) |
| wait ⟨value⟩ seconds | The same, but the number comes from a value block — a variable, or a sum. | time.sleep(x) |
wait is the block that turns switches into steps. It appears in more programs
than any other block in the toolbox. See Loops and Timing.
Loops
| Block | What it does | What it writes |
|---|---|---|
| repeat ⟨n⟩ times / do | Runs the blocks inside it over and over, a fixed number of times. | for count in range(4): |
| repeat while / until ⟨test⟩ do | Keeps going as long as the test holds — or until it does. | while x:while not x: |
| count with i from ⟨1⟩ to ⟨10⟩ by ⟨1⟩ | The same as repeat, except the program can see which pass it is on. | for i in range(1, 11): |
| for each item i in list ⟨list⟩ | One pass per item in a list. | for i in mylist: |
| break out of loop / continue with next iteration | Leaves the loop early, or skips to the next pass. | breakcontinue |
Logic
| Block | What it does | What it writes |
|---|---|---|
| if ⟨test⟩ do | Runs the blocks inside only when the test is true. Click the gear to add else. | if x: |
| ⟨a⟩ = ⟨b⟩ | Compares two values. The dropdown also has ≠ < ≤ > ≥. | a == b |
| ⟨a⟩ and ⟨b⟩ | True when both are true. The dropdown also has or. | a and b |
| not ⟨a⟩ | Turns true into false and back. | not a |
| true / false | A value you can drop into a test. | True |
| null | Nothing, deliberately. | None |
| test ⟨a⟩ if true ⟨b⟩ if false ⟨c⟩ | Picks one of two values. | b if a else c |
Math
| Block | What it does | What it writes |
|---|---|---|
| number | Any number. Click it and type. | 123 |
| ⟨a⟩ + ⟨b⟩ | Arithmetic. The dropdown also has − × ÷ ^. | a + b |
| square root ⟨a⟩ | And absolute value, negation, ln, log10, e^, 10^. | math.sqrt(a) |
| sin ⟨a⟩ | And cos, tan and the inverses. In degrees. | math.sin(a / 180.0 * math.pi) |
| π | And e, φ, √2, √½, ∞. | math.pi |
| ⟨a⟩ is even | Even, odd, prime, whole, positive, negative, divisible by. | a % 2 == 0 |
| round ⟨a⟩ | Also round up and round down. | round(a) |
| sum of list ⟨list⟩ | And min, max, average, median, modes, standard deviation, random item. | sum(mylist) |
| remainder of ⟨a⟩ ÷ ⟨b⟩ | What is left over. | a % b |
| constrain ⟨a⟩ low ⟨b⟩ high ⟨c⟩ | Keeps a number inside a range. Useful for speeds. | min(max(a, b), c) |
| random integer from ⟨a⟩ to ⟨b⟩ | A different whole number each run. | random.randint(a, b) |
| random fraction | A different number between 0 and 1 each run. | random.random() |
| atan2 of X: ⟨a⟩ Y: ⟨b⟩ | The angle to a point, in degrees. | math.atan2(b, a) / math.pi * 180 |
Logic and Math both get a page of their own: Logic and Math.
Text
| Block | What it does | What it writes |
|---|---|---|
| ” ” | A piece of text. | 'hello' |
| print ⟨a⟩ | Writes a line in the terminal at the bottom of the screen. | print('hello') |
| create text with | Joins pieces of text together. Gear to add more. | ''.join([a, b]) |
| to x append text ⟨a⟩ | Adds to the end of a text variable. | x = str(x) + str(a) |
| length of ⟨a⟩ | How many characters. | len(a) |
| is empty ⟨a⟩ | True when there are none. | not len(a) |
| in text ⟨a⟩ find first occurrence of ⟨b⟩ | Where a piece appears, counting from 1. | a.find(b) + 1 |
| in text ⟨a⟩ get letter # ⟨n⟩ | One character. Also first, last, random. | a[0] |
| in text ⟨a⟩ get substring from … to … | A run of characters. | a[1:3] |
| to UPPER CASE ⟨a⟩ | Also lower case and Title Case. | a.upper() |
| trim spaces from both sides of ⟨a⟩ | Also one side only. | a.strip() |
| count ⟨a⟩ in ⟨b⟩ | How many times a piece appears. | b.count(a) |
| replace ⟨a⟩ with ⟨b⟩ in ⟨c⟩ | Substitution. | c.replace(a, b) |
| reverse ⟨a⟩ | Backwards. | a[::-1] |
print is the block to reach for when a program is not doing what it should. Put one between two flight blocks and you can see how far the program got.
Lists
| Block | What it does | What it writes |
|---|---|---|
| create list with | A list of values in order. Gear to add more. | [a, b, c] |
| create list with item ⟨a⟩ repeated n times | A list of the same thing. | [a] * n |
| length of ⟨list⟩ | How many items. | len(x) |
| is empty ⟨list⟩ | True when there are none. | not len(x) |
| in list ⟨list⟩ find first occurrence of ⟨a⟩ | Which position, counting from 1. | first_index(x, a), plus a helper the editor writes |
| in list ⟨list⟩ get # ⟨n⟩ | One item. Also first, last, random, and get-and-remove. | x[0] |
| in list ⟨list⟩ set # ⟨n⟩ to ⟨a⟩ | Replaces an item. Also insert and append. | x[0] = a |
| in list ⟨list⟩ get sub-list from … to … | A run of items. | x[1:3] |
| make list from text ⟨a⟩ with delimiter ⟨b⟩ | And back the other way. | a.split(',') |
| sort ⟨list⟩ | Numeric, alphabetic or ignoring case; ascending or descending. | a sorted copy of the list |
| reverse ⟨list⟩ | Backwards. | list(reversed(x)) |
Text and Lists are explained at length in Lists and Text.
Variables and Functions
These two categories start empty, because what goes in them is yours. See Variables and Functions.
Variables — press Create variable…, give it a name, and three blocks
appear. A variable is a box with a name on it; speed is a good name, x is
not.
| Block | What it writes |
|---|---|
| set speed to ⟨a⟩ | speed = a |
| change speed by ⟨a⟩ | speed = (speed if isinstance(speed, Number) else 0) + a |
| speed | speed |
Functions — press Create function… to give a group of blocks a name. Then
you can use that name as a single block, as many times as you like. A function
called fly one side — start, wait, stop, turn, wait, stop — leaves the flight
itself four blocks long, and turns into a hexagon the day you change your mind:
repeat 6, and one number in the turn.
| Block | What it writes |
|---|---|
| to fly one side do | def fly_one_side(): |
| fly one side | fly_one_side() |