xxxxxxxxxx
154
let space = {
bugs: [],
numBugs: 500,
birds: [],
numBirds: 750
};
function preload() {
mySound = loadSound('For IA2.mp3');
}
function setup() {
noStroke()
createCanvas(1280, 720);
mySound.setVolume(0.75);
mySound.play();
// Create objects
for (let i = 0; i < space.numBugs; i++) {
let bug = new Jitter();
space.bugs.push(bug);
}
for (let i = 0; i < space.numBirds; i++) {
let bird = new Bird();
space.birds.push(bird);
}
}
function draw() {
background(0);
for (let i = 0; i < space.numBugs; i++) {
let bug = space.bugs[i];
bug.move();
bug.display();
}
for (let i = 0; i < space.numBirds; i++) {
let bird = space.birds[i];
bird.move();
bird.display();
}
}
// Jitter class
class Jitter {
constructor() {
this.x = random(0,width);
this.y = random(0,height);
this.diameter = random(-10, 40);
this.size = 1;
this.speed = 0.5;
this.color = {
r: random(100,110),
g: random(100,110),
b: random(100,110)
}
}
move() {
this.x += random(-this.speed, this.speed);
this.y += random(-this.speed, this.speed);
}
display() {
let d = dist(this.x, this.y, mouseX, mouseY);
this.color = {
r: map(d, 200, 0, 0, 255),
g: map(d, 125, 0, 0, 0),
b: map(d, 225, 0, 1, 0)
};
this.color.r = this.color.r + map(d, 200, 100, 75, 200);
this.color.b = this.color.b + map(d, 250, 100, 75, 0);
this.diameter = this.diameter + map(d, 150, 1000, -10, 10);
this. diameter = constrain(this.diameter, -10, 150);
fill(this.color.r,this.color.g,this.color.b);
ellipse(this.x, this.y, this.diameter, this.diameter);
}
}
// bird class
class Bird {
constructor() {
this.x = random(100,1500);
this.y = random(300,1200);
this.maxSpeed = random(2.5,4.5);
this.diameter = random(4,6);
this.vx = 0;
this.vy = 0;
this.ax = 0;
this.ay = 0;
this.acceleration = random(0.03,0.06);
this.color = {
r: 75,
g: 75,
b: 75
}
}
move() {
if (this.y > height) {
this.y = this.y - height;
}
if (this.y < 0) {
this.y = this.y + height;
}
if (this.x > width) {
this.x = this.x - width;
}
if (this.x < 0) {
this.x = this.x + width;
}
if (mouseX > this.x) {
this.ax = this.acceleration;
}
else if (mouseX < this.x) {
this.ax = -this.acceleration;
}
if (mouseY > this.y) {
this.ay = this.acceleration;
}
else if (mouseY < this.y) {
this.ay = -this.acceleration;
}
this.vx = this.vx + this.ax;
this.vx = constrain(this.vx, -this.maxSpeed, this.maxSpeed);
this.vy = this.vy + this.ay;
this.vy = constrain(this.vy, -this.maxSpeed, this.maxSpeed);
this.x = this.x + this.vx;
this.y = this.y + this.vy;
}
disperse() {
this.acceleration = -this.acceleration;
}
display() {
let d = dist(this.x, this.y, mouseX, mouseY);
this.color = {
r: map(d, 175, 0, 120, 255),
g: map(d, 175, 0, 120, 255),
b: map(d, 175, 0, 120, 255)
};
fill(this.color.r,this.color.g,this.color.b);
ellipse(this.x, this.y, this.diameter, this.diameter);
}
}
function mousePressed() {
for (let i = 0; i < space.birds.length; i++) {
let bird = space.birds[i];
bird.disperse();
}
}