roundtrack = ->
dot gold, 400
dot erase, 200
bk 150
rt 90
do rungame
loopytrack = ->
pen gold, 100
fd 100
lt 270, 100
fd 200
rt 270, 100
fd 100
pen null
do rungame
rungame = ->
speed Infinity
tick 60, ->
if pressed "up" then fd 1
if pressed "left" then lt 1
if not touches gold
write "Game Over"
tick null
button "Play Again", replay
replay = ->
do location.reload
menu
"Round Track": roundtrack
"Loopy Track": loopytrack
This version of the race is divided into functions.
roundtrack is a function that draws a circular track.
loopytrack draws a figure-8 track.
rungame lets you drive the turtle.
replay reloads the webpage to start over.
Code indented under a definition like
rungame = -> becomes a function
that can be run using do rungamedo location.reload reloads the webpage.
Interactive gadgets like
menu and button work by calling
functions.
menu sets up a map of choices, listing
a function to run for each choice. When the menu is shown, it
lets you pick exactly one choice to run:
either roundtrack or loopytrack.
button also uses a function: when the
button is clicked, the function replay is run.