Breakout-8 implements a rudimentary particle system to create a nicer visual effect when the ball collides with a brick. For a fantastic introduction to particles, please watch this video from The Coding Train. As the final update, we’re also adding music.
Important Code
- In
Particle.jswe define a class that represents one tiny particle in the game. Each particle has its own position, velocity, and acceleration. We can manipulate these values to achieve a little “explosion” effect in the game when a brick is hit. - In the
constructor()ofBrick.js, we define a new array ofparticlesthat will be populated inhit()and acoloursarray of RGB values corresponding to the blue, green, red, purple, and gold colours of our bricks from the sprite sheet.- In
hit(), we then usecoloursto set the colour when instantiating new particle objects. - In
update()andrender(), we make sure every particle gets updated and drawn to the screen according to each particle’s individuallifevalue. - Make sure to now call
brick.update(dt)inPlayState.js.
- In
- Finally, we add some music to the game in
globals.jsby adding the music track to oursoundsobject (notice thetruefor looping) and callingsounds.music.play()inmain.js.
And with that, we have a fully functioning game of Breakout!
📚 References
Files
Preparing Environment