How does Templeton work?

The following is the code that allows Templeton to be so brilliant.

global [number-hits isdead?]
; the number-hits variables is a counter that keeps track
; of how many times templeton has been hit.
; the isdead? lets him know if he should die.

to tarantula
; this is the main method that controls templeton.
; it runs the throwing and catching method and moving
; method simultaneously and when he has been hit three
; times, it activates the 'die' method and ends the others.
move
throwandcatch
when [isdead? = 1] [stoprules die
reset]
end

to move
;this method makes templeton walk randomly around,
;turning occasionally. if he hits a wall, he will
;stop, back up, turn, and continue on his way
when [hitwall?]
[stopmoving wait 5 backwardfor 5 turn-left forwardfor (random 10)]
forever [forwardfor (random 100) turn-left forwardfor (random 100) turn-right]
end

to throwandcatch
;this method controls the throwing mechanism.
;when a ball is caught, it throws it back unless it is
;the third ball, in which case it sets isdead? to true.
when [catch?]
[ifelse (number-hits < 2)
[wait 5 throw setnumber-hits (number-hits + 1)] [setisdead? 1]]
end

to die
;this method makes templeton die melodramatically
;to the sound of taps
stopmoving
launch [spin-around twitch]
play-taps
end

to reset
;this method resets the variables to 0 so the game can
;begin again
setisdead? 0
setnumber-hits 0
end

to spin-around
;this method makes templeton spin in a circle
leftwheel thatway onfor 60
off
end

to twitch
;this method makes templeton move quickly
;forward and backward
repeat 5 [forwardfor 2 backwardfor 2]
end

to hitwall?
;this method monitors the touch sensors on the front of
;templeton. if either of them hits something, it is true.
output (or (switch 7) (switch 8))
end

to catch?
;this monitors the reflectance sensor that is in the cone
;to see if there is a ball there.
output (sensor 0) < 60
end

to throw
;this method turns on the motor to throw the ball back
catapult thisway onfor 12
end

to forwardfor :time
;this method moves templeton forward for a specified
;period of time (in tenths of seconds)
bothwheels thisway onfor :time
end

to backwardfor :time
;this method moves templeton backward for a specified
;period of time (in tenths of seconds)
bothwheels thatway onfor :time
end

to stopmoving
;this method stops templeton
bothwheels off
end

to forward
;this method starts templeton moving forward
bothwheels thisway on
end

to backward
;this method starts templeton moving backward
bothwheels thatway on
end

to turn-left
;this method turns templeton left a random amount
leftwheel thisway off
rightwheel thisway onfor (random 10)
end

to turn-right
;this method turns templeton right a random amount
rightwheel thisway off
leftwheel thisway onfor (random 10)
end

;the following are shortcuts to more easily
;reference the motors

to leftwheel
a,
end

to rightwheel
b,
end

to bothwheels
ab,
end

 

to catapult
c,
end

to play-taps
;this code was taken from Virginia Butler and
;Lisa Lapman's Western Duel robot from 2000.
note 70 7 wait .5
note 70 3 wait .5
note 75 13 wait 3
note 70 7 wait .5
note 75 3 wait .5
note 79 13 wait 3
repeat 2 [note 70 5 wait .5
note 75 3 wait .5
note 79 5 wait 3]
note 70 5 wait .5
note 75 3 wait .5
note 79 13 wait 3
note 75 7 wait .5
note 79 3 wait .5
note 82 13 wait 3
note 79 7 wait .5
note 75 3 wait .5
note 70 13 wait 3
note 70 7 wait .5
note 70 3 wait .5
note 75 13 wait 3
end