Forever and Stop
Happy new year!
Here is a new year's resolution: by July 2015, we will freeze the Pencil Code interface so that we can build curriculum without worrying about removed features. So mail me any ideas of things that need to be changed in Pencil Code before then - info@pencilcode.net.
forever
The first change for 2015 is forever
.
forever
lets you repeat something indefinitely. Put an
arrow ->
after it, then indent code that you want to
repeat.
forever -> fd 2 rt 2
It is useful for making animations. The previous version
of this function was called tick
. The difference is
that forever
lets you set up more than one loop to
run at once:
forever -> fd 2 rt 2 forever -> pen random color
The two processes run in parallel forever, so the pen continually changes color as the turtle moves.
stop()
nothing is forever
Inside a forever
loop, stop()
will stop the repetitions.
The following program will run the forever loop, watching for
the w
and down s
keys on the keyboard. One of them will
increase v
and make the turtle move. The other will stop
the forever loop.
v = 0 forever -> fd v if pressed 'w' v = v + 0.1 if pressed 's' stop()
forever
loops are different from for
and while
loops.
forever
is a function whose loops are stopped by
calling the function stop()
.
for
and while
are loops that are built-in to the language. To
stop those loops, use the built-in command break
.
The speed of forever
The forever
function can be set to repeat at any number of
repetitions per second. Just add an optional number before
the arrow:
forever 1, -> dot random color rt 30 fd 25
The number is the number of repetitions per second.