-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticle Simulation.py
More file actions
64 lines (48 loc) · 1.34 KB
/
Particle Simulation.py
File metadata and controls
64 lines (48 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import pygame
import random
import math
# Initialize Pygame
pygame.init()
# Set dimensions of the display
width, height = 900, 900
# Create the display surface
screen = pygame.display.set_mode((width, height))
# Define colors
white = (255, 255, 255)
# Particle class
class Particle:
def __init__(self, x, y):
self.x = x
self.y = y
self.vx = random.uniform(-1, 1)
self.vy = random.uniform(-1, 1)
self.radius = 5
self.color = white
def update(self):
self.x += self.vx
self.y += self.vy
# Bounce off the walls
if self.x <= 0 or self.x >= width:
self.vx *= -1
if self.y <= 0 or self.y >= height:
self.vy *= -1
def draw(self):
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
# Create particles
num_particles = 50
particles = [Particle(random.randint(0, width), random.randint(0, height)) for _ in range(num_particles)]
# Main loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Clear the screen
screen.fill((0, 0, 0))
# Update and draw particles
for particle in particles:
particle.update()
particle.draw()
pygame.display.flip()
pygame.time.delay(10)
pygame.quit()