Project Code

Bumpermania game code:

global [score reading1 reading2 comreading fblast bblast x y randommotion randomdirection shadow]

to remote
loop[
ifelse switcha [send 1] [send 2]
ifelse switchb [send 3] [send 4]
]
end

to checkir
if ir = 1 [ a, on thisway wait 1 ab, off]
if ir = 2 [a, off]
if ir = 3 [ b, on thisway wait 1 ab, off]
if ir = 4 [b, off]
end

to realcar
setscore 0
setfblast 0
setbblast 0
loop[
if newir? [checkir]
ifelse frontbumper [checkfb][setfblast 0]
ifelse backbumper [checkbb][setbblast 0]
]
end

to checkbb
if (not bblast) [a, on thisway b, on thatway wait 20 ab, on repeat score [ab, rd wait 10 ab, rd beep wait 10] ab, off]
setbblast 1
end

to checkfb
if not fblast [setscore (score + 1) ab, on thatway wait 1]
setfblast 1 beep
end

to frontbumper
output switchb
end

to backbumper
output switcha
end

; chaser code

to chasercar
wait 1
auto-threshold
loop[
ifelse lostcar [searchforcar beep]
[follow]
]
end

to light
loop[
a, on
mwait 7
a, off
mwait 7
]
end

to lostcar
output ((readlight 1) < 18) and ((readlight 2) < 18)
end

to follow
setx readlight 1
sety readlight 2

ifelse ((x > y) and (abs (x - y) > 70)) [a, onfor 4 b, off]
[ifelse ((y > x) and (abs (y - x) > 70))
[a, off b, onfor 4] [ab, onfor 8]
]

end


to searchforcar

setrandommotion (random % 2)
ifelse (randommotion = 0) [ab, onfor 8]
[
setrandomdirection (random % 2)
ifelse (randomdirection = 0) [a, onfor 5 b, off] [a, off b, onfor 5]
]
if ((sensora < shadow) or (sensorb < shadow)) [ab, rd wait 15 ab, rd]
end

to auto-threshold
setshadow (sensora - 33)
end

to readlight :sensor
setcomreading 0
repeat 5
[
setreading1 sensorread :sensor
mwait 7
setreading2 sensorread :sensor
mwait 6
setcomreading (comreading + abs (reading1 - reading2))
]

output comreading
end

to sensorread :sensor
ifelse (:sensor = 1) [ output sensora ] [output sensorb]
end

to abs :x
ifelse ( :x < 0 ) [ output (0 - :x) ] [ output :x ]
end

to mwait :n
resett
waituntil [timer > :n]
end

Remote control car code (when not playing game):

global [score fblast bblast]

; for remote control car

to remote
loop[
ifelse switcha [send 1] [send 2]
ifelse switchb [send 3] [send 4]
]
end

to checkir
if ir = 1 [setpower 5 a, on thisway]
if ir = 2 [a, off]
if ir = 3 [setpower 5 b, on thisway]
if ir = 4 [b, off]
end

to realcar
setscore 0
setfblast 0
setbblast 0
loop[
if newir? [checkir]
ifelse frontbumper [checkfb][setfblast 0]
ifelse backbumper [checkbb][setbblast 0]
]
end

to checkbb
if (not bblast) [a, on thisway b, on thatway wait 20 ab, on repeat score [ab, rd wait 10 ab, rd beep wait 10] ab, off]
setbblast 1
end

to checkfb
if not fblast [setscore (score + 1) ab, on thatway wait 1]
setfblast 1 beep
end

to frontbumper
output switchb
end

to backbumper
output switcha
end

 

Cricket photo found at: http://www.inbio.ac.cr/sancho/jose.html