I would like to apologize for not being able to post in a very long time. The schoolyear’s about to end here in Manila, and there is still a lot of work that needs to be done.
I needed a break, so I came up with this very simple demo of particle effects for Pygame, which I suspect might be useful to game programmers in general, particularly those who use SDL.
For those of you looking for a distraction, this demo was implemented as a “Space Invaders“-type gamelet, with PLENTY of room for improvement . As always, the graphics were taken from Ari Feldman’s very useful SpriteLib. Grab the source now!
To implement the particle effects, I implemented a Particle class that needs to be given the following information:
- initial position (x and y)
- initial velocity (x and y)
- acceleration (x and y)
- particle size
- “color structure”
Numbers 1 through 4 are self-explanatory. The particle is drawn as a square with the given side length, then it is placed on screen at the initial position. Then, at every frame, its position is changed by the amount prescribed by the velocity. At each frame also, the velocity is changed by the amount prescribed by the acceleration. By tweaking the velocity and acceleration, together with the use of random numbers, it’s possible to come up with interesting movements for the particles. If you want to make things more interesting, you can experiment with nonconstant acceleration by defining acceleration as a function of time.
But we all know that particle effects are primarily used for eye candy, and can produce spectacular results by playing with the colors. This is where the “color structure” comes in. Basically, the “color structure” is a list of tuples of the form:
(initial rgb, final rgb, duration)
The purpose of this structure is to set the initial color of the particle, then smoothly change the color to the final color over the number of frames set by the duration.
Since “color structure” is not just a single tuple, but a list of tuples, it is possible to chain together a sequence of color transitions in order to make more sophisticated effects. For instance, the explosions from the enemies are actually a combination of two color transitions: random color to white for 20 frames, then white to black for 10 frames.
The particles emitted by the shots from the player simply change color from a random shade of orange to black. You might find the particles emitted by the bombs interesting: the particles change from either orange or red, then go to black – this results in an interesting “fire-like” effect.
This is a VERY simple particle engine, only meant to show the basics. Feel free to experiment with the code, and if you come up with an interesting particle effect or an interesting modification to the code, I’d really appreciate it if you could share it with everyone .
Perhaps there are other topics which you would like covered in this site? Feel free to send me an e-mail or leave a comment . Thank you and I hope you find this useful!