Java StarLogo
2.0
english
`turtle`
turtles-own [energy]

to go 
  wiggle 
  check-patches   
  scale-color blue energy  0 20    ;set color based on energy
  move
  wait .05
end

to check-patches      ;change energy based on patch color
   if pc = yellow [setenergy (1.1 * energy)]    ;gain 10%
   if pc = blue   [setenergy (.99 * energy)]    ;lose 1%
   if pc = red    [setenergy (1.5 * energy)]    ;gain 50%
   if pc = gray   [setenergy (.95 * energy)]    ;lose 5%
   if energy < .1 [setenergy .1]                ;don't let energy go below .1
   if energy > 20 [setenergy 20]                ;or above 20
end

to wiggle
  lt random 10 
  rt random 10 
end

to move
  fd energy / 5	         ;move forward based on energy
end


`observer`
to setup 
  ask-turtles [die] 
  crt number 
  ask-turtles
    [setenergy random initial-energy 
     scale-color 95 energy  0 20        ;set color based on energy
     setxy random screen-width random screen-height] 
end


`information`
For use with the Adventures in Modeling book,  by Vanessa Colella, Eric Klopfer, and Mitchel Resnick published by Teachers College Press, 2001.  

For more information, see  
<http://education.mit.edu/starlogo/adventures>  

Challenge 5: Energizer Turtles  
A straightforward example of using a variable is shown in the  project Energizer Turtles. This project builds on the Bumper Turtles series of projects. In this case when the turtles walk over patches of different colors their energy either increases or decreases, depending on the color of the patch that they pass.  

The turtles gain energy if they step on a red or yellow patch.  They lose energy if they step on a blue or gray patch. The energy level of each turtle is indicated by its color (a more energetic turtle is brighter) as well as by how fast it moves (a more energetic turtle moves faster).   

As you explore this project, there are a couple of details to notice in the procedures. 
First, a modified "check-patches" procedure asks each turtle to increase or decrease its energy  depending on the patch color at its location. 
Second, a turtle’s energy affects both its color and its speed. The "fd" command uses the value of the turtle’s energy variable to determine the size of the turtle’s forward step each time it moves. The "scale-color" command sets the turtles with the most energy to almost white and the least energy to almost black, with the turtles in between showing varying shades of blue. 

The "scale-color" command takes four arguments (or inputs), so it is worth a quick look.
 
This  program uses the following command: 	
scale-color blue energy 0 20 

This command literally means, scale the color of the turtles to a  shade of blue, depending on the value of the energy variable, with an energy value of 0 resulting in very dark blue and 20 in very light blue.  

To try this project, click the "setup" button to create the  number of turtles shown on the "number" slider. You can add "energy-change patches" by drawing red, yellow, blue or gray  patches. Click the "go" button to start the simulation and see the effect of the colored patches on the color and the speed of the turtles. You may notice that turtles tend to get stuck in some of the patches that decrease their energy. 

Can you modify  the percentage increase or decrease in the turtles’ energy (in  the Turtle Procedures) to change this tendency?


`interface`
SLCanvas
top-left 31 245

SLButton
turtle-or-observer? turtle
top-left 63 6
width-height 66 30
name "button2"
line-to-run "go"
forever? true
button-number 2
show-name? false
whichip 5

SLSlider
top-left 63 89
width-height 90 25
name "slider2"
variable "initial-energy"
min-value 0
max-value 20
current-value 5
slider-number 1
show-name? false

SLSlider
top-left 23 89
width-height 90 25
name "slider2"
variable "number"
min-value 0
max-value 200
current-value 50
slider-number 0
show-name? false

SLButton
turtle-or-observer? observer
top-left 23 6
width-height 66 30
name "button1"
line-to-run "setup"
forever? false
button-number 0
show-name? false
whichip 6


`turtle shapes`
[encoded data, 5330 characters]
qqqqqqqaoR8DO5wb3dkOFQAAFQ7Z

`template`
null

`settings`
patch-size 6
num-shapes 256
screen-half-width 25
screen-half-height 25
interface-window-xcor 312
interface-window-ycor 120
interface-window-size 586 369
output-window-xcor 0
output-window-ycor 0
output-window-width 300
output-window-height 219
info-window-xcor 327
info-window-ycor 378
info-window-width 500
info-window-height 419
control-center-xcor 0
control-center-ycor 85
control-center-width 285
control-center-height 415
turtle-command-center-height 150
observer-command-center-height 150

`string table`
H4sIAAAAAAAAAGNgYGAAABzfRCEEAAAAAAAABA==

`symbol table`
H4sIAAAAAAAAAGNgYGAAABzfRCEEAAAAAAAABA==

`double table`
H4sIAAAAAAAAAGNgYOB0iHJgAAH7lTNBYJaDWwOE/xHCt3+/TqTqofs6Bz0GiPgP
KP0uDQzsd0LUAQCpeei2TAAAAAAAAEw=

`list table`
H4sIAAAAAAAAAE2MQRKAIAwDQ0RgPHPwd/7/FyZQxzKzbENpATQAvegSJfyEzZRO
/nm9cZe3THF8PW5qyp5tada5p+w/w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