Turtan

Shoots a turtle in a random direction and bounces it around.

Created by maraz on 2018/12/22
77
0

Log in to post a comment.

Canvas.setpenopacity(0.5);
const iters = 2000000+100000*Math.round(Math.random()*3);
const size = 95;

const turtle = new Turtle();
const startSide = Math.random() > 0.5;
let ax, ay;
let turned = 0;

turtle.penup();
turtle.goto(startSide ? (Math.random() > 0.5 ? -size+1 : size-1) : size * Math.random(), !startSide ? (Math.random() > 0.5 ? -size+1 : size-1) : size * Math.random());
turtle.seth(Math.random()*360);
turtle.pendown();

function walk(i) {
    ax = Math.round(Math.abs(turtle.x()));
    ay = Math.round(Math.abs(turtle.y()));
    if (ay === size && ax === size && turned < i-1) {
        turtle.seth(180+turtle.h());
        turned = i;
    }
    else if (ax === size && turned < i-1) {
        turtle.seth(180-turtle.h());
        turned = i;
    }
    else if (ay === size && turned < i-1) {
        turtle.seth(360-turtle.h());
        turned = i;
    }
    turtle.forward(0.5);
    return (i < iters || !(ax === size || ay === size));
}