xxxxxxxxxx
let num = 10;
let vecLocation = [];
let vecVelocity = [];
let ballColor = [];
let hitNumber = [];
let size = 20;
function judge (AX,AY,BX,BY) {
hit = false;
if(abs(AX-BX)<size){
if(abs(AY-BY)<size){
hit = true;
}
}
return hit;
}
function setup(){
createCanvas(800,800);
frameRate(60);
for(let i = 0; i < num; i++){
vecLocation[i] = createVector(random(100,700), random(100,700));
vecVelocity[i] = createVector(random(-10,10), random(-10,10));
ballColor[i] = [3]
for(let j = 0;j < 3;j++){
ballColor[i][j] = 255;
}
}
}
function draw(){
background(0);
noStroke();
for(let i = 0;i < num;i++){
hitNumber[i] = false;
}
for(let i = 0; i < num; i++){
for(let j = 0;j < num;j++){
if(i != j){
if(judge(vecLocation[i].x,vecLocation[i].y,vecLocation[j].x,vecLocation[j].y)== true){
hitNumber[i] = true;
hitNumber[j] = true;
for(let k = 0;k < 3;k++){
ballColor[i][k] = random(0,254);
ballColor[j][k] = random(0,254);
}
}
}
}
if(hitNumber[i]){
vecVelocity[i].x =vecVelocity[i].x * -1;
vecVelocity[i].y = vecVelocity[i].y * -1;
}
if(vecLocation[i].x < 0 || vecLocation[i].x > width){
vecVelocity[i].x = vecVelocity[i].x * -1;
}
if(vecLocation[i].y < 0 || vecLocation[i].y > height){
vecVelocity[i].y = vecVelocity[i].y * -1;
}
fill(ballColor[i][0],ballColor[i][1],ballColor[i][2]);
vecLocation[i].add(vecVelocity[i]);
ellipse(vecLocation[i].x,vecLocation[i].y,size,size);
}
}