Chapter 2: Orientation

The following set of exercises will get you started with StarLogo Nova (aka "SLN").
The best way to learn to code is to build something simple. Here, you will write code to draw some flowers on the screen.
Be sure to go step-by-step for best results.

In this chapter, you will:

  •  Register for an account with StarLogo Nova
  •  Navigate to profile
  •  Create a new blank project 
  •  Change the name of a project 
  •  Save and Run code 
  •  Use tabs to switch between different pages 
  •  Drag blocks from the drawer 
  •  Connect blocks to create a program 
  •  Create more than 1 agent
  •  Use movement blocks 
  •  Draw on the terrain 
  •  Identify parts of the interface 
  •  Distinguish between push and toggle buttons 
  •  Explain how movement blocks affect turtle agents in different interface blocks 
  •  Download and set up NetLogo
  •  Play around with NetLogo to compare and contrast it with StarLogo Nova.     

New terms:

  • Workspace 
  •  SpaceLand 
  •  Drawers 
  •  Blocks 
  •  Widgets 
  •  Terrain 
  •  Agent 
  •  Breed of agents 
  •  3D Model 
  •  Tab 
  •  Page 
  •  Interface 
  •  Toggled 
  •  Loop 
  •  (x, y) coordinates 

Part 1: Create a StarLogo Nova Account and log in

  1) In a web browser with an internet connection, navigate to   www.SLnova.org
2) If you do not yet have a SLNova account, click the Register button and follow the directions.
You can create a new username + password OR register with a google account.
We ask for your email ONLY for password recovery.
3) You will then see a screen with all your projects. If this is your first login, there won't be any. As the term progresses, there'll be many!
Click the orange new project icon at the upper right.
4) In the window that pops up, give your project a name, select "Blank Project" and click "Create". 

Part 2: StarLogo Nova Interface

Once you get to your project, the screen has many important parts. We'll introduce them as you need them. 

Naming your project

Near the center of the top of the window is a blank where you can change the project's name. You should name your projects thoughtfully so you can keep track of them. For this project, something like "Orientation" is a good choice.        
SpaceLand.

The top part of the SLN window, called SpaceLand, is a 3D world where agents follow the programs. It consists of a green flat plane (called the terrain). Note that it has several buttons. The “setup” button, “forever” button and “Data” box are called widgets.

You can zoom in or out by clicking the "Edit Camera" button and then scrolling up (zoom in) or down (zoom out). You can lock the zoom by clicking "Lock Camera" or reset it by clicking "Reset Camera".
Agents

Agents are the actors in your simulation; they are shown acting in SpaceLand and they'll play all the roles in your model. An agent is represented by a 3D shape. You can select from simple built-in shapes or a library of 3D shapes. 

Workspace

The bottom part of the SLN window,  called the Workspace, contains drawers of blocks that represent the programming commands, and pages where you snap the blocks together to write programs.

Programming is basically putting together a logical sequence of directions. (We'll have a LOT more to say about that)

Workspace Tabs

 You will want the different types ("breeds") of agents in your simulation to have different behaviors. You'll do this by writing different SLN code for each of them. By placing code in a particular tab, you indicate to the program that this code applies to this particular breed.

You may want to have some code apply to all breeds - you put that code in the Everyone tab.

You may want some code to apply to the world as a whole (like when the sun comes up or it gets warmer) - you put that code in the TheWorld tab. 

You access the code for each breed by clicking on its tab at the top of the workspace. 

The 'parts drawer'

This is where you get the building blocks for your code.

At the top left of the workspace, you'll find the parts drawer. Clicking on the "Agents" menu will bring up the menu shown at right.

Briefly the drawers contain:
  • Agents: code blocks for creating, deleting, selecting, etc agents.
  • Detection: code blocks for dealing with contact ("collision") between agents as well as counting agents.
  • Environment: code blocks for dealing with things that affect the world: coloring it, drawing on it, detecting the color you're on, and dealing with time.
  • Interface: code blocks for dealing with buttons, indicators, graphs, and controls that the user can change or observe.
  • Keyboard: code blocks for recognizing when the user presses a key on the keyboard.
  • Lists: code blocks for dealing with specific groups of particular agents.
  • Logic: code blocks for making decisions like - "if this, then that"
  • Math: code blocks for calculating things and getting random numbers.
  • Movement: code blocks to control the direction and speed of movement of agents.
  • Procedures: code blocks for giving a commonly-used set of blocks a useful name so you can use it in many places without re-coding it all.
  • Sound: code blocks to let your simulation make sounds the user can hear.
  • Traits: code blocks to change how your agent looks (size, 3-d shape, color) and detect these traits of other agents.
  • Variables: code blocks to add custom traits to your agents that you can change and use to alter behavior (like age, nutritional status, etc).
  • Debugger: code blocks to let you print messages and data to the browser.

Part 3: Start Coding!

We'll start by making some agents.

Show NetLogo Code            

1) Be sure that you're working in the "World" tab so that the code you'll be writing applies to the world as a whole.   

Any simulation you can create in SLN, you can also create in NetLogo using text-based code. See the end of this chapter for how to get NetLogo set up.  

 
2) Choose "Interface" from the parts drawer and then drag the "when ... pushed" block to the Workspace.

Choose "setup" from the drop down menu in the block in the workspace.

The code you put in this block will run when you click the setup button.

The Net Logo code for this would be:

to setup
		  
end

		  

3) Choose "Agents" from the parts drawer and drag the "create... ... each do" block right into the 'mouth' of the "when setup pushed" block - you'll see that block stretch to hold the create block and then snap into place.

Type a "5" into the socket of the "create ... each do" block.

Choose "turtles" from the drop down menu in the "create ... each do" block.  

The Net Logo code for this would be:

to setup
  create-turtles 5 [
    
  ]
end

4) Take your code for a test drive.

You should test often to be sure your code is doing what you intend. It is easier to find mistakes early, when your code is simpler.

Scroll up and click the "Run Code" button to update the code in the program. If the "Run Code" button is red, that means there's a syntax error in your code - check for messages at the bottom of the Workspace that will help you find and fix the error. Note that this only checks if your code is connected properly - it cannot tell if it will do what you want.

Click the "setup" button and you should see a tiny white circle in the middle of spaceland. If you do, then your code is working; congratulations! If not, check to be sure that you've clicked "run code" and that your code matches the screenshot above. 

You can zoom in or out by clicking the "Edit Camera" button and then scrolling up (zoom in) or down (zoom out). You can lock the zoom by clicking "Lock Camera" or reset it by clicking "Reset Camera".

Click the "Interface"tab and then click the 'setup' button to see the result. It will be similar to this:

 
5) Make your turtles move.

Where are the 4 other agents? When created, all the agents begin at the same starting position in the middle of SpaceLand.

Program the turtles to each take one step forward as soon as they're created by dragging in a "forward" block from the "Movement" drawer and filling the "1" to indicate how far forward they should each move.

The Net Logo code for this would be:

to setup
  create-turtles 5 [
    forward 1
  ]
end

6) Test your code.

Scroll to Spaceland, click "Run Code" and then "setup".

You should see that the turtles have moved away from their starting point at the center of the screen. You might have to look closely. 

You can zoom in or out by clicking the "Edit Camera" button and then scrolling up (zoom in) or down (zoom out). You can lock the zoom by clicking "Lock Camera" or reset it by clicking "Reset Camera".

 

Note that, in Net Logo, the turtles are 'born' facing in random directions and with random colors.   

7) Fix a problem.

Why does it look like there are more than 5 agents? Every time you click the “Setup” button, five more agents will be created. To fix this delete the previously created agents before creating new ones.

Add a "delete everyone" block from the "Agents" drawer just above - so that it will run before the other steps - the "create 5 turtles" block. Note that the other blocks move to let this block snap into place.    

Test your code: scroll up to Spaceland, click "Run Code" and then "setup". No matter how many times you click it, there will be only 5 turtles.

What would happen if you put "delete everyone" after the "create 5 agents"? Try it and see... (but don't forget to put the code back the way it is shown at left).

The Net Logo code for this would be:

to setup
  clear-all
  create-turtles 5 [
    forward 1
  ]
end

8) Set up your turtles so they can draw on the terrain as they move.

Add the "Pen down" block from the "Environment" drawer just above the "forward 1" block in the "create 5 turtles..." block. 

The Net Logo code for this would be:

to setup
  clear-all
  create-turtles 5 [
    pen-down
    forward 1
  ]
end

9) Test your code.

Scroll up to Spaceland, click "Run Code", and click "setup".

If you zoom in, you should see the 5 turtle agents with a white color patch at origin (where they started), and then spread out after they leave take a step forward. 

You can zoom in from the "Zoom" menu. Note that each turtle leaves a trail in its own color.

Part 4: Draw A Flower

In this part, you will make the turtles draw a flower as they are moving.

 1) Program the turtle agents.

Be sure to open the turtles page in the workspace. Any code you enter there will only be run by turtles.

Drag in a "while... toggled" block from the "Interface" drawer and select "forever" from its drop down menu.

The Net Logo code for this would be:

to setup
  clear-all
  create-turtles 5 [
    pen-down
    forward 1
  ]
end

to forever
  ask turtles [
  
  ]
end

Note that the command "ask turtles" is the Net Logo way of putting these commands in the "Turtle" page.

2) Make the turtle agents move in a circle.

From the "Movement" drawer, take a "forward..." block, add it to the "while forever toggled" block and enter "1" for the number of steps forward to take each tick of the clock.

From the "Movement" drawer, take a "left by ... degrees" block and add it to the "while forever toggled" block and enter "15" for the number of degrees to turn left each tick of the clock.

Each of the turtles will follow these directions over and over as long as the "forever" button is 'down'. This is also called a 'loop'.

The Net Logo code for this would be:

to setup
  clear-all
  create-turtles 5 [
    pen-down
    forward 1
  ]
end

to forever
  ask turtles [
    forward 1
    left 15
  ]
end

3) Test your code.

Scroll up to SpaceLand, click "Run Code", click "setup", then click "forever" and the turtles should draw a flower.

You should see the turtles going around and around in circles leaving trails of their color (white) drawn by their pens.

If you click "forever" again to un-toggle it, the turtles will stop. They'll move again if you click "forever" again. 

The 'flower' here isn't quite perfect since the turtles here each start in a random direction while the turtles in SLN start evenly-spaced around the circle. 

4) Clear the terrain.

If you click setup again, you’ll notice that the flower drawing is still on the terrain. To reset the simulation back to an all green terrain:

Drag a "clear terrain" block from the "Environment" drawer into the code on the World page and above the "delete everyone" in the "when setup pushed" button code. 

If you test this now, you'll see that clicking "setup" clears the flower.

The Net Logo command "clear-all" takes care of clearing everything so there's no need to change the code to accomplish this.

  

Part 5: Extensions

Now that you've got that working, you can play around. Try these explorations as a start.  

 Movement

Use existing blocks or new blocks from the movement drawer to explore variations on the flower drawing program by changing the:
  • number of agents
  • number of degrees of the turn
  • direction of the turn (try "face towards")
  • number of steps forward
  • direction of steps (try backward or forward)

The Net Logo commands to try are shown below. One additional cool feature of Net Logo is that you can add comments - notes to yourself that the program will ignore - to any of your code. Net Logo will ignore anything after two semicolons ";;". I've used this feature to include explanations for each command below:

forward 1    ;; step forward 1
back 5       ;; step backward 5
right 20     ;; turn 20 degrees right
left 45      ;; turn 45 degrees left
set heading 270 ;; set your heading to 270 degrees
                ;;  0 = north; 90 = east; 180 = south; 270 = west


You can try some of these, one-by-one or in combination.

Agent Traits

Look in the "Traits" drawer and use the "set my ... to ..." block to change:
  • color of agents
  • size of agents
  • starting location (x,y) of agents

The Net Logo commands to try are (translations in the comments):

set color blue    ;; a list of color names can be found here

set shape "airplane" ;; a list of shapes can be found here 
                     ;;  DON'T FORGET THE QUOTES

set size 4        ;; 4x normal size

set xcor 3        ;; x coordinate = 3
set ycor -12      ;; y coordinate = -12
                  ;;  (x,y) 0,0 is the center of the screen


You can try some of these, one-by-one or in combination.

A Design Challenge

Draw three flowers in different locations that are different colors

Hint: you can have more than one "create..." block inside ONE "when setup pushed..." block (It is DANGEROUS to have more than one "when setup pushed block"; the author KNOWS).

Hint: In spaceland, the center is (x,y) 0,0; the upper right is 50,50 and the lower left is -50,-50. 
 

Part 6: Net Logo and text-based programming

Take your coding to the next level

StarLogo Nova ("Nova") is relatively easy to learn because of its drag and drop programming method. It's very 'goof proof' since you can't plug blocks in where they don't belong and you don't have to worry about how to spell the commands, whether you need a space or a dash between parts, etc.  

For most simulations, Nova is sufficient. However, it can be a pain to copy/paste and share code in Nova and it can be a little under-powered for simulations with lots of agents. There is a text-based programming language that is very similar to Nova, called Net Logo, which allows easy copy/paste and code sharing and can run simulations with tens of thousands of agents. 

Beginning programmers often find text-based coding to be very challenging since you have to get the text just right or it won't work. However, it will be well worth your time to give Net Logo a try. Trying out Net Logo will help you to  understand your Nova code better; it will allow you to make much more capable simulations; and learning to program in text is a very useful skill - once you've learned to program in one language, learning others is easy. Also, NetLogo is a more powerful simulation - you can easily use thousands of agents and run the simulation very fast so you can model long periods of time very precisely.   

          

If you scroll up to the top of Part 3, you'll see a checkbox like the one above at the far right of the page. Click it and the Net Logo code for each of the exercises appears. If you look at it, you will see that it is very similar to the Nova code you already built. You can copy the text from those parts and paste it right into Net Logo and run it. Give it a try! See below for details:

 1) Download the Net Logo program.

Net Logo is a free stand-alone program available for Mac and PC.

You can download it here. The site will ask for your contact info but they won't bother you. Once you get it downloaded and set up, you can run it.

The Interface tab corresponds to Spaceland; the Code tab corresponds to the workspace in Nova.

Very complete documentation can be found here.

If you google "Netlogo" and what you're trying to do, you'll usually get a bunch of useful links.
2) Download a starting template file.

But where are the "setup" and "forever" buttons? I've created a starter template file that you can download here. Download it, un-zip it, and open it from Net Logo's File->Open menu.

You should then see the two buttons in the "Interface" tab.
3) Enter some code.

If you click on the "Code" tab, you'll see the "to setup" and "to forever" commands.

You can type your own code here or paste in code from one of the examples above. I've pasted in the flower-making code from Part 4 Step 2 to show you what the colors look like when the code is right.

NetLogo will color your code in useful colors if it understands what you've entered.
  4) Check your code.

Clicking the  green "Check" button will ask Net Logo to see if your code is written correctly. It can't tell if it's going to do what you want; it can only tell if your syntax is right. If it's right, you won't see any message.

If there's an error - in the example at left, I mis-spelled "forward" - Net Logo will fuss at you and try its best to let you know where the problem is. First, you'll notice that 'foward' is in black type instead of blue. Second, if you click the "Check" button, Net Logo will peruse your code and flag it with a yellow warning saying that it doesn't understand what I meant by "foward".

Once your code is OK, you can go to the "Interface" tab and run it.

Don't forget to save your work! You can share these files with others.

Especially at the beginning, Net Logo can be a pain to use. You have to get the commands just right and it's not always super helpful with its error messages. You don't have to use it in the course, but it is worth the effort to give it a real try.