xxxxxxxxxx
100
//And so they swam under the setting sun, on and on...
//Create Function for Scenery
function scenery(){
noStroke();
//Sunset Sky
for (let i = 0; i < 200; i = i + 2){
fill(255, 170 - i/1.5, 85);
rect(0, i, 600, 5);
}
//Sun
fill(255, 190, 40);
arc(300, 200, 200, 200, radians(180), radians(0));
//Sea
for (let i = 0; i < 400; i = i + 4){
fill(100 - i/4, 130 - i/4, 255);
rect(0, i + 200, 600, 5);
}
}
//Create Array for Fish
let fish = [];
//Create Class for Fish
class Fish{
constructor(){
this.xPos = random(0, 600);
this.yPos = random(230, 580);
this.xSpeed = random(-5, -1);
this.ySpeed = random(-2, 2);
this.rValue = random(255);
this.gValue = random(255);
this.bValue = random(255);
}
//Function for Drawing Fish
draw(){
fill(this.rValue, this.gValue, this.bValue);
//Body
ellipse(this.xPos, this.yPos, 60, 30);
//Tail
triangle(this.xPos + 10, this.yPos, this.xPos + 40, this.yPos - 15, this.xPos + 40, this.yPos + 15);
//Eye
fill(0);
ellipse(this.xPos - 20, this.yPos - 4, 5);
//Smile
arc(this.xPos - 30, this.yPos, 15, 7, radians(0), radians(90));
}
//Function for Fish Movement
move(){
this.xPos += this.xSpeed;
//Check to Stop Fish from Leaving Water
if (this.yPos > 230){
this.yPos += this.ySpeed;
}
else {
this.ySpeed = random(0, 1);
this.yPos += this.ySpeed;
}
//Check to Stop Fish from Staying Below Frame
if(this.yPos < 610){
this.yPos += this.ySpeed;
}
else {
this.ySpeed = random(-1, 0);
this.yPos += this.ySpeed;
}
}
//Function for Resetting + Rerolling Values of Fish Out of Frame
reset(){
if(this.xPos < -50){
this.xPos = 650;
this.yPos = random(230, 580);
this.xSpeed = random(-5, -1);
this.ySpeed = random(-2, 2);
this.rValue = random(255);
this.gValue = random(255);
this.bValue = random(255);
}
}
}
function setup() {
createCanvas(600, 600);
//Generate Multiple Values in Array for Multiple Fish
for (let i = 0; i < 15; i++){
fish[i] = new Fish();
}
}
function draw() {
//Draw In Scenery
scenery();
//Draw In Fish
for (let i = 0; i < 15; i++){
fish[i].move();
fish[i].draw();
fish[i].reset();
}
}
//An unsolicited comment. Just to top this off at a 100 lines.