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.
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)
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!!!
forward 20 right 90 forward 20 right 90 forward 20 right 90 forward 20If 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!!!
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.)
home cs repeat 4 [forward 20 right 90]Ok, so that example wasn't so impressive. How about this?
home cs 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!
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.
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 box2. 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.
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:
home cs make length 5 box make length 25 box make length 50 box
Try this example:
to rectangle params [ x y ] repeat 2 [forward :x left 90 forward :y left 90] endNow type:
home cs rectangle 20 10 rectangle 10 20 rectangle 15 15Using params, the box procedure becomes:
to box params [ length ] repeat 4 [ forward :length right 90] end
And you type 'box 25' to draw a box of length 25. This is much simpler than typing 'make length 25' before running box!!!
to poly params [ sides length ] repeat :sides [ forward :length right (360/:sides)] endNow, invoke it with the following commands:
home cs poly 3 20 poly 5 30 poly 8 30Notice 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)
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]
]
end
home
cs
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] end home cs spiral 50
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.
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)] [] end home cs spiral 50