LOGO Programming: Turtle Basics
LOGO Programming: Turtle Basics
Turtle Basics
The turtle looks like a little triangle in the middle of the screen.
The head shows you which direction he is facing. When the turtle
moves he draws a line behind him.
FD 100
To get the turtle to draw a line, just tell him to move FORWARD a number of
steps (turtles take small steps, so you need to tell him to move FORWARD many
steps, like 100, to get him to move enough to see). I like to use the smaller word
FD instead of typing FORWARD. Both work the same way.
To get the turtle to draw a
shape, like a square, you can
give him the instructions he
needs to "walk" around the
shape of a square.
Basic Commands
Command What it does
FD 100 Move the turtle forward 100 steps.
RT 90 Turn the turtle to the right 90º.
LT 90 Turn the turtle to the left 90º.
BK 100 Move the turtle backwards 100 steps.
PU Pick the turtle's pen up off the paper.
PD Put the turtles pen back down on the paper.
CS Clear the screen and start over.
HT Hide the turtle (triangle).
ST Show the turtle (triangle).
REPEAT 3 [...] Repeat the commands 3 times.
Polygons
One of the easiest things to do in logo is use the turtle to draw various polygons. You
can notice that in order to draw a closed polygon, the turtle has to walk around the
edges, eventually turning through 360 degrees before coming back home. Since the
turtle makes N turns for an N-sided polygon, the size of each turn in 360/N degrees.
Triangle (3 sides)
Turtle Turns 120º
REPEAT 3 [FD 100 RT 120]
Square (4 sides)
Turtle Turns 90º
REPEAT 4 [FD 100 RT 90]
Pentagon (5 sides)
Turtle Turns 72º
REPEAT 5 [FD 100 RT 72]
Hexagon (6 sides)
Turtle Turns 60º
REPEAT 6 [FD 100 RT 60]
Septagon (7 sides)
Turtle Turns 51º
REPEAT 7 [FD 100 RT 51]
Octagon (8 sides)
Turtle Turns 45º
REPEAT 8 [FD 100 RT 45]
Draw the shape in the right box that goes with the turtle instructions in the
left box.
FD 100
RT 90
FD 100
RT 90
FD 100
RT 90
FD 100
RT 90
FD 100
RT 120
FD 100
RT 120
FD 100
RT 120
FD 100
BK 50
RT 90
FD 50
LT 90
FD 50
BK 100
PU
RT 90
FD 25
LT 90
PD
FD 50
PU
FD 10
PD
FD 5
HT
What if we want to draw a lot of squares. Each time we have to type in:
That's not a whole lot of typing, but wouldn't it be nice if we could just type:
SQUARE
TO SQUARE
REPEAT 4 [FD 100 RT 90]
END
Now we can use our new word to make cool new programs. Can you figure out
what this does?
We can make our new words use numbers too. Let's change our SQUARE word
to draw squares of different sizes:
TO SQUARE :SIZE
REPEAT 4 [FD :SIZE RT 90]
END
SQUARE 50
or
SQUARE 100
Draw the shape in the right box that goes with the turtle instructions in the
left box.
TO TRI :SIZE
REPEAT 3 [FD :SIZE RT 120]
END
TO HEXTRI :SIZE
REPEAT 6 [TRI :SIZE RT 60]
END
TO POLY :SIDES
REPEAT :SIDES [FD 50 RT 360/:SIDES]
END
to star :sides
make "ang 360/:sides
repeat :sides [FD 50 RT 2*:ANG FD 50 LT :ANG]
end
You can type in notes, and logo will play them in the same order you type them in:
do re mi fa sol fa mi re do
This makes a sound like a piano, but you can also make sounds like different
instruments:
eighth do re mi
quarter do re mi
half do re mi
whole do re mi
dinosaur do re mi
low do re mi
middle do re mi
high do re mi
TO LINES
REPEAT 100 [SETXY RANDOM 200 RANDOM 200]
END
TO SONG
REPEAT 24 [NOTE RANDOM 12]
END
We can even make a "picture" of the music:
TO SONG2
REPEAT 24 [PLAYDRAW RANDOM 12 REPCOUNT]
END
to sample
reset
repeat 128 [instrument repcount
quarter do re mi fa whole sol]
end
piano sheep
high banjo sheep
high flute sheep
Pumpkin
; Fillpoly
;
; Creates a polygon filled in with a color. The :size is the approximate
; diameter. :color is an RGB list. The polygon is created about the turtle
; at its center.
;
to fillpoly :sides :size :color
poly :sides :size
setfloodcolor :color
fill
end
pd
lt 45 fd :dzMouth
lt 90 fd :dzMouth
rt 90 fd :dzMouth
rt 45
pu setpos :ptSave pd
rt 45 fd :dzMouth
rt 90 fd :dzMouth
lt 90 fd :dzMouth
lt 45
pu setpos :ptSave
end
setpensize [5 5]
head :size
tri :size
pu setpos :ptCenter
fd :dyMouth rt 180
mouth :size
pu setpos :ptCenter
end
; Draws triangular eyes and nose dimensioned for the :size'd pumpkin head.
to tri :size
fillpoly 3 :size/6 [0 0 0]
end
Recursive Trees
to rtree :size
if :size < 5 [stop]
fd :size
lt 30 rtree :size*(((random 5)+5)/10)
rt 60 rtree :size*(((random 5)+5)/10)
lt 30 bk :size
end
Towers of Hanoi
The Towers of Hanoi puzzle may be familiar to you. It is a game of 3 pegs and a
number of disks. Each disk has a different size. The puzzle starts with all the disks on
one peg, with the largest disks on the bottom. The problem is to move the disks from
the first peg to the last peg without ever placing a larger disk on top of a smaller one.
A simple program to solve this problem is this one. It merely prints the "moves" to be
made.
A more complex version (with some satisfying graphics displaying an animation of all
the moves - type "towers 10" to start the demo...you can turn Trace on to slow down
the program to see the individual moves if the program is running too swiftly).
to drawdisk :disk :peg :height :fDraw
pu
setxy :peg*150-300 :height*10
ifelse :fDraw [penpaint] [penerase]
bk :disk*5
fd :disk*10
end
; show :stacks
; show :pegs
end
to start :disks
cs
ht
make "moves 0
rt 90
setpensize [5 5]
make "stacks (array 3)
setitem 1 :stacks :disks
setitem 2 :stacks 0
setitem 3 :stacks 0
to towers :disks
start :disks
tower :disks 1 3 2
end