//Name: Alessandra Fleck
//Class Section : B
//Email: afleck@andrew.cmu.edu
//Project-10
var cake = [];
function setup() {
createCanvas(480, 480);
// the initial collection of cake
for (var i = 0; i < 10; i++){ //automatically counts up
var rx = random(width);
cake[i] = makeCake(rx);
}
frameRate(100);
}
function draw() {
background(80,177,198);
displayHorizon();
updateAndDisplayCake();
removeCakeThatHaveSlippedOutOfView();
addNewCakeRandomly();
//bottom table
fill(225,229,194);
stroke(0);
strokeWeight(3);
rect(0,430,480,100);
//bear eating cake
//bear head
fill(225,229,194);
stroke(0);
noStroke();
ellipse(50,350,150,150);
//bear ear
fill(225,229,194);
ellipse(30,260,50,80);
//bear cheek
fill(225,200,190);
ellipse(30,350,50,50);
//bear mouth
fill(80,177,198);
ellipse(100,380,60,50);
//bear eye
fill(0);
ellipse(80,330,30,30);
fill(255);
ellipse(80,320,10,10);
//bear nose
fill('red');
ellipse(120,340,20,20);
//bear hand
fill(225,229,194);
ellipse(50,430,80,20);
}
function updateAndDisplayCake(){
// Update the cake's positions, and display them.
for (var i = 0; i < cake.length; i++){
cake[i].move();
cake[i].display();
}
}
function removeCakeThatHaveSlippedOutOfView(){
var cakeToKeep = [];
for (var i = 0; i < cake.length; i++){
if (cake[i].x + cake[i].breadth > 0) {
cakeToKeep.push(cake[i]);
}
}
cake = cakeToKeep; // remember the surviving buildings
}
function addNewCakeRandomly() {
// With a small margin of probability, add another cake
var newCakeLikelihood = 0.010;
if (random(0,1) < newCakeLikelihood) {
cake.push(makeCake(width));
}
}
function cakeMove() {
// update cake position
this.x += this.speed;
}
// draw cake
function cakeDisplay() {
var cakeHeight = 40;
fill(255);
noStroke();
push();
translate(this.x, height - 40);
//cake bottom
fill(197,174,135);
rect(40,-cakeHeight,50,30);
//cake middle
fill(220,157,155);
rect(40,-cakeHeight -50,50,60);
//cake middle
fill(220,157,155);
rect(40,-cakeHeight -50,50,60);
//cake top
fill(197,174,135);
rect(40,cakeHeight-150, 50,60);
//cake frosting
fill(250);
rect(40,cakeHeight-150, 50,10);
pop();
}
function makeCake(birthLocationX) {
var cake = {x: birthLocationX,
breadth: 10,
speed: -0.5,
move: cakeMove,
display: cakeDisplay}
return cake;
}
function displayHorizon(){
stroke(0);
line (0,height-50, width, height-50);
}
For this assignment I just wanted to do a conveyor belt with a bear eating cake that came at different lengths.
As seen in the sketch below, I wasn’t able to add toppings to the cake or get the cake to disappear as it entered the bear’s mouth. If I were to go back and edit the script, I would make it so that the vanishing point is set at the bear’s mouth and not the edge of the canvas.