Table of Contents
- The Grammar of TurtleScript
- Mathematical, boolean and comparing operators
- Assignment of variables
- Controlling execution
- Create your own commands with “learn”
This is the reference for KTurtle's TurtleScript. In the first section of this chapter have a look at some aspects of the grammar of TurtleScript programs. The second section deals exclusively with mathematical operators, boolean (true/false) operators and comparison operators. The third section is basically a giant list of all commands explaining them one-by-one. Section four explains how to assign values to variables. Finally we explain how to arrange the execution of commands with execution controlling statements in section five and how to create you own commands with learn in section six.
As in any language, TurtleScript has different types of words and symbols. In English we distinguish verbs (like 'to walk' or 'to sing') and nouns (like 'sister' or 'house'), they are used for different purposes. TurtleScript is a programming language, it is used to instruct KTurtle what to do.
In this section some of TurtleScript's different types of words and symbols are briefly explained. We explain comments, commands and the three different kinds of literals: numbers, strings and boolean (true/false) values.
A program consists instructions that are executed when the program is run and so called comments. Comments are not executed, KTurtle simply ignores them when executing your program. Comment are there for other programmers to make them understand your program better. Everything that follows on a
# symbol is considered a comment in TurtleScript. For example this little program that does nothing:
# this little program does nothing, it is only a comment!
It is a bit useless but it explain the matter well.
Comments get very useful when the program gets a little bit more complex. It can help to give some advice to other programmers. In the following program you see comments being used together with the print command.
# this program has been made by Cies Breijs. print "this text will get printed on the canvas" # the previous line is not a comment, but the next line is: # print "this text will not get printed!"
The first line describes the program. The second line is executed by KTurtle and prints
this text will get printed on the canvas on the canvas. The third line is a comment. And the forth line is a comment that contains a piece of TurtleScript, if the
# symbol would be removed on the fourth line the print statement will we executed by KTurtle. Programmers say: the print statement on the fourth line is 'commented out'.
Commented lines are highlighted with light gray in the code editor.
Using commands you tell the turtle or KTurtle to do something. Some commands need input, some give output.
# forward is a command that needs input, in this case the number 100: forward 100
Some commands like e.g.
go need more than one input value. Multiple values have to be separated using the
, character (comma).
For a detailed overview of all commands that KTurtle supports go here. Built-in commands are highlighted in dark blue
Most likely you already know quite a bit about numbers. The way numbers are used in KTurtle is not much different from spoken language, or math.
We have the so called natural numbers:
5, etc. The negative numbers:
-3, etc. And the numbers with decimals, or dot-numbers, for example:
. character (dot) is used as decimal separator.
First an example:
print "Hello, I'm a string."
In this example
"Hello, I'm a string." is a string. Strings start and end with the
" mark, by these marks KTurtle knows it is a string.
There are only two boolean values:
false. Sometimes they are also called: on and off, yes and no, one and zero. But in TurtleScript we call them, always,
false. Have a look at this piece of TurtleScript:
$a = true
Often boolean values are the result of a comparison operator, like in the following piece of TurtleScript:
$answer = 10 > 3
$answer is set to
10 is larger than
false, are highlighted with dark red.