I am a big fan of Liam Fraser’s tutorials on YouTube. I wanted to change PiShooter so that there was an explosion when you hit the Pi. I made a few changes to Liam’s code. Becuase I am only just getting to grips with pygame, I didn’t make a class for my explosion – I just found a “quick and dirty” was to achieve my goal.
First, I loaded up an explosion image into “boom”.
Then I added a bit to the collision detection part.
I stored the location of the shot in x1 and y1. Then, if there was a hit, I killed the Pi sprite and made a new one (so the one we hit disappears and a new one begins again at x=0. I’ve made a variable “count” and then in the main loop I blit the explosion to the screen for 20 frames. On my computer this is enough to make the explosion visible (but my laptop is pretty slow!)