Final Project

For my final project I created an animation of a robot watching a tree grow through different seasons. I was able to create what I proposed in that the eyes watch the tree grow and there is something different about the robot in each season. I modified the tree example from lab so that it would grow at a steady rate. There is not much different between the project and my proposal except that I added a bit to summer and fall that were not mentioned. Overall some aspects of this project were harder to make than I thought, but it was a lot of fun to create.

sketch

//Rebecca Enright
//renright@andrew.cmu.edu
//Section A
//Final Project

//variables for rain/ snow/ leaves locations
var x;
var y;
//variable for ground color
var c;
//create object for umbrella
uX = 215;
uY = 240;
uR = 100;
uS = 50;
var umbrella = {locationX: uX, locationY: uY, left: uR, right: uS};

function setup() {
    createCanvas(600, 400);
    frameRate(10);
    
}

function draw() {
    background(0, 220, 255);
    //create variable for tree growth
    var growth = 0;
    growth = growth + (millis()/4000);
    //create condition to stop growth 
    if (growth > 30) {
    	growth = 30;	
    }

    //create conditionals to change scene
    if (second() <= 14) {
        MakeItRain();
    }
    
    if (second() > 14 & second() < 29 ) {
        summerSun(growth); 
    }

    if (second() >= 29 & second() < 44) {
        autumn(growth);
    }

    if (second() >= 44) {
    	LetItSnow();
    }

    //call tree function
    push();
    translate(400, 350);
    drawBranch(0, growth);
    pop();  
    //call robot functions
    drawRobot(growth);
    
}

//create tree
function drawBranch(depth, len) {
    stroke(215, 150, 60);
    line(0, 0, 0, -len);
    push();
    translate(0, -len);
    drawTree(depth + 1, len);
    stroke(0, 200, 0);
    pop();
    
}

//create tree
function drawTree(depth, len) {
    //variable for tree swaying
    var angle = 5 * (noise(millis()/4000) - 0.5);
    //conditional for number of branches
    if (depth < 10) {
        rotate(radians(-10 + angle));
        drawBranch(depth, len);
        rotate(radians(20));
        drawBranch(depth, len);
    }
}

//create general draw robot function
function drawRobot(growth) {
    //drawRock();
    drawRobotHead(growth);
    drawRobotBody();
}

//function for robot head
function drawRobotHead(growth) {
    //draw robot face
    fill(200);
    rect(200, 250, 25, 25);
    //draw robot eyes
    movingRobotEyes(growth);
    //draw robot smile
    fill(255);
    arc(218, 270, 10, 10, 0, PI);
    //draw robot antenna
    fill(200);
    rect(210, 240, 5, 10);  
}

//function for robot body
function drawRobotBody() {
	//draw neck
    fill(200);
    rect(207, 275, 10, 5);
    //draw stomach
    fill(200);
    rect(200, 280, 20, 30);
    //arm
    fill(200);
    rect(195, 280, 10, 20);
    rect(195, 300, 30, 10);
    //bolts for arm
    fill(190);
    ellipse(200, 285, 5, 5);
    ellipse(200, 305, 5, 5);
    //legs
    fill(200);
    rect(200, 310, 30, 10);
    rect(220, 310, 10, 25);
    //bolt for leg
    fill(190);
    ellipse(225, 315, 5, 5);
    //foot
    fill(200);
    rect(220, 335, 15, 5);
}

//create rock function for robot to sit on
function drawRock() {
    fill(175);
    ellipse(200, 325, 50, 25);
}

//create snow function for winter
function LetItSnow() {
    //draw ellipse
    fill(255);
    //variables for snowflake locations
    //x location
    x = 10 + random(5);
    //y location
    y = 0;
    y = 2 * y; 
    
    //make loop for snowflakes
    for (var i = 0; i < width; i++) {
        sX = x * i;
        sY = y + random(300);

        r = random(5, 10);
        ellipse(sX, sY, r, r);    
    }
    //create snowy ground
    fill(255);
	rect(0, 295, 599, 104);
	//draw red dot for robot antenna
    fill(255, 0, 0);
    ellipse(213, 240, 5, 5);
    //draw scarf
    scarf(); 
    //draw rock to sit on
    drawRock();  
}

//create rain function for spring
function MakeItRain() {
    stroke(0, 0, 255);
    //reset variables for x and y locations
    //x location
    x = 5;
    //y location 
    y = 0;
    y = y + 1;

    //make loop for rain drops
    for (var i = 0; i < width; i++) {
        //rain drop x and y location
        rX = x * i;
        rY = y + random(300);
        line(rX, rY, rX + 5, rY + 5);	
    }
    //reset stroke color
    stroke(0);
    
    //call workUmbrella to open umbrella
    workUmbrella();

    //create ground 
    fill(0, 190, 0);
	rect(0, 295, 599, 104);
	//draw rock to sit on
	drawRock();
}

//create function for umbrella
function workUmbrella() {
    fill(255, 0, 0);
    arc(umbrella.locationX, umbrella.locationY, umbrella.left, umbrella.right, PI, 0); 
    fill(0);
    ellipse(umbrella.locationX, umbrella.locationY - 25, 5, 5);
}

//create function for scarf
function scarf() {
	fill(0, 190, 0);
	rect(200, 275, 25, 5);
	fill(200);
	rect(220, 280, 5, 10);
    fill(0, 190, 0);
	rect(220, 290, 5, 10);
}

//create function for summer
function summerSun(growth) {//lX, lY, lS, lC, len) {
    //create sun
    fill(255, 255, 0);
    ellipse(100, 100, 50, 50);
    //create rays for sun
    push();
    translate(100, 100);
    for (i = 0; i < 8; i++) {
        //create sun rays
        rotate(QUARTER_PI);
        triangle(30, 30, 40, 60, 50, 50);
    }
    pop();
    //create ground
    fill(0, 227, 0);
    rect(0, 295, 599, 104); 
    

    //call leaves
    push();
    translate(400, 350);
    drawBranch2(0, growth);
    pop(); 
    //create flower for antenna 
    flower(); 
    //create rock to sit on
    drawRock();  
}
//create second drawbranch function for green during summer
function drawBranch2(depth, len) {
    stroke(0, 200, 0);
    line(0, 0, 0, -len);
    push();
    translate(0, -len);
    drawTree2(depth + 1, len);
    stroke(0, 200, 0);
    pop();
    
}

//create second drawtree for green during summer
function drawTree2(depth, len) {
    //variable for tree swaying
    var angle = 5 * (noise(millis()/4000) - 0.5);
    //conditional for tree branches
    if (depth < 12) {
        rotate(radians(-10 + angle));
        drawBranch2(depth, len);
        rotate(radians(20));
        drawBranch2(depth, len);
    }
}

//create function for fall
function autumn(growth) {
    //create ground
    fill(0, 210, 0);
    rect(0, 295, 599, 104); 
    //create leaves on ground
    var ln = 10;
    ln = ln + millis()/5000;

    for (i = 0; i < ln; i++) {
        //set random leaf locations around base of tree
        var lx = random(340, 460);
        var ly = random(350, 390);
        //create leaf size
        var ls = 1;
        ls = growth//1 + millis()/5000;
        //draw leaves
        fill(random(255), random(255), 0);
        ellipse(lx, ly, ls/2, ls/2 + 2);
    }
    //create background 
    fallLeafBackground(); 
    //draw  pumpkin for robot to sit on
    drawPumpkin();  
}

//create function for leaf background
function fallLeafBackground() {
    //reassign pixel vairables so m moves by 10
    //and reassign y so that it is randomized
    for (var i = 0; i < width; i++) {
        //leaf locations
        var blx = 25;
        blx = blx * i;
        var bly = random(1,300);
        //draw leaves
        fill(random(255), random(255), 0);
        ellipse(blx, bly, 5, 10);
       
    }

}


//put flower on antenna during summer
function flower() {
    //center
    fill(255, 255, 0);
	ellipse(215, 235, 5, 5);
    //petals
    push();
    translate(215, 235);
	for (var i = 0; i < 10; i++) {
        rotate(QUARTER_PI);
		fill(255);
		ellipse(5, 0, 5, 5);   
	}
	pop();
}

//create function so that robot eyes move
function movingRobotEyes(growth) {
	//variable for robot pupil y location
	var ey = 262;
	ey = ey - (millis()/4000)/10;
    //conditional to limit eye location
	if (ey < 259) {
		ey = 259;
	}

	//draw robot eyes
    fill(255);
    ellipse(210, 260, 10, 10);
    ellipse(225, 260, 10, 10);
    //pupil
    fill(0);
    ellipse(212, ey, 5, 5);
    ellipse(227, ey, 5, 5);
}

function drawPumpkin() {
    fill(255, 150, 0);
    ellipse(200, 325, 50, 25);
    fill(0);
    //eyes
    triangle(190, 325, 195, 320, 200, 325);
    triangle(205, 325, 210, 320, 215, 325);
    //mouth
    rect(190, 330, 25, 2);
    rect(195, 332, 5, 2);
    rect(205, 332, 5, 2);
}






Leave a Reply