xxxxxxxxxx
53
//Create a fireworks program. When the user clicks, display an explosion at that position. Hint: your explosion can be 100 red circles that fly off in random directions.
//
let fireworks
let newFirework
function setup() {
createCanvas(400, 400);
background(0);
noStroke();
colorMode(HSB)
}
function draw() {
if (fireworks) {
background(0);
for (let f = 0; f < fireworks.length; f++) {
fireworks[f].display()
fireworks[f].spread++
}
}
}
function mouseClicked() {
thisColour = [random(255), 255, 255];
newFirework = new Firework(mouseX, mouseY, 10, thisColour)
append(fireworks, newFirework)
}
class Firework {
constructor(clickX, clickY, spread, colour) {
this.clickX = clickX;
this.clickY = clickY;
this.spread = spread;
this.colour = colour;
}
display() {
fill(this.colour)
for (let i = 0; i < 100; i++) {
circle(
this.clickX + random(
-this.spread,
this.spread
),
this.clickY + random(
-this.spread,
this.spread
),
2
);
}
}
}