var balls = []
function setup(){
createCanvas(400, 200)
}
function draw(){
background(0)
var r1 = random(10)
var r2 = random(9, 10)
if(r1 > r2){
balls.push( new ball() )
}
for(let i=balls.length-1; i>=0; i--){
balls[i].run();
if(balls[i].isRemove()){
balls.splice(i, 1)
}
}
}
class ball {
constructor(){
// 位置向量
this.location = createVector(random(190, 205), 150);
// x移动时速度向量
this.velocity = createVector(random(0.4, 0.6))
// x-- or x++
this.ab = floor(random(1, 3));
// 随机的力
this.f = random(3, 5);
// color
this.c = createVector(random(255), random(255), random(255))
}
run(){
this.update(this.location, this.velocity)
this.show(this.location, this.c)
}
show(l, c){
stroke(c.x, c.y, c.z)
noFill()
ellipse(l.x, l.y, 10, 10)
}
update(l,v){
if( this.isDown() ){
this.leftORtight(l, v);
l.y -= this.f;
}else{
this.leftORtight(l, v);
l.y += -this.f
}
this.f -= 0.16;
}
leftORtight(l, v){
if(this.ab < 2){
l.x += v.x
}else{
l.x -= v.x
}
}
// 什么时候落下,没有力自然就落下了
isDown(){
return this.f >= 0;
}
// 什么时候清空内存
isRemove(){
return this.location.y > height
}
}
console