xxxxxxxxxx
65
let rockets = [];
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
for (let i = rockets.length - 1; i >= 0; i--) {
rocket = rockets[i];
rocket.moveUp();
rocket.show();
rocket.explode();
if (rocket.reachedTarget()) {
rockets.splice(i, 1); // Remove the rocket from the array when it reaches the final Y.
}
}
}
function mouseClicked() {
newRocket = new Rocket(mouseX, mouseY);
rockets.push(newRocket);
}
class Rocket {
constructor(x, finalY) {
this.x = x;
this.y = height;
this.speed = 3;
this.finalY = finalY;
}
moveUp() {
this.y -= this.speed;
if (this.y <= this.finalY) {
this.y = this.finalY;
}
}
explode() {
this.diameter = random(30, 40) // for the arc
if (this.y === this.finalY) {
for (let i; i <= 12; i++) {
line(this.x, this.finalY, arc(this.x, this.finalY, this.diameter, this.diameter, 0, PI * i));
}
}
}
show() {
fill("yellow");
noStroke();
rect(this.x - 2, this.y - 7, 2, 7);
}
reachedTarget() {
return this.y === this.finalY;
}
}