Project 6.2: Fireworks
Click to make fireworks
When the user clicks on the screen it creates a burst of fireworks
This program must use an ArrayList to track a collection of Particle objects that are added and removed from the list dynamically.
Each particle must be assigned a random color at creation
Each particle moves in a random direction but is affected by gravity
STEP BY STEP
1) Create a simple Particle class
Particles a x position, y position, and size.
Particle objects have a constructor that sets x and y equal to mouseX and mouseY, and size to 5.
Particle objects have a render() method that draws an ellipse based on x, y, and size
2) Create an array list of particles in your main program
Declare your ArrayList<Particle> as a global variable named particles.
In setup(), initialize particles.
In draw(), loop through particles.
Update each particle
In draw(), through particles a second time.
Render each particle
Explanation: Why don't we just call update() and render() inside the same loop? This is because (1) It's good style to separate our logic and display parts of our program and (2) Later on we'll remove particles using update(), and doing so will cause render() to crash the program when it tries to draw a particle that isn't there anymore.
Write the mousePressed() method. Remember this is a special method in Processing which is called each time the user completes a full click.
Add one particle to the ArrayList
3) Improving Particles
Add xSpeed and ySpeed variables to Particle
In the constructor, assign them random starting values.
In the update() method, use them to change x and y position
Add red, green, and blue variables to Particle
In the constructor, assign them random starting value
In the update() method, use them to set the fill color
Each frame, increase the particle's ySpeed by a set value to represent gravity
In your mousePressed() method, use a loop to add 30 particles instead of just one.
4) Counting Particles
Add a text output to your program that displays the number of particles in your ArrayList
Notice that when you run the program, this number never goes down. We'll need to add in code to remove extra particles from the list once they leave the screen!
5) Removing Particles
In the particle's update method, it can remove itself from the ArrayList when it goes off the bottom of the screen.
To do so we'll use a special keyword: this. The keyword this simply means "me." You an use the line of code below to say, "When I am below the screen, remove me from the list."
EXAMPLE: RUNNING PROGRAM
Click here to download a running version of the program. <COMING SOON>