Pencil Code Reference > speed


speed sets turtle speed in moves per second

Control the speed of the turtle using speed.

speed is one of the commands used for controlling animation.

Default Speed

The starting default speed for turtles is one move per second. The program below does not specify a speed, so it takes one second to draw each dot, and another second to move forward between each dot. Since the program repeats those two motions 8 times, the turtle takes 8×2=16 seconds total to complete its animation.

for [1..8]
  dot random(color), 10
  fd 25

Five moves per second:

speed 5

Ten moves per second:

speed 10

As fast as your computer can compute (often looks instant, and thus is used in frame-based animation):

speed Infinity

Turtles with Diffent Speeds

If you have more than one turtle, they will all animate simultaneously.

Each one can set its own speed using the per-turtle method t.speed. The global function speed controls the default speed for all turtles, used when they have not set their own speed.

speed Infinity
a = (new Turtle for [0..8])
for t, j in a
  t.moveto 0, j*25 - 100
  t.rt 90
  if j > 0
    t.speed j
  for [1..8] random(color), 10
    t.fd 25

How Turtle Speed Works

Each turtle is a jQuery object that animates using the jQuery animation queue.

When you tell a turtle to take a sequence of motions, it makes a list of all the motions right away, but it does not do them until later. When your program finishes running, the turtle starts working through its list, using some time for each command. Each turtle has its own list, and its own speed.

That means that a program that creates a 20-second animation is actually done running almost instantly, because all it needs to do is give the turtle a list of commands! The turtle walks through the motions after the program is done sending all the commands. You can see the effect writing output to the screen using a function that runs immediately without using the jQuery animation queue, like log:

pen red
for j in [1..5]
  rt 360, 50
  log "#{j}: sent rt 360, 60"
  rt 144
  log "#{j}: sent rt 144"

If you need to run code later, after the turtle has moved, you can use the plan command to put your own code on the jQuery animation queue.

By default, turtle motions animate by taking the number of milliseconds in the special jQuery variable $.fx.speeds.turtle. This number starts at 1000 milliseconds (1 second), which is pretty slow.

The speed function changes this number. For example, calling "speed 5" chages $.fx.speeds.turtle to 1000/5 = 200 milliseconds, speeding up all turtles to take 5 steps per second.

When individual turtles override their own speed, their speed is stored in the pseudo-CSS property 'turtleSpeed'. The following does the same thing as turtle.speed 10.

turtle.css turtleSpeed: 100

Going Really Fast

speed Infinity sets the animation time to zero, which means turtle motion occurs immediately, as soon as you run it (as long as there is nothing else already ahead of it in the queue).

When making an interactive program where you need to be aware of the turtle position at any instant, you are likely to want to use speed Infinity at the beginning of your program. That way every turtle movement command happens immediately, and you can know where the turtle is going to be right away. This technique is often used with the tick command, which lets you create your own animation frames.

If you use a turtle speed that is slower than Infinity, jQuery will move turtles on its own time, some time after you have requested the motion. That can sometimes have surprising effects.

For example, speed 10000000 does not actually go any faster than speed 100. That is because the master jQuery animation timer uses $.fx.interval for the frame duration, and the default value is 13 milliseconds (about 77 frames per second). If you wish to move faster than speed 77 (but slower than Infinity), then you can experiment with changing this number.