A small educational physics simulation built with Pygame Zero.
Particles interact using a simple inverse-square force model, move across the screen, and bounce off the window boundaries.
- How to structure a basic real-time simulation loop (
draw+update) in Pygame Zero. - How pairwise forces are computed between particles.
- How velocity and position are updated each frame.
- How boundary collisions can be handled with simple reflection.
- How simulation constants affect visual behavior.
Each particle has:
- Position:
x,y - Velocity:
vx,vy - Accumulated force:
forceX,forceY - Radius, mass, and charge
At every frame:
- Forces are computed for each particle pair.
- The force is converted to acceleration (
F / m) and applied to velocity. - Velocity updates position.
- Particles are clamped to the window bounds and bounce.
main.py: Full simulation logic and Pygame Zero entry point.requirements.txt: Python dependency list.magnetic_particles.gif: Visual preview used in this README.
- Python 3.9+
pgzero
- Create and activate a virtual environment (recommended).
- Install dependencies:
pip install -r requirements.txtpython main.pyYou can edit constants in main.py to experiment:
COUNT: number of particlesMINRADIUS,MAXRADIUS: particle size rangeMAXFORCE: cap on force magnitude for stabilityWIDTH,HEIGHT: simulation window sizeBG: background color
Try changing one value at a time and observe the emergent behavior.
- This is an educational model, not a physically accurate magnetic simulation.
- All particles are assigned positive charge in the current implementation.
- A force cap is used to prevent unstable motion when particles are very close.
This project is licensed under the terms in the LICENSE file.
