The Beginning and the End

A cloud of wobbly turtles.

Log in to post a comment.

Canvas.setpenopacity(-0.025);

const turtle = new Turtle();
const initialSpeed = 2.5;
const size = 2;
const maxEnergy = 200000;
const epsilon = 0.0001;

turtle.penup();
turtle.goto(-33, 25);
turtle.pendown();
turtle.speed = initialSpeed;
turtle.energy = maxEnergy;

let turtles = [turtle];

function walk(i) {
    turtles.forEach((t, idx) => {
        const sine = Math.sin(Math.PI/1.525+t.energy/maxEnergy*t.speed*i/0.0001);
        const dir = Math.round(sine);
        t.forward(size);
        t.right(t.speed*60*sine);
        if (Math.abs(sine) + epsilon >= 1) {
            const newTurtle = t.clone();
            newTurtle.speed = t.speed + 0.1;
            newTurtle.energy = t.energy / 2;
            newTurtle.seth(t.h()-dir*t.h()%90 - dir*90 + 30);
            turtles.push(newTurtle);
        }
        t.energy *= 0.996;
        if (t.energy < 10 || t.x() >= 100 || t.x() <= -100 || t.y() >= 100 || t.y() <= -100) {
            turtles[idx] = null;
        }
    })
    turtles = turtles.filter(t => t != null);
    return turtles.length > 0;
}