Galloping Boxes

Try animating turnFac

Log in to post a comment.

// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(0.8);

const gridSize = 10; // min=1, max=20, step=1
const turnFac = 4; // min=-90, max=90, step=1
const step = 3; // min=1, max=9, step=1

// Global code will be evaluated once.
const turtle = new Turtle();
turtle.penup();
turtle.goto(-50,-20);
turtle.pendown();


const rect = (turtle, w, h) => {
    turtle.forward(w);
    turtle.right(90);
    turtle.forward(h);
    turtle.right(90);
    turtle.forward(w);
    turtle.right(90);
    turtle.forward(h);
    turtle.right(90);
}

const rects = (turtle, w, h, step) => {
    while (w > 0 && h > 0) {
        rect(turtle, w, h);
        turtle.penup();
        turtle.forward(step/2);
        turtle.right(90);
        turtle.forward(step/2);
        turtle.left(90);
        turtle.pendown();
        
        w-=step; h-=step;
    }
}


// The walk function will be called until it returns false.
function walk(i,t) {
    const dim = 200 / gridSize | 0;
    const [row, col] = [i % gridSize, i / gridSize | 0 ];
    const [x, y] = [row/gridSize * 200 - 100, col/gridSize * 200 - 100];
    !row && turtle.setheading(0)
    turtle.jump(x, y);
    turtle.right(turnFac + (t -1) * 90)
    //console.log(t)
    rects(turtle, dim-1, dim-1, step);

    return i < gridSize * gridSize -1;
}