xxxxxxxxxx
70
let balls = [];
let mouse
var n = 10
class ball{
constructor(x, y ,r){
this.x = x;
this.y = y;
this.ra = r;
this.r = 220
this.g = 220
this.b =220
}
intersects(mouse){
let d = dist(mouse.x, mouse.y, this.x, this.y);
if(d < n + this.ra){
return true
}
else{
return false
}
}
changeColor(){
this.r = random(255)
this.g = random(255)
this.b = random(255)
}
move(){
this.y += random(-1, 1);
this.x += random(-1, 1);
}
show(){
stroke(10)
strokeWeight(1);
fill(this.r, this.g, this.b);
ellipse(this.x, this.y, this.ra, this.ra); }}
function setup(){
createCanvas(600, 600);
mouse = new ball(400, 200, n)
for(let i = 0; i < 100; i++) {
x = random(width)
y = random(height)
r = random(10, 50)
balls[i] = new ball(x, y, r)
}
}
function draw(){
background(220);
mouse.x = mouseX
mouse.y = mouseY
mouse.show()
mouse.move()
for(b of balls){
b.move()
b.show()
if (mouse.intersects(b)){
b.changeColor()
mouse.changeColor()
}
}
}