Created
May 17, 2015 11:10
-
-
Save jogonba2/6af98c8747764c269e2c to your computer and use it in GitHub Desktop.
Bug Hunt Speeds by Uri Wilensky
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
breed [predators predator] | |
breed [bugs bug] | |
bugs-own [ | |
speed ;; either 1, 2, 3, 4, 5, or 6 | |
] | |
globals [ | |
total-speed-6-caught ;; keeps track of the number of bugs caught with speed of 6 | |
total-speed-5-caught ;; keeps track of the number of bugs caught with speed of 5 | |
total-speed-4-caught ;; keeps track of the number of bugs caught with speed of 4 | |
total-speed-3-caught ;; keeps track of the number of bugs caught with speed of 3 | |
total-speed-2-caught ;; keeps track of the number of bugs caught with speed of 2 | |
total-speed-1-caught ;; keeps track of the number of bugs caught with speed of 1 | |
] | |
to setup | |
clear-all | |
set total-speed-6-caught 0 | |
set total-speed-5-caught 0 | |
set total-speed-4-caught 0 | |
set total-speed-3-caught 0 | |
set total-speed-2-caught 0 | |
set total-speed-1-caught 0 | |
set-default-shape bugs "bug" | |
set-default-shape predators "bird" | |
ask patches [ set pcolor white ] ;; white background | |
foreach [1 2 3 4 5 6] [ | |
create-bugs initial-bugs-each-speed [ set speed ? ] | |
] | |
ask bugs [ | |
setxy random-xcor random-ycor | |
set-color | |
] | |
;; the predator breed contains one turtle that is used to represent | |
;; a predator of the bugs (a bird) | |
create-predators 1 [ | |
set shape "bird" | |
set color black | |
set size 1.5 | |
set heading 315 | |
hide-turtle | |
] | |
reset-ticks | |
end | |
to go | |
;; use EVERY to limit the overall speed of the model | |
every 0.03 [ | |
check-caught | |
move-predator | |
;; recolor the bugs in case the user changed SPEED-COLOR-MAP | |
ask bugs [ set-color move-bugs] | |
;; advance the clock without plotting | |
tick-advance 1 | |
;; plotting takes time, so only plot every 10 ticks | |
if ticks mod 10 = 0 [ update-plots ] | |
] | |
end | |
;;;;;;;;;;;;;;;;;;;;; | |
;; Runtime Procedures | |
;;;;;;;;;;;;;;;;;;;;; | |
to move-bugs | |
let candidate-predator nobody | |
let target-heading 0 | |
ask bugs [ | |
if wiggle? [right (random-float 5 - random-float 5)] | |
fd speed * 0.001 | |
ifelse flee? [ | |
ifelse any? predators in-cone 2 120 [ | |
set candidate-predator one-of predators in-cone 2 120 | |
set target-heading 180 + towards candidate-predator | |
set heading target-heading | |
set label "!" | |
] | |
[set label ""] | |
] | |
[set label ""] | |
] | |
end | |
to move-predator | |
ask predators [ | |
setxy mouse-xcor mouse-ycor | |
;; only show the predator if the mouse pointer is | |
;; actually inside the view | |
set hidden? not mouse-inside? | |
] | |
end | |
to check-caught | |
if not mouse-down? or not mouse-inside? [ stop ] | |
let prey [bugs in-radius (size / 2)] of one-of predators | |
;; no prey here? oh well | |
if not any? prey [ stop ] | |
;; eat only one of the bugs at the mouse location | |
ask one-of prey [ | |
if speed = 6 [ set total-speed-6-caught total-speed-6-caught + 1 ] | |
if speed = 5 [ set total-speed-5-caught total-speed-5-caught + 1 ] | |
if speed = 4 [ set total-speed-4-caught total-speed-4-caught + 1 ] | |
if speed = 3 [ set total-speed-3-caught total-speed-3-caught + 1 ] | |
if speed = 2 [ set total-speed-2-caught total-speed-2-caught + 1 ] | |
if speed = 1 [ set total-speed-1-caught total-speed-1-caught + 1 ] | |
die | |
] | |
;; replace the eaten bug with a random offspring from the remaining population | |
ask one-of bugs [ hatch 1 [ rt random 360 ] ] | |
end | |
to set-color ;; turtle procedure | |
ifelse show-colors? | |
[ set color item (speed - 1) [violet blue green brown orange red] ] | |
[ set color gray] | |
end | |
; Copyright 2005 Uri Wilensky. | |
; See Info tab for full copyright and license. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment