-
Notifications
You must be signed in to change notification settings - Fork 12
/
particle_simulation.py
89 lines (65 loc) · 1.86 KB
/
particle_simulation.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
import sfml as sf
from random import randint
WIDTH = 800
HEIGHT = 600
TITLE = "Python SFML Particle Simulation"
FPS = 60
LIFETIME = 100
class ParticleInfo:
def __init__(self, velocity, lifetime):
self.velocity = velocity
self.lifetime = lifetime
class ParticleSystem:
def __init__(self):
self.v_array = None
self.particles = None
def load(self, size):
self.v_array = sf.VertexArray(
sf.PrimitiveType.POINTS,
size
)
p_list = []
for i in xrange(size):
x = randint(-100, 100)
y = randint(-100, 100)
lifetime = randint(1, LIFETIME)
p_info = ParticleInfo(sf.Vector2(x, y), lifetime)
p_list.append(p_info)
self.particles = dict(
zip(self.v_array, p_list)
)
window = sf.RenderWindow(sf.VideoMode(WIDTH, HEIGHT),
TITLE)
window.framerate_limit = 60
p_system = ParticleSystem()
p_system.load(1000)
for i in p_system.particles.keys():
i.position = sf.Mouse.get_position(window)
i.color = sf.Color(
randint(0, 255),
randint(0, 255),
randint(0, 255),
randint(0, 255)
)
clock = sf.Clock()
while window.is_open:
# Eventler
for event in window.events:
if type(event) is sf.CloseEvent:
window.close()
elap = clock.restart().seconds
window.title = "FPS: %.2f - Particles: %d" % (
(1.0 / elap), len(p_system.particles.keys())
)
# Update
for i, j in p_system.particles.iteritems():
i.position += j.velocity * elap
j.lifetime -= 1
if j.lifetime <= 0:
j.lifetime = randint(1, LIFETIME)
i.position = sf.Mouse.get_position(window)
# Render
window.clear()
window.draw(p_system.v_array)
window.display()