Lecture: 11-29-2012

posted Nov 29, 2012, 6:21 AM by Samuel Konstantinovich   [ updated Nov 29, 2012, 10:31 AM ]


-Tutorial 8, 14

(the only tutorials up to now that you should not have completed are 12, and 15+)

-Finish Tree Lab


-Keyboard shortcuts

-Using create in a setup vs using ask in a go function properly.

-Wait and proper usage.

-Functions with parameters and how to use them with sliders.

VERY useful keyboard shortcuts:

-terminal : ctrl + alt + T

-logout : alt + printscreen + K

Create turtle commands / Making observer context functions that asks turtles:

Observer context commands can give commands to create turtles that have instructions for turtles. Generally a setup function uses those:

to setup


    crt 5 [set shape "fish"]

    crt 5 [set shape "turtle" set size 2]

        ;we don't ask turtles to do this, because the fish would change also!

    ask turtles[setxy random-xcor random-ycor]

    ask patches [set pcolor red]


to doTurtleStuff

    fd .5

    ;a turtle context command


to doPatchStuff

    set pcolor pcolor + .1

    ;patch context


The go commands you made so far were probably turtle based. Soon we will be using more than just turtles so we would do somethng like this:

to go;attached to a forever button

    ask turtles [ doTurtleStuff ]

    ask patches [ dopatchStuff ]

    wait 1 / 24


What would happen if wait was inside of the 'doTurtleSTuff' command:

Would there still be a delay?

Is it a good idea or not and why?

Function Parameters used with sliders:

Lets say we have:

 A slider that goes from 1 to 10 in increments of 1 that it is named TREESIZE. 

 A slider that goes from 1 to 40 in increments of 1 that is named MAXANGLE.

Consider two types of code:

;Code that doesn't use the slider in a parameter:

;this takes the number on the slider and uses it in the rt command directly. This is the preferred method for constants.

to wiggle [ d ]

    repeat d


        fd 1

        rt random MAXANGLE

        lt random MAXANGLE



;code that does use the slider in a parameter:

;this takes the number on the slider and gives it to the wiggle command.

There is a subtle difference, that is that the value of TREESIZE is placed into the parameter of the wiggle function (named d) so that wiggle can do whatever it wants. This is the prefered method for values that change in recursion such as the size of a tree, which gets smaller over time.

to tree [ d ]

    if d > 0



        wiggle d

        hatch 1 [ rt 30 tree d – 1]

        hatch 1 [ lt 30 tree d - 1]    




You can make a button to call tree:


That will give the number on the slider to your tree function, even though the parameter says N, it STARTS out as the same number as the slider. Remember the slider stays the same number unless you move it, since it is only used once (at the very start of tree) it doesn't matter that the value stays the same while your tree is being created.