A Turtle Remote Control

Do this on pencilcode.net

A function is a part of a program that does not run until you want it to.

Making A Button Function

Here is how you make a function that is run whenever you click on a button. What does it do?

button "Forward", ->
  fd 10

1. The indented code after the arrow -> is a function that is not run right away.

2. The command button "Forward", function tells the computer to create a button, and then to run the function whenever the button is clicked.

Making A Remote Control

A program can set up any number of button functions:

button "Forward", ->
  fd 10
button "Red", ->
  pen red

Try creating a set of buttons to control the turtle:

Making A Bigger Function

You can create a function with any number of lines of code. In CoffeeScript, it is important to indent a function neatly when you do this.

button "Star", ->
  for [1..5]
    fd 100
    rt 144
button "Loops", ->
  rt 360, 50
  lt 360, 50