This lesson was updated. Reset to the new version?
This browser isn't keeping your edits, so they're lost when you leave the page.
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 music to the game.
globals.jsloads the music track intosoundswithloop: true, andTitleScreenState.update()instates/TitleScreenState.jscallssounds.play(SoundName.Music)every frame. Sounds can’t start until the player presses a key, so the title screen keeps asking, and because the track loops, asking again leaves the one copy playing.
And with that, we have a fully functioning game of Breakout!
📚 References
Files
Preparing Environment