user9913522
user9913522

Reputation:

How to set a default color(turtle's color when its enter in the model) of turtle after it changes its color?

I write a line of code which is this

ask turtles [if count other turtles in-radius 1 > 5 [set color white]]

now when turtle has changed color to white, but after the certain amount of time this property is not true for that same turtle, it should change its color to default color? how can I fix it?

Upvotes: 0

Views: 575

Answers (1)

Luke C
Luke C

Reputation: 10291

I think you're after a turtles-own counter that decreases whenever the condition is not satisfied. With this setup:

turtles-own [ default-color countdown ]

to setup
  ca
  crt 150 [ 
    setxy random-xcor random-ycor 
    set default-color blue
    set color default-color
  ]
  reset-ticks
end

Now you can get your turtles to wander around and change their countdown variable whenever they change color. When that condition is not met, they can decrease the counter until it hits zero, at which point they can revert to their default color. More detail in comments:

to go
  ask turtles [
    rt random 60 - 30
    fd 0.25

    ; if there are more than 5 turtles in radius 3,
    ; turn white and set countdown to 5   
    ifelse count other turtles in-radius 3 > 5 [
      set color white 
      set countdown 5
    ] [
      ; If not, and counter is greater than 0,
      ; decrease the counter. 
      if countdown > 0 [
        set countdown countdown - 1

        ; If counter gets down to 0, 
        ; set color back to the default.
        if countdown = 0 [
          set color default-color
        ]
      ]
    ]

  ]
  tick
end

Upvotes: 1

Related Questions