Fracturtle

Exploring how turtles breed.

Log in to post a comment.

Canvas.setpenopacity(0.4);

const routeAngle = 80;          // min = 45, max = 90, step = 1
const generations = 10;         // min = 1, max = 12, step = 1

//Turtle.prototype.age = 0;
//Turtle.prototype.gen = 0;

let turtles = [];

function addTurtle(x = 0, y = 0, h = 90){
    let turtle = new Turtle();
    turtle.jump(x, y);
    turtle.seth(h);
    turtle.age = 0;
    turtle.gen = 0;
    
    return turtle;
}

turtles.push(addTurtle(0, 60, -90));
/*
turtles.push(addTurtle(0, -30, -90));
turtles.push(addTurtle(0, 30, 90));
*/

// The walk function will be called until it returns false.
function walk(i) {
    
    let goon = true;
    
    for(turtle of turtles){

        turtle.age++;
        
        turtle.forward(0.1);
        
        if((turtle.age + turtle.gen * 6) > 80){
            turtle.age = 0;
            turtle.gen++;

            if(turtle.gen > generations){
                goon = false;
                break;
            }
            
            let baby = turtle.clone();
            baby.age = turtle.age;
            baby.gen = turtle.gen;
            
            baby.left(90);
            baby.circle(3, routeAngle);
            
            turtle.right(90);
            turtle.circle(-3, routeAngle);
            
            turtles.push(baby);
            
        }
    }
    return goon;
}