A new turtle to try a few thing, like, classes, and if several turtles can walk together.

Log in to post a comment.

// You can find the Turtle API reference here: https://turtletoy.net/syntax Canvas.setpenopacity(0.5); // Global code will be evaluated once. let qty = 200; //min = 1, max = 500, step = 1 const useCircle = 0; //min = 0, max = 1, step = 1, (straight, circle) //const turtle = new Turtle(); class Wanderer{ constructor(){ this.turtle = new Turtle(); this.turtle.setheading(Math.random() * 360); this.x = 0; this.y = 0; } walk(){ this.turtle.setheading(this.turtle.heading() + Math.random() * 20 - 10); this.turtle.forward(Math.random() * 5); } circle(){ this.turtle.circle(Math.random() * 40 - 20, Math.random() * 30); } } const family = []; for(let i = 0; i < qty; i++){ family[i] = new Wanderer; } // The walk function will be called until it returns false. function walk(i) { for (let item of family){ if(useCircle) item.circle(); else item.walk(); } return i < 50; }