Variables for Various Shapes and Sizes

Do this on

Programs use variables to remember numbers or other data.

Changing the Size of Shapes

In this program we use the variable s to represent the size of a shape:

s = 100
click (e) ->
  moveto e
  label s
  dot gold, s
button "Small", ->
  s = 20
button "Medium", ->
  s = 100

In CoffeeScript, it is important to define the variable outside a function if we want all the functions to share it. So our program begins by defining s = 100.

Each time we click a button, the value of s changes. When we click on the screen, dot gold, s draws a dot whose size depends on s.

Using if to Make a Choice

This program uses the variable k to represent the kind of shape we want.

k = 1
click (e) ->
  moveto e
  label k
  if k is 1
    dot gold, 50
  if k is 2
    box gold, 50
button "Dots", ->
  k = 1
button "Boxes", ->
  k = 2

Things to Try