Our second program will be a bit more complicated, but not much. The goal for your buggle is simply to draw a house (a box), and hide inside. Check the objective world to see exactly what this means.
For that, you will need forward()
, left()
and
right()
. Use your brush to mark the ground as you walk. Start
marking the ground with brushDown()
, and stop it with
brushUp()
afterward (e.g., to enter your house without marking
its ground).
Programs are mainly suites of method calls, which are no more than a list of simple orders given to the machine. It is very similar to a recipe stating Melt the chocolate pieces, add sugar, cool the mix and serve. [!thelang] wants to have the instructions separated by semi-columns (;)[!python|scala] or by new lines[/!], and followed by parenthesis. Our little receipe would thus be written in the following way[!python|scala] (you can also add semi-columns at the end of the lines, but this is not mandatory)[/!].
meltTheChocolatePieces()[!java|c];[/!] addSugar()[!java|c];[/!] coolMix()[!java|c];[/!] serve()[!java|c];[/!]
Do not write it all on one line only even if the computer would accept it [!python|scala](provided that you don't omit the semi-columns)[/!]. This is a really bad idea, hindering the readability. Programs must be written for people to read, and only incidentally for machines to execute, as said Harold Abelson.
There is a limited set of instructions that buggles understand, and we will eventually learn how to define new ones. For now, there is a method for each button of the interactive control panel. To achieve the same effect than the forward button (making the buggle moving one step forward), you need to write the following in the editor:
[!java|scala|python]forward()[!java];[/!][/!][!c]stepForward();[/!]
Likewise, to achieve the same effect than the [!java|scala|python]backward[/!][!c]stepBackward[/!], left and right buttons, you need to use respectively:
backward()[!java|c];[/!] left()[!java|c];[/!] right()[!java|c];[/!]
The mark button is a bit particular, since it correspond to two methods: the first one moves the pen up while the second moves it down.
brushUp()[!java|c];[/!] brushDown()[!java|c];[/!]
The buggle offers other methods, that are presented from the "Help/about this world" menu and will be introduced on need.