xxxxxxxxxx
88
//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));
}
//Ocean
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);
}
//Function for Drawing Fish
draw(){
fill(240, 145, 40);
ellipse(this.xPos, this.yPos, 60, 30);
triangle(this.xPos + 10, this.yPos, this.xPos + 40, this.yPos - 15, this.xPos + 40, this.yPos + 15);
fill(0);
ellipse(this.xPos - 20, this.yPos - 4, 5);
arc(this.xPos - 30, this.yPos, 15, 7, radians(0), radians(90));
}
//Function for Fish Movement
move(){
//Check to Stop Fish from Leaving Water
this.xPos += this.xSpeed;
if (0 > this.yPos > 300){
while (this.ySpeed < 0){
this.ySpeed += 1
}
}
else {
this.yPos += this.ySpeed;
}
//Check to Stop Fish from Staying Below Frame
if(this.yPos < 630){
this.yPos += this.ySpeed;
}
else {
this.ySpeed = random(-1, 0);
this.yPos += this.ySpeed;
}
}
//Function for Resetting Position and Rerolling Speed 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);
}
}
}
function setup() {
createCanvas(600, 600);
//Generate Multiple Values in Array for Multiple Fish
for (let i = 0; i < 10; i++){
fish[i] = new Fish();
}
}
function draw() {
//Draw In Scenery
scenery();
//Draw In Fish
for (let i = 0; i < 10; i++){
fish[i].move();
fish[i].draw();
fish[i].reset();
}
}