Skip to content
Physics and Astronomy
Home Our Teaching Resources C programming Exercise: Mr Turtle
Back to top
On this page
Contents

Mr Turtle

Mr Turtle (logo as he is formally called) is a well-known teaching tool. We will only be playing with him for this one week. So make the most of him!

Unlike most turtles Mr Turtle can obey simple commands and even draw lines with a multi-colured pen, so we can use him to create interesting patterns.

For this week only we suggest you work in pairs. Do not print out this page: work from the web as there are some examples to copy and paste.

Before we start it's worth noting that Mr Turtle has his own rather unique language and syntax (he's a turtle after all) and we won't be using all his capabilities. It's the discussions you will have with your partner that will be the most useful part of today's exercise so don't get too worried about the details of how you do things: concentrate on the principles.

For some of the tasks we will tell you what to do and ask you to work out what will happen when you do it. If what happens isn't what you expected, try to work out why it did happen. Other tasks will ask you to combine aspects of two of the previous tasks to do something new.

This is a whole week's practical work so expect it to take over an hour.

Load the logo web app

Make the window with the turtle in quite big.

Controlling the turtle

Commands are entered in the box at the bottom of the page ("Type your code here") and executed when you hit the "Run" button. Pressing "Run" runs the entire contents of the command box so you will normally need to delete the previous command before typing in the next (Ctrl-A Backspace).

NB: clicking the small upward arrow at the top right of the input box changes it ito a multi-line box which is probaby more convenient.

Command history

The app keeps a record of all the commands you have run which can be seen from the "History" link towards the top right of the page. Clicking inside the body of the command copies it into the command window where it can be edited if necessary and then and rerun.

Some useful commands

The more common commands have two-letter shortcuts.

Long version Short version Effect
forward 100 fd 100 Move forward 100 pixels
left 45 lt 45 Rotate anti-clockwise 45°
right 90 rt 90 If you need to ask...
clearscreen cs Clears the screen
penup pu Lifts the pen so Mr Turtle no longer draws
pendown pd Lowers the pen so Mr Turtle draws again

For example, copy either of the following into the (multi-line) control window and press Run

Long version Short version
clearscreen
forward 100
right 90
forward 100
right 90
forward 100
right 90
forward 100
right 90
       
cs
fd 100
rt 90
fd 100
rt 90
fd 100
rt 90
fd 100
rt 90

Just for fun: changing the pen color

None of the exercises require you to change the pen color but it's fun anyway!

  • Please note the funny double quote at the start of the color but not at the end.
setpencolor "black     setpencolor "blue
setpencolor "lime      setpencolor "cyan
setpencolor "red       setpencolor "magenta
setpencolor "yellow    setpencolor "brown
setpencolor "tan       setpencolor "green
setpencolor "aqua      setpencolor "salmon
setpencolor "purple    setpencolor "orange
setpencolor "gray      setpencolor "white

Play around a bit to get the idea, then move to task 1. (It's worth noticing that Mr Turtle doesn't care too much about new lines: we can put two commands onto one line or split a single command over several lines as we wish.)

Task 1: Drawing a house

Draw the classic line-drawing of a house (a square with the two diagonals and a roof) without going over any line twice. We suggest drawing it by hand first to check the right way to do it.

Discuss

  • Is it easier to build it up one bit at a time or to go for it all at once?
  • Did anybody manage, or even try, to type it all in one go and have it work first time?

Task 2: Teaching Mr Turtle a new trick

Unsurprisingly Mr Turtle does not know what a triangle is:

triangle   =>   Don't know how to TRIANGLE

A procedure is just a fancy name for a command we can teach Mr Turtle, in the same was that we might teach a dog to sit and beg.

The following code (there is no need to type it in if you don't want to) would draw a triangle:

Long version Short version
clearscreen
forward 100
left 120
forward 100
left 120
forward 100
left 120
       
cs
fd 100
lt 120
fd 100
lt 120
fd 100
lt 120

Teaching Mr Turtle the meaning of the word triangle

  1. Click on the "Library" link at the top right. The (large) area at the bottom right should now be blank.
  2. Replace the word "clearscreen" or "cs" above with "to triangle" and add the word "end" after the last command to obtain something like this and paste it into your command window:
    Long version Short version
    to triangle
    forward 75
    left 120
    forward 75
    left 120
    forward 75
    left 120
    end
    
           
    to triangle
    fd 75
    lt 120
    fd 75
    lt 120
    fd 75
    lt 120
    end
    
    • Note: when you type the words "to" and "end" into the command box logo will show it has recgonised them by making them uppercase and colouring them green or pink.
  3. Press "Run": nothing happens to the turtle but something should appear in the "Library" box.
    • What do you think will happen if you type triangle into the command window and press "Run"? Try it.

Possibly confusing Mr Trurtle?

  1. Click anywhere in the "Triangle" code in the "Library" section on the right. This should copy the code back back into the Command widow.

    Note: Sometimes when you have already changed a library procedure the code copied into the Command window is an old version of the procedure. In this case your best bet is to open the History window, find the section where you defined the procedure and click on that.

  2. Replace the word "triangle" with the word "square" without changing anything else so the code now looks like:
    Long version Short version
    to square
    forward 75
    left 120
    forward 75
    left 120
    forward 75
    left 120
    end
    
           
    to square
    fd 75
    lt 120
    fd 75
    lt 120
    fd 75
    lt 120
    end
    
     

     

     

     

    Note: the body of the
    procedure has not changed.

  3. Without actually trying it yet discuss with your partner what you would expect to happen and why if you were to run the command: cs square.
  4. Now try entering cs square in the command box and pressing "Run". Did iMr Turtle do what you expected? Why did he do what it did?

Task 3: Teaching Mr Turtle to draw a house

Previously we have created a sequence of commands to draw a house and then defined a new, single command to draw a triangle. Now we will combine these two concepts together to create a new command to draw a house.

  1. Click on the "History" link at the top right and scroll to find your house commands. Click anywhere into that section of the history window and that code will be pasted into your command box.
  2. Add the text "to procedurename" at the top of the command window and "end" at the bottom in exactly the same way as you did for the triangle.
    • What do you think would be a good choice for the procedure name?
  3. Press "Run" and click on the "Library" link to check the procedure has been accepted.

Try it out!

Task 4: A polygon

In the triangle example above we drew a triangle simply by typing in "fd ... lt" three times. Here we see another, more flexible approach.

Discuss

  • What do you think will happen if you type the following into the main window and press "Run"? (Don't forget to select both lines.)
make "SIDES  6 
repeat :SIDES [ fd 400/:SIDES    lt 360/:SIDES ]
"

Hints: the somewhat strange syntax :SIDES means "the value of SIDES", and don't forget the double quote on the first line.

Questions to discuss with your partner

  • What would happen if you changed the "6" to 8? Try it.
  • What would happen if you replaced all the occurrences of the word SIDES with the word AREA? Or SATSUMA?
    • How would it affect your answer if Mr Turtle did understand the meaning of the words "sides", "area" and satsuma?
    • How would it affect your answer if Mr Turtle did not understand the meaning of the words "sides", "area" and satsuma?
    • Do you think Mr Turtle does in fact understand these words?
    • What would happen if Mr Turtle were French or German? or Chinese?
    Feel free to try it!

Task 5: A more-complicated pattern

Modify the above code snippet to draw a regular hexagon of triangles, i.e. six triangles such that their centres form a regular hexagon as shown above. (Do not modify the "triangle" function, rather use it unchanged inside the previous code snippet.

  • Tip: the snippet above moves and turns six times. We want it to move, draw a triangle and turn six times.

Your first attempt will have an irritating flaw. The commands "penup" and "pendown" may help here.

Try making a pentagon of triangles, octagon of triangles, etc.

Task 6: Teaching Mr Turtle to draw polygons

So far we have taught Mr Turtle what to do on the command "triangle" (unsurprisingly, it's to draw a triangle) and created a sequence of commands to draw an arbitrary polygon.

We can combine this "triangle" procedure and the polygon loop in a rather neat way.

to polygon :SIDES
repeat :SIDES [ fd 400/:SIDES    lt 360/:SIDES ]
end

This looks pretty much like what we have seen before except that:

  • The "TO" line now has :SIDES after the name of the procedure
  • The body of this procedure does not contain the command: make"SIDES 6

The effect of these two changes is that the polygon command expects the value of SIDES to be provided to it, in exactly the same way as fd 100 tells the fd command to move forward 100 pixels, not 50.

Click "Run".

  • What would happen if you typed the following into the command window:
polygon 3 
and pressed "Run"?

How about:

polygon 6

Try it.

Task 7: A scalable house

In the example above we passed the procedure a parameter, which we chose to call "SIDES". Then inside the body of the procedure we were able to use ":SIDES" (don't forget the colon) inside arithmetic expressions which was then equal to the value we passed to it from the main command line.

Modify your "house" procedure to take an argument which is the side of the main square so that you can can type in "house 100 ", "house 200" etc. (or whatever you chose to call your procedure) to draw houses of different sizes.

Task 8: Lots of polygons.

In the command line create a procedure polypolygon (or make up your own name).

to polypolygon :POLYS :SIDES
pendown
repeat :POLYS [ polygon(:SIDES) penup
                fd 1200/:POLYS lt 360/:POLYS pendown 
              ]
end

Functions can call other functions.

Hit "Run" and try it, eg polypolygon 6 6 .

Task 8a: centre it

The previous function draws the polypolygon to the left of the center. Try adding a command to the start of the polypolygon function to move the turtle to the right before drawing:

to polypolygon :POLYS :SIDES
pendown
repeat :POLYS [ polygon(:SIDES) penup
                fd 1200/:POLYS lt 360/:POLYS pendown 
              ]
end
  • To edit a function click on the "Library" link and click on the polypolygon function.

Task 9: Lots of houses!

Now create a new procedure, based on the previous one, to draw a "polygon of houses", i.e. exactly the same as task 8 but houses instead of polygons. The two parameters should be the number of houses and their size. Do not worry about whether they are vertical or not.

Task 10: Discuss:

  • What feature(s) of this have you found the most surprising or fun?
  • How would the things we have seen here help you create complicated patterns more easily than just a huge list of "fd"s and "rts"s, etc?
                                                                                                                                                                                                                                                                       

Validate   Link-check © Copyright & disclaimer Privacy & cookies Share
Back to top