Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Logo and Logic This presentation uses a version of Logo called StarLogo available through MIT. It can be downloaded for free and installed on your.

Similar presentations


Presentation on theme: "Using Logo and Logic This presentation uses a version of Logo called StarLogo available through MIT. It can be downloaded for free and installed on your."— Presentation transcript:

1 Using Logo and Logic This presentation uses a version of Logo called StarLogo available through MIT. It can be downloaded for free and installed on your home computer.

2 In later slides, we will reduce the number of turtles to 1, but for now it is fun to create patterns with 30 turtles! StarLogo starts with 30 turtles on top of each other in the center of the screen (note that the black portion of the screen where the turtles are is somewhat truncated in this slide).

3 FD is the command for forward
FD is the command for forward. You follow it with a number that specifies how many steps each turtle should move forward. Note that the command can be given in upper or lower case. In this example, I keyed in the command fd 5 in the Turtle Command Center. FD 5 means that each of the turtles moves forward 5 steps. Each of the turtles moved out from the center by 5 commands.

4 Note again that the command can be given in upper or lower case
Note again that the command can be given in upper or lower case. My first command used fd and the next command used FD. What I have done is used the sequence structure - each turtle followed the first command and then the second command. I am issuing commands using the sequence structure - one command after another. I have now told the turtles to move forward another 2 steps. The two commands I have issued are: fd 5 FD 2

5 Commands (SEQUENCE): fd 5
Pendown is the PD command and penup is the PU command. Pendown means that the turtles draw as they step. PU means they just move. Notice that when we start the pen is up. Commands (SEQUENCE): fd 5 FD 2 PD In this example, I used PD for pen down and then told the turtles to take two steps forward. The result are lines drawn by each of the 30 turtles.

6 The box on the slide shows the sequence of commands that have been executed so far.
Commands: fd 5 FD 2 PD PU FD 3 The last two commands I issued lifted up the pen so the turtle does not draw as it moves and then moved the turtles forward 3.

7 I can save my projects for use in the future.
Note that I can then close the project and later reopen it. To clear the code that I wrote in the Turtle Command Center, I can use Edit/Clear. I did a File/Save Project As and seved the project in a directory/folder I created as movedraw.slogo.

8 I have started a new project here and am moving the turtles and turning them as well. RT means take a right turn RT 90 means turn right 90 degrees. Commands: FD 5 PD RT 90 FD 5 When I executed the sequence of commands shown here, I moved each of the turtles forward by 5, then I put the pen down, then I made the turtles all turn right by 90 degrees and then I made each of the turtles step forward 5 in the direction they were now pointing.

9 The turtle command center can be used to control the activity of the turtles. When I want to change the number of turtles and things like that, I switch (using the tabs) to the Observer Command Center. In this example, I then clear the number of turtles and then establish that there will only be one turtle. When you start a new project, if you do not want the default of 30 turtles, you must set the CT and CRT. I am doing CT and CRT 1 to get one turtle. Commands: ct crt 1 ct means clear turtles crt means create turtles. must be followed by a number to tell logo how many turtles to create. There are two tabs in the Control Center: one for turtle and one for observer - up until now we have been using the one for turtle. Now I am switching to the one for observer to change the number of turtles.

10 SETC YELLOW FD 10 PD RT 60 RT 90 FD 20 SETC PINK FD 15 RT 20 FD 5
SETC BLUE LT 90 I pasted the sequence of commands to give a more readable copy. To make the picture I made it is important to execute in exactly the same sequence I did. If a step forward or a turn or a color setting were in a different position, the results would be different. Note the new commands LT for left turn and SETC to set a color. Logo only recognizes the words for standard colors like black, blue, brown, cyan, gray, green, lime, magenta, orange, pink, red, sky, turquoise, white, yellow. These commands are executed in sequence. If I changed the sequence, I would get different results.

11 PD SETC ORANGE FD 10 RT 90 BK 5 LT 90 SETC LIME FD 5 PU
The new command here is BK which means go back. BK 5 means backup 5 steps. Note here that I too the PU, moved forward and then put the pen down. This can also be done with JUMP (see next slide). I pasted a copy of the commands in the white area to show you the commands I used - the actual commands are hard to read.

12 PD SETC ORANGE FD 10 RT 90 BK 5 LT 90 SETC LIME FD 5 PU JUMP 5
The new command here is JUMP you can see how it works in the example on this slide. Note that again I have pasted a copy of the commands into the white space for readability.

13 Commands: PD FD 10 RT 90 In this example, I instructed the turtle to create a square. I put the pen down (PD) and then had it go forward 10 and then take a right run of 10 and then go forward 10 and take a right run of 10 until it had created the square. Note that in all of these where there is only one turtle, the observer control contains CT, CRT1 to clear turtles and then create 1 turtle. When you start a new project, you must set the CT and CRT in the observer control center. Note also that the default color is red. When I start a new project, the color will get set to red. Following this sequence, the turtle draws a square. Notice that I am repeating the sequence FD 10, RT 90. In the next slide I will use a loop to make this happen.

14 Commands: PD REPEAT 4 [FD 10 RT 90]
The REPEAT command does whatever is enclosed in the square brackets the number of times indicated. REPEAT 4 means repeat the commands 4 times. The commands are to go forward 10 and then make a right turn of 90. When this is done 4 times, I make a square and end up facing in the same direction as when I started. Commands: PD REPEAT 4 [FD 10 RT 90]

15 SETC LIME PD REPEAT 3 [FD 10 RT 120] RT 180 SETC PINK
In this example I used the repeat to draw the three lines of a triangle and then draw the four lines of a square. See the flowchart on the following page. SETC LIME PD REPEAT 3 [FD 10 RT 120] RT 180 SETC PINK REPEAT 4 [FD 10 RT 90]

16 Increase # times done by 1 FD 10 RT120
Start Set color to lime Have not done 3 times Y Increase # times done by 1 FD 10 RT120 RT 180 Set color to pink Have not done 4 times Y This shows the logic of the code shown. I set the color to lime and then I do the code in the loop until I have done it 3 times. Then I turn 180 degrees, set the color to pink and do the code in the next loop until I have done it 4 times. Increase # times done by 1 FD 10 RT 90 SETC LIME PD REPEAT 3 [FD 10 RT 120] RT 180 SETC PINK REPEAT 4 [FD 10 RT 90] End

17 Now I am back to using thirty turtles (the default)
Now I am back to using thirty turtles (the default). Be careful of the spacing when you type the commands, logo wants spaces as indicated when I typed the command. The syntax is reasonably strict. The syntax for the if is: if condition [list of commands] The condition is COLOR = PINK I only used one command, FD 10. I could have added other commands which I will do on the next slide. I am using the default of 3 0 turtles and I only want the ones that are PINK to move forward. The code here is checking to see if the color is equal to PINK. If it is those turtles and only those turtles will move forward 10 steps. Note that the FD10 must be enclosed in square brackets because the syntax of logo requires it. COMMAND: IF COLOR = PINK [FD 10]

18 IF COLOR = YELLOW [PD FD 5]
Now I have the results of executing two IF statements. The second one said if the turtle was yellow, I wanted to put the pen down and then move forward 5 which draws a line. Commands: IF COLOR = PINK [FD 10] IF COLOR = YELLOW [PD FD 5]

19 IF COLOR = YELLOW [PD FD 5] IF COLOR = BLUE [FD 15 PD RT 90 FD 5]
Finally, I added the IF command that said if the turtle was BLUE, I wanted to move forward 15, put the pen down, make a right turn of 90 and then move forward 5. This is one of those cases where there is a little flexibility in writing the command. I could have said move forward 15 and then right turn of 90 and then put the pen down before moving forward 5. In other words, it does not matter whether I put the pen down before or after I make the right turn of 90. It does matter that I get the pen down before I go forward 5 or I will not get the effect shown. Commands: IF COLOR = PINK [FD 10] IF COLOR = YELLOW [PD FD 5] IF COLOR = BLUE [FD 15 PD RT 90 FD 5]

20 IFELSE COLOR = PINK [FD 10] [FD 5]
The IFELSE has the following syntax: IFELSE condition [list of commands1] [list of commands2] Which translates as if the conditions is true execute the list of commands1 which is in the first set of brackets. If the condition is not true execute the list of commands 2 which is in the following set of square brackets. See flowchart on next slide. Command: IFELSE COLOR = PINK [FD 10] [FD 5]

21 IFELSE COLOR = PINK [FD 10] [FD 5]
YES NO FD 5 FD 10 If the color is pink step forward 10 else step forward 5. IFELSE COLOR = PINK [FD 10] [FD 5]

22 IFELSE COLOR = PINK [FD 10] [FD 5]
I have a series of IF statements that are executed in sequence and sent turtles forward and back depending on whether they meet the color specified in the condition. IFELSE COLOR = PINK [FD 10] [FD 5] IFELSE COLOR = YELLOW [FD 15] [FD 2] IFELSE COLOR = BLUE [FD 7] [BK 3] IFELSE COLOR = ORANGE [FD 4] [BK 2] IFELSE COLOR = PINK [FD 10] [FD 2] IFELSE COLOR = LIME [FD 5] [FD 1] IFELSE COLOR = PURPLE [FD 3] [BK 1] IFELSE COLOR = MAGENTA [FD 8] [BK 1] IFELSE COLOR = BROWN [FD 5] [FD 2]

23 IFELSE (COLOR = PINK OR COLOR = LIME) [FD 10] [FD 5]
In this example, if the color is either pink or lime the turtle moves forward 10 else the turtle moves forward 5. See the flowchart on the next slide. IFELSE (COLOR = PINK OR COLOR = LIME) [FD 10] [FD 5]

24 COLOR = PINK YES NO FD 10 COLOR = LIME YES FD 5 FD 10
If the color is either pink or lime you want to move forward by 10 steps else you want to move forward by 5 steps. IFELSE (COLOR = PINK OR COLOR = LIME) [FD 10] [FD 5]

25 IFELSE (COLOR = PINK OR COLOR = ORANGE OR COLOR = LIME) [FD 10] [HT]
HT means hide turtle and ST means show turtle SHOWN? tests to see if the turtle is shown or visible. Note that I put parenthesis around the whole condition on the first IFELSE and did not put parenthesis around the whole condition on the second IFELSE. They both worked proving that the parentheses are optional. IFELSE (COLOR = PINK OR COLOR = ORANGE OR COLOR = LIME) [FD 10] [HT] IFELSE COLOR = ORANGE AND SHOWN? [FD 10] [FD 5]


Download ppt "Using Logo and Logic This presentation uses a version of Logo called StarLogo available through MIT. It can be downloaded for free and installed on your."

Similar presentations


Ads by Google