Project
/*Sharon Yang
Section C
junginny
Project-10
*/
var snowman = [];
function setup() {
createCanvas(640, 240);
// create initial snowmen
for (var i = 0; i < 10; i++){
var rx = random(width);
snowman[i] = makeSnowman(rx);
}
frameRate(10);
}
function draw() {
background(105, 58, 29);
displayGround();
updateAndDisplaySnowman();
removeSnowmanThatHaveSlippedOutOfView();
addNewSnowmanWithSomeRandomProbability();
}
function updateAndDisplaySnowman(){
// Update the snowman's positions, and display them
for (var i = 0; i < snowman.length; i++){
snowman[i].move();
snowman[i].display();
}
}
function removeSnowmanThatHaveSlippedOutOfView(){
var snowmanToKeep = [];
for (var i = 0; i < snowman.length; i++){
if (snowman[i].x + 50> 0) {
snowmanToKeep.push(snowman[i]);
}
}
snowman = snowmanToKeep; // the snowmen that are out of the view are removed
}
function addNewSnowmanWithSomeRandomProbability() {
// With the probability of 0.5%, add a new snowman to the end
var newSnowmanLikelihood = 0.005;
if (random(0,1) < newSnowmanLikelihood) {
snowman.push(makeSnowman(width));
}
}
//Update position of snowman every frame
function snowMove() {
this.x += this.speed;
}
// draw the snowmen
function snowDisplay() {
var centerB = 25 * this.size;
var centerU = 15 * this.size;
var bhatW = 35 * this.size;
var thatW = bhatW * 3 / 4;
var eyeW = centerU / 2;
var noseW = eyeW;
noStroke();
fill(255);
push();
translate(this.x, height - 40);
ellipse(this.x, - centerB, centerB * 2, centerB * 2);
ellipse(this.x, -(2 * centerB) - centerU, centerU * 2, centerU * 2);
fill(0);
ellipse(this.x - eyeW, -(2*centerB)-centerU-(3*this.size),5*this.size,5*this.size);
ellipse(this.x + eyeW, -(2*centerB)-centerU-(3*this.size),5*this.size,5*this.size);
fill(255,0,0);
triangle(this.x,-(2*centerB)-centerU,this.x+noseW,-(2*centerB)-centerU+2*this.size,this.x,-(2*centerB)-centerU+5*this.size);
fill(0);
rect(this.x - (thatW / 2),-(2*centerB) - 2*(5*centerU/6), thatW, - 20 * this.size);
rect(this.x - (bhatW / 2),-(2*centerB) - 2*(5*centerU/6), bhatW, - 5 * this.size);
pop();
}
//function for making snowman
function makeSnowman(birthLocationX) {
var snowman = {x: birthLocationX,
speed: -1.0,
move: snowMove,
display: snowDisplay,
size: round(random(1,2))}
return snowman;
}
//the ground is displayed
function displayGround(){
fill(121, 186, 209);
rect(0, 0, width, height - 50);
}
For this project, I started with the template code, which I changed to make different objects. I played around with making the snowmen. I really understood how arrays in functions are used for greater efficiency. The following is the sketch for the project.