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.
roundtrackis a function that draws a circular track.
loopytrackdraws a figure-8 track.
rungamelets you drive the turtle.
replayreloads the webpage to start over.
Code indented under a definition like
rungame = -> becomes a function
that can be run using
do location.reload reloads the webpage.
Interactive gadgets like
button work by calling
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:
button also uses a function: when the
button is clicked, the function
replay is run.