mdambruc-Project-10

sketch

//Mairead Dambruch
//mdambruc@andrew.cmu.edu
//Section C
//Project 10
var seaweed = [];
var speed = .5;
var fishes = [];
function setup() {
  createCanvas(600, 400);
  //creating random occurrences of seaweed placement
  for (var i = 0; i < 50; i++){
  var rx = random(width);
  var ry = random(height);
  seaweed[i] = makeSeaweed(rx);
  fishes[i] = makeFishes(rx, ry);
  }
  frameRate(5);
  }

function draw() {
  background(color(10, 75, 88));
  //draw sand
  sand();
  //draw and update seaweed
  showUpdateSeaweed();
  removeOldSeaweed();
  addSeaweed();
  //draw fishes
  showUpdateFishes();
  removeOldFishes();
  addFishes();
}
//creates new seaweeds at beginning of image
function showUpdateSeaweed(){
  for (var i = 0; i < seaweed.length; i++){
  seaweed[i].move();
  seaweed[i].display();
  }
  }
//creates new fishes at beginning of image
function showUpdateFishes(){
  for (var i = 0; i < fishes.length; i++){
  fishes[i].move();
  fishes[i].display();
  }
  }
//removes seaweed when seaweed goes past canvas
function removeOldSeaweed(){
  var keepSeaweed = [];
  for (var i = 0; i < seaweed.length; i++){
  if (seaweed[i].x + seaweed[i].breadth > 0) {
      keepSeaweed.push(seaweed[i]);
    }
    }
  seaweed = keepSeaweed;
  }
//removes fishes once out of canvas
function removeOldFishes(){
    var keepfishes = [];
    for (var i = 0; i < fishes.length; i++){
    if (fishes[i].x > 0) {
      keepfishes.push(fishes[i]);
      }
      }
    fishes = keepfishes;
    }
function addSeaweed(){
  //will create seaweed 50% of the time
  var newSeaweed = 0.5;
  if (random(0,1) < newSeaweed){
     seaweed.push(makeSeaweed(width));
  }
  }
function addFishes(){
  var newFishes= 0.5;
  if (random(0,1) < newFishes){
       fishes.push(makeFishes(width, random(5, 10)));
  }
  }
//allows for seaweed to move offscreen
function moveSeaweed(){
  this.x += this.speed;
  }
function moveFishes(){
  this.x += this.speed;
  this.y += this.speed;
  }
function showSeaweed(){
  //places seaweed at proper place in sand
  var sandHeight = 5;
  var offset = 15;
  //created randomized height of seaweed
  var weedHeight = this.nGrowth * sandHeight;
  strokeWeight(1);
  push();
  translate(this.x, height - offset);
  fill("Green");
  rect(0, -weedHeight, this.breadth, weedHeight);
  stroke(50);
  var bubbleLen = 3;
  var bubblestart = 5;
  var bubbleend = 10;
  //creates bubbles flowing out of seaweed
  for (var i = 0; i < this.nGrowth; i++) {
  fill("lightBlue");
  noStroke();
  ellipse(bubblestart, -bubbleend - (i * weedHeight), bubbleLen, bubbleLen);
}
  pop();
}
//draws fishes
function showFishes(){
  push();
  translate(this.x, this.y);
  noStroke();
  fill("Salmon");
  //fish body
  ellipse(0, 0, 15, 10);
  //fishtail
  triangle(5, 0, 14, 10, 14, -5);
  //fisheye
  ellipse (2, 2, 2, 2);
  pop();
}
function makeSeaweed(LocationX) {
    var sweed = {x: LocationX,
                breadth: 5,
                speed: -10,
                nGrowth: round(random(2,20)),
                move: moveSeaweed,
                display: showSeaweed}
    return sweed;
  }
function makeFishes(LocationX, LocationY) {
      var fishes = {x: LocationX,
                  y : LocationY,
                  breadth: 10,
                  speed: -random(1, 10),
                  move: moveFishes,
                  display: showFishes}
      return fishes;
    }
function sand(){
  strokeWeight(5);
  for (var i = 0; i < width; i++) {
  //creates randomized terrain
  var gradient = noise(i * .004 + millis() * speed)
  var s = height - .20 * height * gradient;
  stroke(color(240, 229, 144));
  line(i, height, i, s);
}
}

img_5756

For this assignment, I wanted to create an underwater scene which featured a school of fish passing by. Being hesitant with objects, I started to begin simply with them by creating the seaweed. I then added the bubbles coming out of the seaweed. Then, I added the fishes as objects, in attempts to further understand how objects work. With every new element in the animation, I grew to be more comfortable with objects and how they interact – both to each other and to surrounding code. I’m looking forward to being able to create more animations with ease now using objects. Prior to this assignment I was confused by the uses of “this” and how helper functions were used. After this assignment, I am more comfortable with objects.

Leave a Reply