-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticles.py
More file actions
58 lines (49 loc) · 1.66 KB
/
Copy pathParticles.py
File metadata and controls
58 lines (49 loc) · 1.66 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
import numpy as np
import pygame
from pygame.locals import *
import random
from time import sleep
pygame.init()
w, h = 500, 500
screensize = np.int32([w, h])
screen = pygame.display.set_mode(screensize)
particleno = 100
particles = []
particlepos = np.zeros((particleno, 2), dtype='float32')
particleradius = np.zeros(particleno, dtype='int32')
class Particle:
def __init__(self, pos, radius=-1):
self.pos = np.float32(pos)
if radius <= 0:
self.r = int(random.random() * 10)
else:
self.r = int(radius)
self.id = len(particles)
particlepos[self.id, :] = self.pos[:]
particleradius[self.id] = self.r
particles.append(self)
def collide(self):
pygame.draw.circle(screen, 255, self.pos, self.r)
dists = np.linalg.norm(particlepos - self.pos, axis=1) - (particleradius + self.r)
collisions = dists < 0
if np.sum(collisions) > 1:
print(np.sum(collisions))
diffs = particlepos[collisions] - self.pos
angs = np.arctan2(diffs[:, 1], diffs[:, 0])
units = np.float32([np.cos(angs), np.sin(angs)]).reshape(-1, 2)
print(diffs.shape, angs.shape, units.shape, dists[collisions].shape)
self.pos -= np.sum(dists[collisions] * units[:], axis=1)
for i in range(particleno):
Particle((random.randrange(0, w), random.randrange(0, h)), 10)
while True:
screen.fill(0)
random.shuffle(particles)
for P in particles:
P.collide()
pygame.display.flip()
for e in pygame.event.get():
if e.type == QUIT:
quit()
if e.type == KEYDOWN:
quit()
sleep(0.5)