xxxxxxxxxx
//initial variables
int num=20;
Circle[] circle1;
Circle[] circle2;
void setup() {
size(400, 400);
smooth();
//creating instances of our circles
circle1 = new Circle[num];
circle2 = new Circle[num];
for (int i=0; i<num; i++){
circle1[i] = new Circle(random(20,230),random(20,230),random(20,230),true,random(20,380),random(20,380));
circle2[i] = new Circle(random(20,230),random(20,230),random(20,230),false,random(20,380),random(20,380));
}
}
void draw() {
fill(0,15);
rect(0,0,width,height);
noStroke();
//moving and displaying the circles
for (int i=0; i<num; i++){
circle1[i].display();
circle1[i].move();
circle2[i].display();
circle2[i].move();
//when the circles collide, change their color
float d = dist(circle1[i].x, circle1[i].y, circle2[i].x, circle2[i].y);
if(d<=circle1[i].rad*2){
circle1[i].colorchange();
circle2[i].colorchange();
}
}
}
class Circle{
float r;
float g;
float b;
float x;
float y;
float speed=1;
boolean dir = true;
float rad = 20;
Circle(float rr, float gg, float bb, boolean dirr, float tempX, float tempY){
r=rr;
g=gg;
b=bb;
dir = dirr;
x = tempX;
y = tempY;
}
void display(){
fill(r,g,b);
ellipse(x,y,rad,rad);
}
void move(){
if (dir == true){
x+=speed;
}else if (dir == false) {y+=speed;
}
//if the balls hit the edge of the canvas
if(x>width-rad/2 || x<rad/2){
speed=-speed;
}
if(y>height-rad/2 || y<rad/2){
speed=-speed;
}
}
void colorchange() {
r=random(0,255);
g=random(0,255);
b=random(0,255);
}}