-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnow_engine.py
More file actions
66 lines (56 loc) · 1.96 KB
/
Copy pathsnow_engine.py
File metadata and controls
66 lines (56 loc) · 1.96 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
65
66
from random import random
from math import pi, cos, sin
from OpenGL import GL
class SnowParticle:
def __init__(self, xyz, rgb, size, speed):
self.xyz = xyz
self.rgb = rgb
self.size = size
self.speed = speed
class SnowEngine:
def __init__(self):
self.particles = []
self.size = [100, 40]
self.height = 25
self.height_variation = 5
self.rate = 4
self.min_particle_speed = 0.05
self.max_particle_speed = 0.2
self.gl_list = GL.glGenLists(1)
GL.glNewList(self.gl_list, GL.GL_COMPILE)
GL.glBegin(GL.GL_TRIANGLE_FAN)
for i in range(20):
theta = i * 2.0 * pi / 20.0
x = cos(theta)
y = sin(theta)
GL.glVertex3f(x, y, 0.0)
GL.glEnd()
GL.glEndList()
def enable(self):
self.enable = True
def disable(self):
self.enable = False
def generate_particles(self):
for i in range(self.rate):
xyz = [random(), self.height + random() * self.height_variation, random()]
xyz[0] = xyz[0] * self.size[1] - self.size[1] / 2.0
xyz[2] = xyz[2] * self.size[0] - self.size[0] / 2.0
rgb = [random(), random(), random()]
size = random() * 0.6
speed = min(self.max_particle_speed, max(self.min_particle_speed, random()))
p = SnowParticle(xyz, rgb, size, speed)
self.particles.append(p)
def draw_snow(self):
for p in self.particles:
GL.glPushMatrix()
GL.glTranslatef(p.xyz[0], p.xyz[1], p.xyz[2])
GL.glRotatef(90, 0, 1, 0)
GL.glScalef(p.size, p.size, 1.0)
GL.glColor4f(p.rgb[0], p.rgb[1], p.rgb[2], p.size)
GL.glCallList(self.gl_list)
GL.glPopMatrix()
def update(self):
for p in self.particles:
p.xyz[1] -= p.speed
if p.xyz[1] < 0.0:
self.particles.remove(p)