xxxxxxxxxx
84
let myBall;
let balls;
let howManyBall;
function setup() {
howManyBalls = 400;
//createCanvas(600, 600);
createCanvas(windowWidth, windowHeight);
//myBall = new Ball(width/2,height/2,10);
balls = [];
for(let i=0; i <howManyBalls; i++)
{
balls.push(new Ball(width/2, height/2,random(1,2)));
}
background(250);
}
function draw() {
//background(200);
for(let i=0; i <howManyBalls; i++)
{
balls[i].update();
balls[i].display();
}
}
class Ball{
constructor(tx, ty,ts) {
this.x =tx;
this.y =ty;
this.speed = ts;
this.position = createVector(this.x,this.y);
this.velocity = createVector(random(-this.speed,this.speed),random(-this.speed,this.speed));
this.acceleration = createVector(.0,.001);
//this.acceleration = createVector(-s.01,-.01);
}
update()
{
//print(this.velocity.x);
this.position.add(this.velocity);
this.velocity.add(this.acceleration);
// if(this.position.x > width)
// {
// this.position.x =0;
// }
// if(this.position.x < 0)
// {
// this.position.x =width;
// }
// if(this.position.y > height)
// {
// this.position.y =0;
// }
// if(this.position.y < 0)
// {
// this.position.y =height;
// }
}
display()
{
fill(255,0,0);
ellipse(this.position.x, this.position.y, 1,1);
}
}