sajohnso-project 11

turtleart


/*
*Sadie Johnson
*15-104 Section C
*sajohnso@andrew.cmu.edu
*Project 11
*/

var t1, t2, t3, t4;
function setup() {
    createCanvas(400, 400);
        background('white');

    t1 = makeTurtle(width / 2 + random(-100, 100), height / 2 + random(-100, 100));
    t2 = makeTurtle(width / 2 + random(-100, 100), height / 2 + random(-100, 100));
    t3 = makeTurtle(width / 2 + random(-100, 100), height / 2 + random(-100, 100));
    t4 = makeTurtle(width / 2 + random(-100, 100), height / 2 + random(-100, 100));

    t1.setColor('#C7E094');
    t2.setColor('#4B845C');
    t3.setColor('#4B845C');
    t4.setColor('#4B845C');

    t2.setWeight(6);
    t3.setWeight(7);
    t4.setWeight(8);

    frameRate(80);
}
 
function draw() {
            background('white');
    turtleDesign(t1.x,t1.y);
    t1.forward(1);
    t2.forward(1.3);
    t3.forward(1.6);
    t4.forward(1.9);
    var targetX = mouseX;
    var targetY = mouseY;
    t1.turnToward(targetX, targetY, 1);
    t2.turnToward(targetX, targetY, 1.3);
    t3.turnToward(targetX, targetY, 1.6);
    t4.turnToward(targetX, targetY, 2);
   // t1.left(random(-5, 5));
}

function turtleDesign(turtleX, turtleY) {
    var shellWidth = 30;
    var shellHeight = 35;

    //draw the turtle's head
    stroke('#8BCA79');
    fill('#8BCA79'); //dark green
    ellipse(turtleX, turtleY-22, 15, 20);

        //draw the turtle's limbs
    strokeWeight(9);
    stroke('#8BCA79'); //dark green
    line(turtleX, turtleY, turtleX+17, turtleY-17); //front right leg
    line(turtleX, turtleY, turtleX-17, turtleY+17);//back left leg
    line(turtleX, turtleY, turtleX+17, turtleY+17); //back right leg
    line(turtleX, turtleY, turtleX-17, turtleY-17); //front left leg

    //draw the turtle's shell
    strokeWeight(4);
    stroke('#C7E094'); //light green
    ellipse(turtleX, turtleY, shellWidth, shellHeight);

    //design on the turtle's shell
    quad(turtleX-5, turtleY, turtleX, turtleY+5, turtleX+5, turtleY, turtleX, turtleY-5);
    strokeWeight(2);
    line(turtleX-5, turtleY, turtleX-(shellWidth/2), turtleY);
    line(turtleX, turtleY-5, turtleX, turtleY-(shellWidth/2));
    line(turtleX+5, turtleY, turtleX+(shellWidth/2), turtleY);
    line(turtleX, turtleY+5, turtleX, turtleY+(shellWidth/2));

    //draw the turtle's eyes
    noStroke();
    fill('#4B845C');//darkest green
    ellipse(turtleX-4, turtleY-25, 3, 3);
    ellipse(turtleX+4, turtleY-25, 3, 3);

}


function turtleLeft(d){this.angle-=d;}function turtleRight(d){this.angle+=d;}
function turtleForward(p){var rad=radians(this.angle);var newx=this.x+cos(rad)*p;
var newy=this.y+sin(rad)*p;this.goto(newx,newy);}function turtleBack(p){
this.forward(-p);}function turtlePenDown(){this.penIsDown=true;}
function turtlePenUp(){this.penIsDown = false;}function turtleGoTo(x,y){
if(this.penIsDown){stroke(this.color);strokeWeight(this.weight);
line(this.x,this.y,x,y);}this.x = x;this.y = y;}function turtleDistTo(x,y){
return sqrt(sq(this.x-x)+sq(this.y-y));}function turtleAngleTo(x,y){
var absAngle=degrees(atan2(y-this.y,x-this.x));
var angle=((absAngle-this.angle)+360)%360.0;return angle;}
function turtleTurnToward(x,y,d){var angle = this.angleTo(x,y);if(angle< 180){
this.angle+=d;}else{this.angle-=d;}}function turtleSetColor(c){this.color=c;}
function turtleSetWeight(w){this.weight=w;}function turtleFace(angle){
this.angle = angle;}function makeTurtle(tx,ty){var turtle={x:tx,y:ty,
angle:0.0,penIsDown:true,color:color(128),weight:1,left:turtleLeft,
right:turtleRight,forward:turtleForward, back:turtleBack,penDown:turtlePenDown,
penUp:turtlePenUp,goto:turtleGoTo, angleto:turtleAngleTo,
turnToward:turtleTurnToward,distanceTo:turtleDistTo, angleTo:turtleAngleTo,
setColor:turtleSetColor, setWeight:turtleSetWeight,face:turtleFace};
return turtle;}

Leave a Reply