Monday, October 30, 2006

Programming with Logo - the basics

Let's start by programming a virtual robot. Create a detailed list of instructions for getting your robot from the parking lot to sitting down at a computer in the classroom. [source]

We're going to use StarLogo to learn about and use programming concepts and programmable modeling environments.

Cast of Characters
Turtles: The objects that you control with your programming. Each turtle has a position, heading, color and pen. You can add more specialized traits and properties.

Patches. The piece of the world in which the turtles live. Like turtles, patches can execute StarLogo commands, and they can act on turtles and patches. Patches are arranged in a grid, with each patch corresponding to a square in the Graphics area.

Observer. The observer "looks down" on the turtles and patches from a bird's eye perspective. The observer can create new turtles, and it can monitor the activity of the existing turtles and patches.

Interface
Control Center - Observer. Type commands that can be executed by the observer, eg. clearturtles (ct) and cleargraphics (cg)

Control Center - Turtle. Type commands that will affect the turtle(s), eg. forward 10 (fd 10), right 90 (rt 90)

StarLogo Window. Includes the Graphics area which is the patch where the turtles live. The white area to the left of the Graphics area is the Interface area, where you can create buttons, slides, and monitors that interact with your program.

Commands to build a square
clear-turtles or ct
Erase all turtles (observer command)

clear-graphics or cg
Erase graphics (observer command)

create-turtle number or crt
Create turtles, you can create as many turtles as you want by specifying number (observer command)

forward number or fd
Moves turtle in the direction of its heading the number of steps specified.

right degrees or rt
Rotates the turtle clockwise the number of degrees specified. Remember turning right or left is from the turtle's perspective

left degrees or lt
Rotates the turtle counterclockwise and number of degrees specified.

To create a square
In the observer command center type:
ct
crt 1

In the turtle command center type:
fd 10
rt 90
fd 10
rt 90
fd 10
rt 90
fd 10
rt 90

More turtle commands
repeat number [list of commands]
Repeats the commands in the brackets the number of times specified. For example, to make a square type:
repeat 4 [fd 10 right 90]

back number of bk
Moves the turtle in the opposite direction it is heading the number of steps specified.

pendown or pd
Causes the turtle to draw when it moves.

penup or pu
Causes the turtle to move without drawing.

setcolor colorname or setc
Sets turtle(s) color to colorname.

Challenge: Create a green square

complete command list

No comments: