xxxxxxxxxx
function Walker() {
this.x = random(width);
this.y = random(height);
this.col = color(255);
this.circleSize = 50;
this.r = this.circleSize / 2;
this.circleSpeed = 1;
this.changeColor= function(){
this.col = color(random(255), random(255), random(255));
}
this.display = function() {
fill(this.col);
ellipse(this.x, this.y, this.circleSize, this.circleSize);
}
this.move = function() {
this.x += round(random(-this.circleSpeed, this.circleSpeed));
this.y += round(random(-this.circleSpeed, this.circleSpeed));
this.x = constrain(this.x, 0, width);
this.y = constrain(this.y, 0, height);
}
this.touching = function(other) {
var d = dist(this.x, this.y, other.x, other.y);
if (d < this.r + other.r){
return true;
}else {
return false;
}
}
}
let walkers = [];
let numWalkers = 20;
function setup() {
createCanvas(400, 400);
for (let i = 0; i < numWalkers; i++){
walkers[i] = new Walker();
}
}
function draw() {
background(220);
for (let i = 0; i < walkers.length; i++) {
walkers[i].move();
walkers[i].display();
for (let j = 0; j < walkers.length; j++){
if (i != j && walkers[i].touching(walkers[j]))
walkers[i].changeColor();
walkers[j].changeColor();
}
}
}