Tutorial 1: Diffusion

This tutorial guides you through a sample programming session with StarLogoT, showing you how to create a new StarLogoT project. As an example, it describes how to create a simplified model of gas diffusion (a detailed set of models that investigates the behavior of gasses is available online).

1. Setting Up the Environment

To start a new project, choose "New" from the "File" menu. Then, enter the following commands in the Command Center (put as many commands as you like on a line. New lines just make it easier to read):

ca crt 250

StarLogoT clears the Graphics window (ca stands for clear-all) and creates 280 turtles (crt stands for create-turtles). By default, the turtles start at position (0, 0) in the middle of the screen. So the "dot" in the middle of the screen is actually a tall "pile" of turtles. Each turtle also has a random heading.

Now, we want a red set and a blue set of turtles. The blue turtles should start out scattered randomly throughout the Graphics window, and the red turtles should start out concentrated in the middle. That way, we can watch the red turtles diffuse into the blue. To create our blue and red turtles, we need to flip a coin:

ifelse random 2 = 0 [setc red] [setc blue]

The ifelse statement is just like an if statement. If the condition "(random 2) = 0" is true, it executes the first bracketed set of instructions; if the condition is false, it executes the second set. Random 2 will evaluate to 0 or 1, so this statement acts like a flipped coin. Each turtle has a 50% chance of turning red and a 50% chance of turning blue.

setc, as you might guess, stands for set-color, and it sets a turtle's color.

Now that we have red and blue turtles, we must position them. Another if statement should do the trick:

ifelse (color = red) [ setxy (random 10) - 5 (random 10) - 5 ] [ setxy ((random screen-size-x) - screen-edge-x) ((random screen-size-y) - screen-edge-y) ]

Each turtle evaluates its own color and places itself accordingly. The red turtles place themselves anywhere within 5 spaces of the center. The blue turtles place themselves anywhere in the Graphics window.

setxy sets a turtle's x and y coordinates. The parentheses act just like parentheses in mathematics: they enforce an order of operations, so that the turtle first evaluates (random 10) and then subtracts 5 instead of evaluating "random (10 - 5)" (ie random 5).

screen-size-x and screen-size-y report the total width and height, respectively, of the Graphics window. screen-edge-x and screen-edge-y report the maximum x and y coordinates, respectively, in the Graphics window (the Graphics window coordinates range from negative screen-edge-x & screen-edge-y to positive screen-edge-x & negative screen-edge-y).

Therefore, the red turtles choose x and y coordinates between -5 (if random returns 0) and 5 (if random returns 10), and the blue turtles choose x coordinates between negative screen-edge-x (if random returns 0) and screen-edge-x (if random returns screen-size-x), and y coordinates between screen-edge-y and negative screen-edge-y.

Since we now have the initial state of our model how we would like it, let's save these instructions so we don't have to type them in again. Open the Procedures window and enter the commands as a setup routine:

to setup
  ca
  crt 250
  
  ifelse (random 2) = 0 [
    setc red
  ][	
    setc blue
  ]

  ifelse (color = red) [
    setxy (random 10) - 5 (random 10) - 5
  ][
    setxy ((random screen-size-x) - screen-edge-x) ((random screen-size-y) - screen-edge-y)
  ]
end

Notice the to and end statements that begin and end the function definition.

Now select the button icon from the Main toolbar and drag out a button region in the Interface window. A dialog box appears. In the Instruction field, type setup. Then click OK. Your new button appears. Whenever you click the button, the setup function executes.

2. Motion

Finally, the model needs motion. Return to the Procedures window and create a go function:

to go
  fd 1
  wait .05
end

Create another button with go in the Instruction field and with the Forever box checked. Notice the looping pair of arrows on the face of this button. The arrows indicate that the button is a forever-button and executes its instructions repeatedly. The setup button, on the other hand, is a once-button. When you click it, the button executes its instruction once.

Click on the go button. The turtles execute the go function again and again. As the turtles move, the red turtles diffuse into the blue ones. The wait function slows down the model so you can watch the turtles diffuse.

Forever-buttons run in the "background." You can execute other instructions in the Command Center while forever-buttons are running. Try typing setpc yellow. All the patches turn yellow and the turtles move on the new yellow background. To stop the turtles, click on the go forever-button.

From this example it should be clear that StarLogoT represents "behaviors" as functions. In this model, the go function represents the behavior of molecular motion.

If you want to save the "Diffusion" project, select "Save" from the "File" menu.