310 likes | 322 Views
Learning to program with Logo. Presented by the school of computing and the center for forensics, INFORMATION TECHNOLOGY, AND SECURITY. What is Logo?. Logo was created in 1967
E N D
Learning to program with Logo Presented by the school of computing and the center for forensics, INFORMATION TECHNOLOGY, AND SECURITY
What is Logo? • Logo was created in 1967 • The original purpose of the language was to create a “mathematical land” where students like yourselves could play with words and sentences • Why is called Logo? • The language was called Logo because in the Greek language, logos means thought • When you use Logo, you imagine you have a turtle. You will give that turtle commands to move
What is programming? • When you think of a computer, you normally think of things like video games • These video games are made up of a lot of instructions • The people that created the video games are called programmers • Programmers create a set of instructions fora computer • Today, you are going to use Logo to create instructions for the computer • After this session is over, you should be able to understand a little more about how much is involved in creating programs
Getting started with Logo • On your desktops, look for this icon • Using the left button on the mouse, click twice on it
You should see this This part of the screen is where we see the Logo turtle. When you give commands to the turtle, you will see the turtle respond In this part of the screen, you will type in instructions
Instructions for the turtle • When you give instructions to a computer, you have to use a language that the computer can understand • For example, what if your teacher suddenly started speaking French during Math classand said“ce qui est 1 plus 1” (In English this is “what is 1 plus 1?”) Unless you speak French, you wouldn’t understand • A computer can’t understand what you’re telling it unless you speak in its language • This is true anytime you write a program using something like Logo. It is like learning a new language.
Moving the turtle forward • One instruction that the turtle understands is the command to move forward • The command we will use is FD • If you look at the Commander window, at the bottom there is a box where you type in commands • Type in the command FD 100 and then hit this button There is a space between FD and 100 This is what should happen
Moving the turtle backward • Since we can move the turtle forward, we should be able to move it backward • The command is BK • Type BK 100 in the Commander window and hit the button again
The direction of the turtle • Notice that the turtle is just going up or down • This is because of the direction it is pointing • Another command we can give the turtle is to change the direction it moves • We can have it turn right by giving it the command RT • Notice when we used FD and BK, the number we use tells the turtle is how much to move • When we tell the turtle RT, we tell it what direction to move by giving it a number between 0 and 360 (Have you ever heard of something going 180 degrees or 360 degrees? What does that mean?) • Try this command to the turtle, RT 45, and see what it does when you hit
What the number means after RT • The reason we use a number between 0 and 360 is because of a circle • If you look at a circle, you can break it up into small pieces. 360 of these pieces would make a whole circle • 180 of these pieces would make half a circle • So we use a number between 0 and 360 to say what direction we want to move by telling the turtle how many pieces of the circle to move through • Experiment by typing RT 90, RT 180, RT 270, and RT 360 and hitting the button each time and see what happens
What if you want to move in the other direction • If you don’t want to move right, you can move left • The command to move left is LT • The number after LT means the same thing as the number after RT • Try typing in LT 90 LT 180 LT 270 and LT 360 and hitting the button each time
Clearing the Screen • If we want to clear what we’ve done, we can give the turtle the command CS • The command CS means clear screen • So everything the turtle has done is cleared and we are back with the turtle in the middle • In the Commander window, type CS and hit the button
Let’s add a little color • The turtle doesn’t have to draw in black • We can create many different colors or we can use colors that the turtle knows • The command to tell the turtle to use a different color is SETPC • Type in SETPC and follow it with a space and a number between 0 and 15. This is what the numbers mean
Using Color • As an example of using color, let’s try this. If you haven’t already cleared your screen, use the command to clear • Type the following commands in the Commander window and hit the button after each one • SETPC 4 • FD 100 • RT 90 • FD 100
Creating something called a procedure • In programming, one of the important things you do is create small pieces of code that you can use over and over again • This means you don’t have to typeeverything again • We create something called a procedure which is just a list of commands like FD and RT, but we are able to keep the commands we’ve already typed so we don’t have to type them again • To create a procedure, find the button labeled
What to type • Take your mouse and click after the word To and then hit the Space bar one time and type the word main • This means we are telling the turtle that we are creating a procedure called main, and what we type in between to main and end tells the turtle how to “do main” • If you think about it, it is very much like the FD command. The turtle knows what to do when we type FD. Now we are going to tell the turtle what to do when we type main • After typing main, hit the Enter keyand then type the lines to the right • Now find the word File and click on it. You will see a menu. The first item in the menu is • Select that and click with the left mouse button • The window will close FD 100 RT 90 FD 100 RT 90 FD 100 RT 90 FD 100
How to run the procedure • Telling the turtle to run the procedure is very easy. Clear the screen before you start • All we do is type the name of the procedure and hit the button • So in the Commander window, type main and hit
Looping • What do you think of when someone says loop? • Do you think of something that starts and ends at the same point? • Have you ever run laps around a gym? If you start at one spot, run in a circle and come back to the same spot, you’ve created a loop • How many times can you run the loop? • As many as you want • In programming, we can do something similar. Programmers love to find ways to type as little as possible • So we introduce loops into our code so that we don’t have to type the same thing again • Look again at the code in your procedure by hitting the button • Can you see commands that you repeated more than once?
Let’s modify our procedure • Using your mouse, highlight everything between to main and end • The way you do this is to take the mouse and click with the left button on the line below to main, hold down that button and drag until everything between to main and end is highlighted • Once it’s highlighted, hit the Backspace key
Create a loop • In order to create a loop in Logo, we use the Repeat N command. N is going to be how many times we want Logo to repeat commands • Click the mouse after the word main and hit the Enter key • Type the following • REPEAT 4 [FD 100 RT 90] • Notice that these are not parentheses, these are brackets. You will find them on the keyboard above the Enter key • Once you type that in, click File and • Now run the procedure again • Would you rather type in the 7 lines we typed before or the 1 line we typed now? Do you see how useful a loop is?
Another way of saving typing • So what we drew was a square. A square has 4 sides • What if you wanted to draw a figure that has 5 sides? • One way would be to replace REPEAT 4 [FD 100 RT 90] with REPEAT 5 [FD 100 RT 360/5] • If we wanted to draw a six sided figure, we could type REPEAT 6 [FD 100 RT 360/6] • Wouldn’t it be nice if we could just tell the procedure how many sides our figure has instead of having to change the procedure?
Parameters • Now we are going to introduce a new word that is important in programming. That is parameters • A parameter of a procedure is a value that we can send to that procedure • Now click the button again to load the procedure • Now click with the mouse after the word main and hit the space bar and type :sides • In the second line replace the number 4 with :sides and replace 90 with 360/:sides • Now click File and • To run the procedure type main 5 and hit • Now try main 6, main 7, main 8, main 9, and main 10 and hit after each one
Saving our work • Something that you learn as you write many programs is that it is always important to save your work often • If you don’t save your work, then if something happens like the computer gets unplugged, you will lose all your work. This means you will have to start again • If you look at the menu, you will see the word File. This is not the same File you see when you edit a procedure • Click on File and you should see the word Save. Click on this • You are going to be asked to enter the name you want to call your file. Type your first name and then hit Enter
Another procedure • Now that we know how to write a procedure, let’s practice with another one • The idea is to create a dashed line like you would see on a highway • Let’s think about how we would make this happen • If you were going to draw a dashed line on a piece of paper, how would you do it? • To accomplish this in Logo, we will use two new commands • When we use the command PU, if we move the turtle forward or backward, it will not draw a line • To make it draw a line again, we use the command PD • So how could we use these to draw a dashed line? • Does anyone have ideas?
Drawing a dashed line • One idea is to move forward while drawing a line, then move forward without drawing a line • Then we would repeat this • Click on the button to edit the procedure • Then type the following • Then close the window, and type dashed in the Commander window and hit the button
Drawing a circle • It is very easy to draw a circle in Logo. Imagine you are standing at a certain point, then you start moving one step forward and one step to the right • What kind of direction do you move? • Let’s see what logo does • Click the button again to edit the procedure and add the following • Now run the procedure
Filling in a circle • Suppose we want to fill in the circle with a color • The way we will fill a shape with a color is to move the turtle inside the figure without drawing a line and then type the command FILL • In order for FILL to work, we have to specify what color to use. We do this with SETFC and then specify a color • Let’s change our circle procedure • Now run the procedure
What if you don’t want to see the turtle? • There are two other commands we can use • If you don’t want to see the turtle, use the command HT (Hide Turtle) • If you want to see the turtle, use the command ST (Show Turtle)
Calling procedures from inside of other procedures • We can create as many procedures as we want • We can even call one procedure from another • An example is • See if you can create this procedure and run it
Your challenge • Now that you know how to talk to the computer in a language it knows, see what kind of shapes you can draw • You might try drawing a rectangle. (The difference between a square and a rectangle is that all 4 sides of a square are the same, but only the opposite sides of a rectangle have to be the same.) • After you draw the rectangle, see if you can fill it with red