xxxxxxxxxx
81
const csrs = [ARROW, BALL, DART, DIAMOND, HEXAGON, STAR, TRIANGLE, TURTLE];
let turtle, cx = 170, cy = 170, size = 300;
let ir = 30, or = 140, np = 11, ps = 2.5, dir = LT;
let dc;
function setup() {
createCanvas(340, 400);
cursor(CROSS);
textSize(16);
textAlign(CENTER, CENTER);
dc = drawingContext;
turtle = TG.getTurtle(300, 300);
initTurtle(turtle);
}
function initTurtle(t) {
let nsides = [8, 7, 6, 5, 4, 3];
let rf = [0.48, 0.4, 0.34, 0.27, 0.2, 0.12];
let pcols = ["blue", "red", "green", "darkgoldenrod", "purple", "chocolate"];
let fcols = ["lightblue", "pink", "lightgreen", "lightgoldenrodyellow",
"lavender", "cornsilk"];
// For each polygon type
for (let i = 0; i < nsides.length; i++) {
// Calculate enclosing circle / oval radius for polygon
let rad = rf[i] * size;
// Calculate starting point and heading for random orientation about
// the turtle graphic centre
let ang = random(0.0, 360.0),
px = rad * cos(radians(ang)),
py = rad * sin(radians(ang));
t.pu()
.xy(px, py)
.face(ang + 90)
.pensize(3)
.pencolor(pcols[i])
.fillcolor(fcols[i])
.pd()
.st()
.begin_fill()
.circle(rad, 360, RT, nsides[i])
.end_fill()
.ht()
.sleep(100);
}
t.sleep(1000).clear().do(initTurtle);
t.start();
}
function draw() {
background("skyblue");
fill(255);
stroke(0);
strokeWeight(3);
rectMode(CENTER);
rect(cx, cy, size, size);
turtle.draw(dc, cx, cy);
// Control keys
fill(0);
noStroke();
translate(width / 2, cy + 24 + size / 2);
text("CONTROL KEYS", 0, 0, width, 20);
text("[B] Pause :: [N] Restart animation", 0, 20, width, 20);
text("[1] - [8] Change turtle cursur", 0, 40, width, 20);
}
function keyTyped() {
switch (key) {
case "c":
console.clear();
break;
case "n":
turtle.start();
break;
case "b":
turtle.stop();
break;
default:
if (key >= "1" && key <= "8") turtle.setCursor(csrs[Number(key) - 1]);
}
}