docs.kde.org
TurtleScript Programming Reference
Prev
Next

Chapter 4. TurtleScript Programming Reference

Table of Contents

Different Instruction Types
Commands
Numbers
Strings
Names
Assignments
Math Symbols
Questions
Question Glue-Words
Comments
Commands
Moving the turtle
Where is the turtle?
The turtle has a pen
Commands to control the canvas
Commands to clean up
The turtle is a sprite
Can the turtle write?
A command that rolls dice for you
Input and feedback though dialogs
Containers
Variables: number containers
Containers that contain text (strings)
Can the Turtle do math?
Asking questions, getting answers...
Questions
Question Glue
Controlling execution
Have the turtle wait
Execute "if"
If not, in other words: "else"
The "while" loop
The "repeat" loop
The "for" loop, a counting loop
Stop the turtle
Create your own commands with “learn

This is the reference for KTurtle's TurtleScript. In this chapter we first briefly touch all the different instruction types. Then the commands are explained one by one. Then containers, math, questions and execution controllers are explained. At last you are shown how to create you own commands with learn.

Different Instruction Types

As in any language, TurtleScript has different types of words and symbols. Here the differences between the types are briefly explained.

Commands

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

For a detailed overview of all commands that KTurtle supports go here.

Numbers

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: 0, 1, 2, 3, 4, 5, etc. The negative numbers: -1, -2, -3, etc. And the numbers with decimals, or dot-numbers, for example: 0.1, 3.14, 33.3333, -5.05, -1.0.

Numbers can be used in mathematical calculations and questions. They can also be put in containers.

Numbers are highlighted with dark red in the code editor.

Strings

First an example:

print "Hello, I'm a string."

In this example print is a command where "Hello, I'm a string." is a string. Strings start and end with the " mark, by these marks KTurtle knows it is a string.

Strings can be put in containers. Yet they cannot be used in mathematical calculations and questions.

Strings are highlighted with red in the code editor.

Names

When using the TurtleScript programming language you create new things. If you write a program you will often need containers and in some cases you need learn to create new commands. When making a new command with learn you will have to specify a name.

You can choose any name, as long as it does not already have a meaning. For instance you cannot name a function forward, since that name is already used for an internal command.

# here forward is used as a new command, 
# but it already has a meaning so 
# this will produce an error:
learn forward {
  print "this is invalid"
}

# this works:
learn myforward {
  print "this is ok"
}

Names can contain only letters, numbers and underscores (_). Yet they have to start with a letter. Container names have to start with the container prefix ($).

# here forward is used as a container, 
# starting with the $ prefix, so it does
# not conflict with the forward command
$forward = 20
print $forward

Containers are highlighted with bolded purple in the code editor.

Please read the documentation on containers and the learn command for a better explanation and more examples.

Assignments

Assignment are done with the = symbol. In programming languages it is better to read the single = not as 'equals' but as 'becomes'. The word 'equals' is more appropriate for the == which is a question.

Assignments are generally use for two reasons, (1) to add content containers, and (2) to modify the content of a container. For example:

$x = 10
# the container x now contains the number 10
$W = "My age is: "
# the container W now contains the string "My age is: "
# this prints the content of the containers 'W' and 'x' on the canvas
print $W + $x

For more examples see the section that explains containers.

Math Symbols

KTurtle supports all basic math symbols: add (+), subtract (-), multiply (*), divide (/) and the brackets ( and ).

For a complete explanation and more examples see the math section.

Questions

We can ask simple questions on which the answer will be 'true' or 'false'.

Using questions is extensively explained in the questions section.

Question Glue-Words

Questions can be glued together with so called 'question glue'. The glue words are and, or, and a special glue-word: not.

Using question-glue is explained in the Question Glue section.

Comments

Comments are lines that start with a #. For example:

# this is a comment!
print "this is not a comment"
# the previous line is not a comment, but the next line is:
# print "this is not a comment"

We can add comments to the code for ourselves or for someone else to read. Comments are used for: (1) adding a small description to the program, (2) explaining how a piece of code works if it is a bit cryptic, and (3) to 'comment-out' lines of code that should be (temporarily) ignored (see the last line of the example).

Commented lines are highlighted with light grey in the code editor.

Prev
Next
Home


docs.kde.org