Below is an example of a Python program using Pygame that creates an interactive 2D particle system simulation. In this simulation, each particle is affected by a constant gravitational acceleration and bounces off the window edges. You can add new particles by clicking on the screen.
────────────────────────────────────────────────────────────────────
pythonCopyimport pygame
import sys
import random
Initialize Pygame
pygame.init()
Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("2D Particle System Simulation")
clock = pygame.time.Clock()
FPS = 60
Gravitational acceleration (pixels/frame^2)
GRAVITY = 0.5
class Particle:
def init(self, x, y, radius=5, color=(255, 255, 255)):
# Position as a 2D vector
self.pos = pygame.math.Vector2(x, y)
# Give each particle a random initial velocity
self.vel = pygame.math.Vector2(random.uniform(-2, 2), random.uniform(-5, 0))
# Acceleration; here, gravity acts downward
self.acc = pygame.math.Vector2(0, GRAVITY)
self.radius = radius
self.color = color
def update(self):
# Update velocity and position
self.vel += self.acc
self.pos += self.vel
# Bounce off the bottom of the screen
if self.pos.y + self.radius > HEIGHT:
self.pos.y = HEIGHT - self.radius
self.vel.y *= -0.7 # reverse velocity with damping
# Bounce off the top of the screen
if self.pos.y - self.radius < 0:
self.pos.y = self.radius
self.vel.y *= -0.7
# Bounce off the left wall
if self.pos.x - self.radius < 0:
self.pos.x = self.radius
self.vel.x *= -0.7
# Bounce off the right wall
if self.pos.x + self.radius > WIDTH:
self.pos.x = WIDTH - self.radius
self.vel.x *= -0.7
def draw(self, surface):
pygame.draw.circle(surface, self.color, (int(self.pos.x), <REDACTED>)), self.radius)
List to hold all particles in the simulation
particles = []
Main loop
running = True
while running:
clock.tick(FPS)
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# On mouse click, add new particles at the click position.
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = event.pos
# Create a few particles for a nice effect
for _ in range(5):
particles.append(Particle(mouse_x, mouse_y))
# Update each particle
for particle in particles:
particle.update()
# Clear the screen (fill with black)
screen.fill((0, 0, 0))
# Draw all particles
for particle in particles:
particle.draw(screen)
# Update the display
pygame.display.flip()
Clean up
pygame.quit()
()
How It Works
Initialization:Pygame is initialized and a window of size 800×600 is created.A constant gravitational acceleration (GRAVITY) is defined.
Pygame is initialized and a window of size 800×600 is created.
A constant gravitational acceleration (GRAVITY) is defined.
Particle Class:Each particle has a position, velocity (with a random initial value), and constant acceleration (gravity).Theupdate()method uses simple Euler integration to update the particle’s velocity and position.Boundary checks make particles bounce off the edges with a damping effect (reducing their speed).
Each particle has a position, velocity (with a random initial value), and constant acceleration (gravity).
Theupdate()method uses simple Euler integration to update the particle’s velocity and position.
Boundary checks make particles bounce off the edges with a damping effect (reducing their speed).
Interactivity:When the user clicks anywhere on the screen (), several new particles are added at that location.
When the user clicks anywhere on the screen (), several new particles are added at that location.
Main Loop:The main loop updates all particles, redraws them, and refreshes the display at 60 FPS.
The main loop updates all particles, redraws them, and refreshes the display at 60 FPS.
Initialization:
Pygame is initialized and a window of size 800×600 is created.
A constant gravitational acceleration (GRAVITY) is defined.
Pygame is initialized and a window of size 800×600 is created.
A constant gravitational acceleration (GRAVITY) is defined.
Particle Class:
Each particle has a position, velocity (with a random initial value), and constant acceleration (gravity).
Theupdate()method uses simple Euler integration to update the particle’s velocity and position.
Boundary checks make particles bounce off the edges with a damping effect (reducing their speed).
Each particle has a position, velocity (with a random initial value), and constant acceleration (gravity).
Theupdate()method uses simple Euler integration to update the particle’s velocity and position.
Boundary checks make particles bounce off the edges with a damping effect (reducing their speed).
Interactivity:
When the user clicks anywhere on the screen (), several new particles are added at that location.
When the user clicks anywhere on the screen (), several new particles are added at that location.
Main Loop:
The main loop updates all particles, redraws them, and refreshes the display at 60 FPS.
The main loop updates all particles, redraws them, and refreshes the display at 60 FPS.
You can run this script (assuming you have Pygame installed) to see the particles falling under gravity and bouncing around, and click to add more particles dynamically. Enjoy experimenting with the simulation!