xxxxxxxxxx
82
let balls = [];
class ball{
constructor(x, y ,r){
this.x = x;
this.y = y;
this.ra = r;
this.r = 220
this.g = 220
this.b = 220
}
move(){
this.y += random(-1, 1);
this.x += random(-1, 1);
}
clicked(px, py){
let d = dist(px, py, this.x, this.y);
if(d < this.ra){
return true
}
else{
return false
}}
rollover(x, y){
let d = dist(x, y, this.x, this.y);
if(d < this.ra){
this.r = random(255)
this.b = random(255)
this.g = random(255)
}
else{
return false
}}
show(){
stroke(10)
strokeWeight(1);
fill(this.r, this.g, this.b);
ellipse(this.x, this.y, this.ra, this.ra);
}
click(x, y){
let d = dist(x, y, this.x, this.y);
if(d < this.ra){
this.r = random(255)
this.g = random(255)
this.b = random(255)
}}}
function setup() {
createCanvas(600, 600);
for(let i = 0; i < 100; i++) {
balls[i] = new ball(random(width), random(height), random(10, 40))
}
}
function mousePressed() {
for(let i = 0; i < balls.length; i++){
if (balls[i].clicked(mouseX, mouseY)){
balls.splice(i, 1);
}}
}
function draw() {
background(220);
for(let i = 0; i < balls.length; i++) {
if (balls[i].rollover(mouseX, mouseY)){
balls.click
}
balls[i].move()
balls[i].show()
}
}