Adding a Menu to the Race Game

Do this on pencilcode.net

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.

Code indented under a definition like rungame = -> becomes a function that can be run using do rungame. Built-in functions can be run the same way: do 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.