xxxxxxxxxx
66
// https://discourse.processing.org/t/problem-with-loadimage/14907/4
let bubble = [];
let flower;
let kitten = [];
function preload() {
flower = loadImage("flower.png");
for (let i = 0; i < 5; i++) kitten[i] = loadImage("kitten" + i + ".jpg");
}
function setup() {
createCanvas(600, 400);
for (let i = 0; i < 20; i++) {
let x = random(width);
let y = random(height);
let r = random(10, 50);
bubble[i] = new Bubble(x, y, r);
}
}
function draw() {
background(0);
for (let b of bubble) {
b.show();
b.move();
let overlapping = false;
for (let other of bubble)
if (b !== other && b.intersects(other)) overlapping = true;
if (overlapping) b.changeColor(255);
else b.changeColor(0);
}
}
class Bubble {
constructor(x, y, r = 50) {
this.x = x;
this.y = y;
this.r = r;
this.brightness = 0;
}
intersects(other) {
let d = dist(this.x, this.y, other.x, other.y);
return (d < this.r + other.r);
}
changeColor(bright) {
this.brightness = bright;
}
contains(px, py) {
let d = dist(px, py, this.x, this.y);
return (d < this.r);
}
move() {
this.x = this.x + random(-2, 2);
this.y = this.y + random(-2, 2);
}
show() {
image(kitten[0], this.x, this.y, this.r, this.r);
}
}