COLLISIONS SOURCE CODE

Back to Collisions


Turtle Procedures Observer Procedures
turtles-own 
[speed energy totalenergy] 

to go 
check-patches
collide
fd speed 
getout 
scale-color yellow energy 0 500 
end 

to collide 
grab one-of-turtles-here 
[seth random 360 
seth-of partner random 360  
settotalenergy energy + (energy-of partner)
setenergy (totalenergy *

(((random 99) + 1) / 100
setenergy-of partner totalenergy - energy 
setspeed (sqrt (energy / 500)) 
setspeed-of partner

(sqrt ((energy-of partner) / 500)) 
check-patches
fd 1] 
end 

to check-patches 
if pc-ahead = red 
[setenergy min (energy * 1.2) 500
setspeed (sqrt (energy / 500))
ifelse (heading < 180)
[setheading 180 - heading]
[setheading 540 - heading]
check-patches]
if pc-ahead = sky
[setenergy min (energy * 1.1) 500
setspeed (sqrt (energy / 500))
setheading 360 - heading
check-patches]
if pc-ahead = lime
[setenergy min (energy * .9] 500
setspeed (sqrt (energy / 500))
ifelse (heading < 180)
[setheading 180 - heading]
[setheading 540 - heading]
end

to getout
if (((xcor >= screen-half-width - .5)
or (xcor <= .5 - screen-half-width))
or ((ycor >= screen-half-height - .5)
or (ycor <= .5 - screen-half-height)))
[redraw seth random 360 jump 1 getout]
end

to redraw
if ((xcor = screen-half-width)
or (xcor = 0 - screen-half-width)) [stamp sky]
if ycor = screen-half-height [stamp lime]
if ycor = 0 - screen-half-height [stamp red]
end

to setup 
ask-turtles [die]
crt number 
ask-turtles
[setxy random (screen-width) random (screen-height)
setenergy ((random 200) + 1)
setspeed sqrt (energy / 500)
scale-color yellow energy 0 500
getout]

end

to clear-path
ask-patches [if (pc > 39) and (pc < 50) [setpc black]]
end