var Inanimates = []
function setup(){
createCanvas(600, 400);
// 帧率
frameRate(20)
}
function draw(){
background(0)
Inanimates.push(new test())
for(let j=Inanimates.length-1; j>0; j--){
Inanimates[j].run();
// 控制内存
if(Inanimates[j].isRemove()){
Inanimates.splice(j, 1);
}
}
}
class test {
constructor(){
this.x = random(-40, 100);
this.y = random(height);
this.xSpeet = 3;
this.opacty = 255;
this.w = random(2, 5)
}
run(){
this.update();
this.move()
this.show()
}
// 创建无生命物体
show(){
noStroke()
fill(255, 255, 255, this.opacty)
ellipse(this.x, this.y, this.w)
}
// 赋予力和速度
move(){
this.x += this.xSpeet
}
isRemove(){
return this.x > width + 10;
}
// 赋予行为
update(){
var r = random(1)
if(r < 0.01){
this.opacty -= 100;
}else{
this.opacty = 255;
}
}
}
console