xxxxxxxxxx
let bubbles = [];
let x = 0;
function setup() {
createCanvas(windowWidth,windowHeight);
background(x);
for(let i = 0; i < 480; i++){
let x = random(width);
let y = random(height);
let s = 40;
let l = floor(random(0,1.5));
let b = new Bubble(x, y, s, l);
bubbles.push(b);
}
}
function draw() {
//background(20);
for(let b of bubbles){
b.count = 0;
b.move();
b.show();
for(let other of bubbles){
if (b != other && b.intersect(other) && other.living == 1){
b.flee(other.x,other.y);
b.count += 1;
}
}
if(b.count > 3){
b.b = 0;
b.c = 0;
b.d = 0;
b.living = 0;
}
else if(b.count > 1){
b.b = 255;
b.c = 255;
b.d = 255;
b.living = 1;
}
else{
b.b = 0;
b.c = 0;
b.d = 0;
b.living = 0;
}
//b.flee(mouseX,mouseY);
}
}
class Bubble {
constructor(x, y, r, l) {
this.x = x;
this.y = y;
this.r = r;
this.count = 0;
this.living = l;
this.b = 255;
this.c = 255;
this.d = 255;
}
move() {
let m = 10;
if(this.x < width && this.x > 0){
this.x = this.x + random(-5, 5);
}
else{
if(this.x <= 0){
this.x +=m;
}
else{
this.x -=m;
}
}
if(this.y < height && this.y > 0){
this.y = this.y + random(-5, 5);
}
else{
if(this.y <= 0){
this.y +=m;
}
else{
this.y -=m;
}
}
}
show() {
noStroke();
strokeWeight(1);
fill(this.b, this.c, this.d);
rect(this.x, this.y, this.r, this.r);
}
intersect(other) {
let d = dist(this.x, this.y, other.x, other.y);
return (d < (this.r/1.2 + other.r));
}
flee(px, py) {
let d = dist(px, py, this.x, this.y);
if(d < (this.r + 100)){
if(this.x < width && this.x > 0){
if(this.x < px){
this.x -= 10;
}
else{
this.x += 10;
}
}
if(this.y < height && this.y > 0){
if(this.y < py){
this.y -= 10;
}
else{
this.y += 10;
}
}
}
}
}