Commands

Using commands you tell the turtle or KTurtle to do something. Some commands need input, some give output. In this section we explain all the built-in commands of KTurtle. Alternatively, using learn, you can create your own commands. Built-in commands we discuss here are highlighted with dark blue.

Moving the turtle

There are several commands to move the turtle over the screen.

forward (fw)
forward X

forward moves the turtle forward by the amount of X pixels. When the pen is down the turtle will leave a trail. forward can be abbreviated to fw.

backward (bw)
backward X

backward moves the turtle backward by the amount of X pixels. When the pen is down the turtle will leave a trail. backward can be abbreviated to bw.

turnleft (tl)
turnleft X

turnleft commands the turtle to turn an amount of X degrees to the left. turnleft can be abbreviated to tl.

turnright (tr)
turnright X

turnright the turtle to turn an amount of X degrees to the right. turnright can be abbreviated to tr.

direction (dir)
direction X

direction set the turtle's direction to an amount of X degrees counting from zero, and thus is not relative to the turtle's previous direction. direction can be abbreviated to dir.

getdirection
getdirection

getdirection returns the turtle's direction as an amount of degrees counting from zero, where zero is the direction when the turtle is pointing upwards.

center
center

center moves the turtle to the center on the canvas.

go
go X,Y

go commands the turtle to go to a certain place on the canvas. This place is X pixels from the left of the canvas, and Y pixels from the top of the canvas.

gox
gox X

gox using this command the turtle will move to X pixels from the left of the canvas whilst staying at the same height. gox can be abbreviated to gx.

goy
goy Y

goy using this command the turtle will move to Y pixels from the top of the canvas whilst staying at the same distance from the left border of the canvas. goy can be abbreviated to gy.

Note

Using the commands go, gox, goy and center the turtle will not draw a line, no matter if the pen is up or down.

Where is the turtle?

There are two commands which return the position of the turtle on the screen.

getx

getx returns the number of pixels from the left of the canvas to the current position of the turtle.

gety

gety returns the number of pixels from the top of the canvas to the current position of the turtle.

The turtle has a pen

The turtle has a pen that draws a line when the turtle moves. There are a few commands to control the pen. In this section we explain these commands.

penup (pu)
penup

penup lifts the pen from the canvas. When the pen is up no line will be drawn when the turtle moves. See also pendown. penup can be abbreviated to pu.

pendown (pd)
pendown

pendown presses the pen down on the canvas. When the pen is press down on the canvas a line will be drawn when the turtle moves. See also penup. pendown can be abbreviated to pd.

penwidth (pw)
penwidth X

penwidth sets the width of the pen (the line width) to an amount of X pixels. penwidth can be abbreviated to pw.

pencolor (pc)
pencolor R,G,B

pencolor sets the color of the pen. pencolor takes an RGB combination as input. pencolor can be abbreviated to pc.

Commands to control the canvas

There are several commands to control the canvas.

canvassize (cs)
canvassize X,Y

With the canvassize command you can set the size of the canvas. It takes X and Y as input, where X is the new canvas width in pixels, and Y is the new height of the canvas in pixels. canvassize can be abbreviated to cs.

canvascolor (cc)
canvascolor R,G,B

canvascolor set the color of the canvas. canvascolor takes an RGB combination as input. canvascolor can be abbreviated to cc.

Commands to clean up

There are two commands to clean up the canvas after you have made a mess.

clear (ccl)
clear

With clear you can clean all drawings from the canvas. All other things remain: the position and angle of the turtle, the canvascolor, the visibility of the turtle, and the canvas size.

reset
reset

reset cleans much more thoroughly than the clear command. After a reset command everything is like is was when you had just started KTurtle. The turtle is positioned at the middle of the screen, the canvas color is white, the turtle draws a black line on the canvas and the canvassize is set to 400 x 400 pixels.

The turtle is a sprite

First a brief explanation of what sprites are: sprites are small pictures that can be moved around the screen, like we often see in computer games. Our turtle is also a sprite. For more info see the glossary on sprites.

Next you will find a full overview on all commands to work with sprites.

Note

The current version of KTurtle does not yet support the use of sprites other than the turtle. With future versions you will be able to change the turtle into something of your own design.

spriteshow (ss)
spriteshow

spriteshow makes the turtle visible again after it has been hidden. spriteshow can be abbreviated to ss.

spritehide (sh)
spritehide

spritehide hides the turtle. This can be used if the turtle does not fit in your drawing. spritehide can be abbreviated to sh.

Can the turtle write?

The answer is: yes. The turtle can write: it writes just about everything you command it to.

print
print X

The print command is used to command the turtle to write something on the canvas. print takes numbers and strings as input. You can print various numbers and strings using the + symbol. See here a small example:

$year = 2003
$author = "Cies"
print $author + " started the KTurtle project in " + $year + " and still enjoys working on it!"

fontsize
fontsize X

fontsize sets the size of the font that is used by print. fontsize takes one input which should be a number. The size is set in pixels.

Mathematical commands

The following commands are KTurtle's more advanced mathematical commands.

round
round(x)

round the given number to the nearest integer.

print round(10.8)
forward 20
print round(10.3)

With this code the turtle will print the numbers 11 and 10.

random (rnd)
random X,Y

random is a command that takes input and gives output. As input are required two numbers, the first (X) sets the minimum output, the second (Y) sets the maximum. The output is a randomly chosen number that is equal or greater than the minimum and equal or smaller than the maximum. Here a small example:

repeat 500 {
  $x = random 1,20
  forward $x
  turnleft 10 - $x
}

Using the random command you can add a bit of chaos to your program.

mod
mod X,Y

The mod returns remainder of the division of first number by the second number.

sqrt
sqrt X

The sqrt command is sued to find the square root of a number, X.

pi
pi

This command returns the constant Pi, 3.14159.

sin, cos, tan

sin X
cos X
tan X

These three commands represent the world famous trigoniometrical functions sin, cos and tan. The input argument of these commands, X, is a number.

arcsin, arccos, arctan

arcsin X
arccos X
arctan X

These commands are the inverse functions of sin, cos and tan. The input argument of these commands, X, is a number.

Input and feedback through dialogs

A dialog is a small pop-up window that provides some feedback or asks for some input. KTurtle has two commands for dialogs, namely: message and ask

message
message X

The message command takes a string as input. It shows a pop-up dialog containing the text from the string.

message "Cies started KTurtle in 2003 and still enjoys working on it!"

ask
ask X

ask takes a string as input. It shows this string in a pop-up dialog (similar to message), along with an input field. After the user has entered a number or a string into this, the result can be stored in a variable or passed as an argument to a command. For example:

$in = ask "What is your year of birth?"
$out = 2003 - $in
print "In 2003 you were " + $out + " years old at some point."

If the user cancels the input dialog, or does not enter anything at all, the variable is empty.