Skip to content

blackbat13/MagneticParticlesPygameZero

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Magnetic Particles (Pygame Zero)

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.

Preview

Magnetic particles simulation preview

What You Can Learn

  • 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.

How It Works

Each particle has:

  • Position: x, y
  • Velocity: vx, vy
  • Accumulated force: forceX, forceY
  • Radius, mass, and charge

At every frame:

  1. Forces are computed for each particle pair.
  2. The force is converted to acceleration (F / m) and applied to velocity.
  3. Velocity updates position.
  4. Particles are clamped to the window bounds and bounce.

Project Structure

  • main.py: Full simulation logic and Pygame Zero entry point.
  • requirements.txt: Python dependency list.
  • magnetic_particles.gif: Visual preview used in this README.

Requirements

  • Python 3.9+
  • pgzero

Setup

  1. Create and activate a virtual environment (recommended).
  2. Install dependencies:
pip install -r requirements.txt

Run

python main.py

Tuning The Simulation

You can edit constants in main.py to experiment:

  • COUNT: number of particles
  • MINRADIUS, MAXRADIUS: particle size range
  • MAXFORCE: cap on force magnitude for stability
  • WIDTH, HEIGHT: simulation window size
  • BG: background color

Try changing one value at a time and observe the emergent behavior.

Notes

  • 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.

License

This project is licensed under the terms in the LICENSE file.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages