cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              var waterScene;
var fishArray = [];
var bubbles = [];
var shark;
var shImg;
function setup(){
  createCanvas(500, 500);
  shImg = loadImage("https://vignette.wikia.nocookie.net/pocketgod/images/1/18/Shark_1.png/revision/latest?cb=20090724191650");
  shark = new Shark();
  //set up background image
  waterScene = loadImage("https://images.unsplash.com/photo-1502726299822-6f583f972e02?ixlib=rb-0.3.5&q=85&fm=jpg&crop=entropy&cs=srgb&s=2f1cbe94aa8d78f2023544f15deaf17c");
  
  //populate fish array with fish
 for(var i = 0; i < 10; i++){
   fishArray.push(new Fish());
   bubbles.push(new Bubble());
   bubbles.push(new Bubble());
 }
  
}

function draw(){
 background(waterScene);
  shark.draw();
  shark.swim();
  //draw and move all fish in fishArray
 for(var i = 0; i < 10; i++){
  bubbles[i].draw();
  bubbles[i].float();
}
  for(var i = 0; i < fishArray.length; i++){
  fishArray[i].rend();
   fishArray[i].swim();
 }
  for(var i = 10; i < bubbles.length; i++){
   bubbles[i].draw();
    bubbles[i].float();
  }
  
}

//fish constructor
function Fish(){
  this.x = -this.width;
  this.y = random(0,height);
  this.color = color(random(0,255),random(0,255), random(0,255));
  this.width = random(25, 100);
  this.height = this.width*0.55;
  this.speedX = random(1, 4);
  this.speedY = random(-1,1);
  
  
  //draw fish method
  this.rend = function(){
    fill(this.color);
    //tail
    triangle(this.x-(this.width/2), this.y, this.x-this.width, this.y+(this.height/4), this.x-this.width, this.y - (this.height/4))
    //body
    ellipse(this.x, this.y, this.width, this.height);
    
    //eye
    fill(0,0,0);
    ellipse(this.x + (this.width/4), this.y, this.width/10, this.width/10);
  } 
  
  //swimming method
  this.swim=function(){
    if(this.x > width + this.width || this.y > height + this.height/2 || this.y < -this.height/2){
      this.x = 0 - random(this.width, this.width*5);
      this.y = random(0,height);
      this.color = color(random(0,255), random(0,255), random(0,255));
      this.speedX = random(1, 4);
      this.speedY = random(-1,1);
    }
    if(shark.hunting && this.x >= shark.x){
      this.x+=this.speedX*4;
      this.y+=this.speedY*5;
    }else{
    this.x+=this.speedX;
    this.y+= this.speedY;
    }
  }
}


function Bubble(){
  this.x = random(0, width);
  
  this.size = random(5, 20);
  this.y = height + random(this.size, this.size*20);
  this.speed = 
  this.draw = function(){
    fill(255,255,255,75);
    noStroke();
    ellipse(this.x, this.y, this.size)
  }
  
  this.float = function(){
    if(this.y < 0){
      this.y = height + random(this.size, this.size*20);
      this.x = random(0, width);
    }
    this.y-=2;
  }
}

function Shark(){
  this.width = 989/3;
  this.height = 637/3;
  this.x = -this.width; 
  this.y = random(0, height-this.height);
  this.img = shImg;
  this.speed=4;
  this.hunting = false;
  this.draw = function(){
    
    image(this.img, this.x, this.y,this.width, this.height);
  }
  this.swim = function(){
    if(this.x > 0 - this.width/1.5 && this.x < width){
      this.hunting = true;
    }
    if(this.x > width){
      this.hunting = false;
      this.x = 0 - random(this.width, this.width*15);
      this.y = random(0, height-this.height);
    }
    this.x+=this.speed;
  }
}
            
          
!
999px
Loading ..................

Console