rLogo Tutorial

Getting Started

If you are impatient, and want to jump straight to the examples, please be sure to read the section titled Important Note. It might save you quite a bit of typing!

Below is the rLogo Applet. It is composed of three sections:

1. Output Window - The Output Window occupies the top half of the Applet. This is where the rLogo Turtle draws.
2. Command Box - The Command Box occupies the lower left section of the Applet. This is where you type in rLogo commands, and where rLogo sends messages.
3. Control Panel - The Control Panel occupies the lower right section of the Applet. It has several buttons and sliders with which you may control the Turtle.

Important Notes


Some users may have trouble pressing the 'Enter' key in the Command Box. If nothing happens when you press 'Enter,' you are one of these users! Instead of pressing 'Enter,' you need to click the button located below the Command Box labelled 'Enter.'

Use the break button to stop a running program.

If you get stuck, try typing 'help' in the Command Box!!!

Copy & Paste

If your operating system and web browser support "Copy & Paste" and you know how to use them, rLogo offers two major shortcuts:

To enter a single line, you may simply copy it from the tutorial, and paste it into the Command Box. Then, either press Enter, or click the Enter Button.

A special function has been built into rLogo that will allow many users to quickly load in the program examples without having to type them. Use the following procedure to load in example programs:

1. Select and Copy a program block with your mouse and/or keyboard (do not include the dashed lines).
2. Type 'batch' in the Command Box (don't type the quotes). The Command Box should now be labelled "rLogo Batch Input."
3. Paste the code into the edit box.
4. Click 'finished' on the edit box. (It may take several seconds before any output appears.)

Likewise, there is a way to SAVE your work. It is the dump command. Simply type dump in the Command Box. The editor will pop up, with all of your subroutines and variable definitions, ready for you to copy and paste into your favorite text-editor or word-processor. (And you can load it back in later using the batch command)

Lession #1: Introduction to Turtle Graphics

This lesson will introduce you to Turtle Graphics and the Control Panel.

Notice the triangle located in the middle of the top panel. This is the Turtle. (The tip of the triangle shows the direction the Turtle is pointed.)
The Control Panel allows you to control the Turtle by clicking buttons. As you click the buttons, the Command Box records the actions that occur. These commands may be used in future rLogo programming.

Press the forward button. The Turtle will move forward 10 units. Also, a new line will appear in the Command Box. It says "forward 10."
Press forward again. The Turtle moves some more. The last line in the Command Box changes to "forward 20."
Next, click on the right button. The Turtle turns a bit to the right. The Command Box receives a new line that says "right 10."
Click the right button 8 more times (the Command Box will say "right 90)."
Now click forward two more times.
Then click right 9 more times.
Then click forward 2 more times.
Then click right 9 more times.
Finally, click forward 2 more times.

You have just written a simple program that draws a box!!!


If you followed the example above exactly, the Command Box should show the following lines:
forward 20
right 90
forward 20
right 90
forward 20
right 90
forward 20
If it looks slightly different, don't worry. You probably just clicked once too many, or maybe once too few. If you want to try again, click the home button twice (the first time you click it, it returns the Turtle to its home positon, in the center of the screen. If you click it again before moving the Turtle, it will clear the screen.)

The lines that appear in the Command Box are the commands that rLogo understands.
You may type these commands directly into the Command Box, or you may use the Control Panel (the forward, back, left, right, and home buttons) to avoid some typing. Note that you have more flexibility by typing the commands directly, since you may specify parameters which give you more exact control. For instance, instead of hitting the right button 9 times, you can simply type right 90, which will cause the Turtle to immediately turn right by 90 degrees.

Here are a few more things you can do with the control panel:

1. Normally, the Turtle draws whenever you press forward or back, but if you press the button that says "Pen is Down," it will change to "Pen is Up." Now, forward and back will still cause the Turtle to move, but no line will be drawn. Pressing it again will cause the Turtle to resume its drawing. This is useful when you are composing complex drawings which aren't completely connected.

2. Sometimes the Turtle gets in the way of looking at your drawing. Press the Turtle is Showing button to hide the Turtle. Press it again to bring it back.

3. Tired of drawing black lines yet? You have a lot of control over the color of the lines that the Turtle draws. Notice the red, green, and blue Color Sliders. They control how much of each color is used by the pen. You may mix colors in a fashion similar to mixing paint. Try sliding the red Color Slider, all the way to the right. Notice how the Color Sample Block changes to bright red. Now click the Change Color button to change the color that the Turtle draws to red.

Now, take a break, and draw a picture!!!

Lesson Summary

The Control Panel may be used to control the Turtle by using the following buttons:

  • Forward - moves the Turtle forward 10 units; equivalent to typing 'forward 10'
  • Back - moves the Turtle backwards 10 units; equivalent to typing 'back 10'
  • Left - causes the Turtle to turn 10 degress to the left; equivalent to typing 'left 10'
  • Right - causes the Turtle to turn 10 degrees to the right; equivalent to typing 'right 10'
  • Home/Clear Screen - Pressing Home once returns the Turtle to the center of the Output Window. Its label will then read Clear Screen, until the Turtle is moved. Pressing Clear Screen will erase the Output Window. Pressing this is equivalent to typing 'home' or 'clearscreen' (which may be abbreviated as 'cs').
  • Pen is Down/Pen is Up - Toggles the state of the Turtle's pen. Normally, the pen is down, and the Turtle draws. By pushing this button, the pen will go up, and the Turtle will not draw. Pushing it again resumes drawing. Pressing this is equivalent to typing 'penup' (which may be abbreviated as 'pu') or 'pendown' (which may be abbreviated as 'pd').
  • Turtle is Showing/Turtle is Hiding - Toggles whether the Turtle shows up in the Output Window. Normally, the Turtle is visible, but pressing this button will temporarily hide it. Pushing it again will bring it back. Pressing this is equivalent to typing 'showturtle' (which may be abbreviated as 'st') or 'hideturtle' (which may be abbreviated as 'ht').
  • Change Color - When rLogo first starts, the pen color is black. You may use the Color Sliders to change the color of the Color Sample Block (located to the left of the Sliders). Pressing Change Color will change the pen color to that of the Color Sample Block. This process is equivalent to typing 'setpencolor (red) (green) (blue)' (which may be abbreviated as 'setpc (red) (green) (blue)') where (red), (green), and (blue) are integers between 0 and 255 inclusive.

    Lesson #2: Building a Better Box

    In Lesson #1, you learned how to draw a box, using only the control panel. rLogo has a more convenient way to draw a square, using the powerful repeat command.

    You will need to learn about two more important but simple things about rLogo to effectively use the repeat command:

    First, you need to know a little bit about parameters. A parameter is just a technical term for a piece of information that a command needs in order to be executed.

    You have already seen several commands that use parameters, namely forward, which requires a parameter that tells it how far to move, and right, which requires a parameter which tells it how many degrees to turn.

    Next, you need to know about rLogo lists. A list is composed by enclosing one or more commands inside square brackets (the [ and the ]). Here are three examples of rLogo lists:

    [forward 10 left 90 forward 15 right 90 forward 20 right 45 forward 20]
    [forward 20]
    [forward 20 right 90]

    Now, you are ready for the repeat command. It requires two parameters: how many times to repeat, and a list of commands to repeat.

    We now have a much better way to draw a box:

    (If you want to try this out, don't forget that you can avoid some typing by using "Cut & Paste!" While this isn't a lot to type, it will be good practice for the longer examples which are coming soon.)

    repeat 4 [forward 20 right 90]
    Ok, so that example wasn't so impressive. How about this?

    repeat 20 [ repeat 4 [forward 20 right 90] right 18]
    Now think how long it would have taken to do that by pressing the Control Panel Buttons! Notice how we nested a repeat command inside of another repeat command. rLogo doesn't mind; it will happily process any command inside the repeat command list, including another repeat command!

    Lesson Summary

    Parameters are values that are required by certain rLogo commands.

    Lists are created by placing one or more commands inside square brackets (the [ and the ]).

    The repeat command requires two parameters: the number of times to repeat, and a list of commands to repeat.

    Lesson #3: Introducing Subroutines

    Typing 'repeat 4 [forward 20 right 90]' every time you want to draw a box could become a little confusing, especially when you nest the command inside of another repeat, like in the last example of Lesson #2.

    There is an easier way: rLogo allows you to write subroutines, which are simply collections of commands that you give a new name. For instance, it would be nice to be able to just type 'box', and have the Turtle draw a box. Here is how to do it:

    1. Type this in the command box:

    to box
    2. An editor window will replace the Command Box. Type these commands into the editor window:

    repeat 4 [forward 20 right 90]
    3. Now, click the finished button, at the bottom of the editor window.
    4. Now, whenever you type 'box' into the command box, logo will draw a box! Click the home button twice, to clear the screen, then type 'box' to watch a box be drawn.
    If you want to change the way 'box' works, just type 'to box' again.

    Lesson Summary

    A subroutine is a collection of one or more rLogo commands. To create a new subroutine, or to change an existing subroutine, use the to command, supplying the name of the subroutine as a parameter.

    Lesson #4: Variables

    An rLogo variable is a word that you decide to associate with a numeric value. Try typing this in the Command Box:

    make length 40

    You have just assigned the value of 40 to the variable named "length". To make sure of this, you can use the show command:

    show :length

    Be sure to put a colon (:) in front of the variable name!

    You may now use the "length" variable in the place of a numeric parameter to an rLogo command. For instance:

    repeat 4 [forward :length right 90]

    produces the same results as:

    repeat 4 [forward 40 right 90]

    since the variable named "length" has a value of 40.

    Next, change length and try again:

    make length 20
    repeat 4 [forward :length right 90]

    Of course, we can change the box subroutine to use variables:

    to box
    repeat 4 [ forward :length right 90]

    (Don't forget to click 'Finished')

    And now, we can change the way that the box subroutine works, simply by changing the value of length:

    make length 5
    make length 25
    make length 50

    Lesson Summary

    Variables are created by using the make command, with two parameters: the new variable name, and its value. Variables may be used in place of numeric parameters, by place a : in front of the variable name.

    Lesson #5: The PARAMS command

    You learned earlier that many rLogo commands require parameters. You can create subroutines which require parameters by using the params command. This way, you do not have to use the make command before invoking your subroutine.

    Try this example:

    to rectangle
    params [ x y ]
    repeat 2 [forward :x left 90 forward :y left 90]
    Now type:

    rectangle 20 10
    rectangle 10 20
    rectangle 15 15
    Using params, the box procedure becomes:
    to box
    params [ length ]
    repeat 4 [ forward :length right 90]

    And you type 'box 25' to draw a box of length 25. This is much simpler than typing 'make length 25' before running box!!!

    Lesson Summary

    The params command requires a single parameter: a list of variables. A subroutine that has a params command as its first line will require parameters when it is invoked. These parameters will be automatically assigned to the variables named in the list.

    Lesson #6: Fun with Geometry

    Perhaps you remember from Geometry that the sum of the angles of a closed figure equals 360 degrees. We can take advantage of this fact to draw polygons with any number of sides! Try this:
    to poly
    params [ sides length ]
    repeat :sides [ forward :length right (360/:sides)]
    Now, invoke it with the following commands:
    poly 3 20
    poly 5 30
    poly 8 30
    Notice how the poly subroutine has a / in it. This is the symbol for division. rLogo can also add, with the + symbol, subtract, with the - symbol, and multiply, with the * symbol.

    The equation (360/:sides) is called a mathematical expression. You can experiment with more expressions by using the show command. Try these:

    show (10/2)
    show (10*2)
    show (10+2)
    show (5-2)
    make x 2
    make y 3
    show (:x+:y)
    show (:x*:y)
    show (2*:x)
    show (:x+:x)
    show (:y*2)
    show (:y+:y)

    Lesson Summary

    You may substitute mathematical expressions in place of numeric parameters, by simply enclosing them in parentheses. The show command is extremely useful when creating expressions.

    Lesson #7: Logical Expressions

    Next, you will learn about a special type of expression, known as a Logical expression. A logical expression can have only one of two values: TRUE, or FALSE. In rLogo, FALSE is represented by ZERO, and TRUE is represented by ONE, or any other value than ZERO.

    Try the following:

    show 5>2
    show 5<2
    show 0=0
    show 0=5
    show 5+5=10

    Within a subroutine, you may freely use white-space to make your programs easier to read. For example, the following are equivalent:

    Harder to read

    repeat 18 [repeat 6 [ right 60 forward 20] rt 20]

    Easier to read

    repeat 18 [
       repeat 6 [
          right 60
          forward 20
       rt 20

    The next command to learn is ifelse. This command allows conditional execution of commands, based on the value of a logical expression. Its syntax is:

    ifelse condition [true-command-list] [false-command-list]

    For instance, the following command will always move the turtle forward:

    ifelse (1=2) [ back 20 ] [ forward 20 ]

    Likewise, a small change causes it to always move back:

    ifelse (1=1) [ back 20 ] [ forward 20 ]

    Now, take a look at a less trivial example:

    to wiggle
    params [distance]
    make x 0
    repeat :distance [
      fd 20
      ifelse (:x=0)
        [ make x 1 left 20 ]
        [ make x 0 right 20]
    wiggle 10
    The while command also takes a logical expression as a parameter. It executes a list of commands as long as the expression is true. Try this:

    to spiral
    params [length]
    while (:length>0)
       [ forward :length right 90 make length :length-2]
    spiral 50

    Lesson Summary

    Logical expressions have one of two values: TRUE, or FALSE.

    FALSE is equal to ZERO in rLogo; TRUE is any value other than 0.

    The ifelse command takes three parameters: a logical expression to evaluate, the list of commands to execute if the expression is true, and the list of commands to execute if the expression is false.

    The while command takes two parameters: a logical expression, and a list of commands to execute while the expression is true. Care must be taken to insure the the expression will eventually become false; otherwise, an infinite loop will occur.

    Lesson #8: Recursion

    You are now well on your way to understanding rLogo! Before you finish, you will be introduced to a final challenging concept, known as recursion.

    Recursion occurs when a subroutine calls itself. Consider this alternate way to implement spiral, from above:

    to spiral
    params [length]
    fd :length
    right 90
    ifelse (:length>1) [spiral (:length-2)] []
    spiral 50

    This concludes the tutorial. You may wish to go back to the examples on the rLogo Home Page and study the source for more insight into the rLogo language.