Netlogo笔记04:羊吃草"S"增长模型

turtles-own [energy]
to setup
  __clear-all-and-reset-ticks
  reset-ticks
  ask patches[
    ifelse random-float 1 < 0.2
    [
      set pcolor green
    ]
    [
      set pcolor black
    ]
  ]
  create-turtles 1[
    setxy random-xcor random-ycor
    set heading random 360
    set energy 100
  ]
end

to go
  addfood
  ask turtles [
    turtle_move
    turtle_breed
    turtle_die
  ]
  do_plot
  tick
end

to addfood
  let i 0
  while [i < 10]
  [
    let xx random-pxcor
    let yy random-pycor
    ask patches with [pxcor = xx and pycor = yy][
      set pcolor green
    ]
    set i i + 1
  ]
end

to turtle_move
  if pcolor = green[
    ;eat food
    set pcolor black
    set energy energy + 10
  ]
  forward 1
  if random-float 1 < 0.1
  [
    set heading random 360
  ]
  set energy (energy - 2)
end
to turtle_breed
  if energy > 50
  [
    set energy (energy / 2)
    hatch 1 [;born new pathces with 100 energy
      fd 1
      set energy 100
    ]
  ]
end
to turtle_die
  if energy <= 0[
    die
  ]
end
to do_plot
  set-current-plot "Population"
  set-current-plot-pen "grass"
  plot count patches with [pcolor = green]
  set-current-plot-pen "sheep"
  plot count turtles
end
  
  1.  turtles-own [energy]:为绘图agent新增属性energy
  2. let xxx random-pxcor/pycor:为xxx赋值为当前虚拟世界的x/y坐标随机值
  3. hatch 1 [...]:新增1个turtle agent并继承父对象的所有属性值包括位置,再使它执行[...]操作
  4. set-current-plot "XXX":设定当前绘图的画板,XXX是已经定义的plot控件的名字
  5. set-cuurent-plot-pen "XXX":更新当前画笔为"XXX"
  6. plot count patches with [pcolor = green]:使用当前画笔,绘制颜色为green的patches的数量
  7. plot count turtles:同6

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尼卡尼卡尼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值