gpu_particle_burst_07.py Diff#

gpu_particle_burst_07.py#
--- /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/stable/doc/tutorials/gpu_particle_burst/gpu_particle_burst_06.py
+++ /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/stable/doc/tutorials/gpu_particle_burst/gpu_particle_burst_07.py
@@ -14,6 +14,9 @@
 SCREEN_TITLE = "GPU Particle Explosion"
 
 PARTICLE_COUNT = 300
+
+MIN_FADE_TIME = 0.25
+MAX_FADE_TIME = 1.5
 
 
 @dataclass
@@ -32,11 +35,11 @@
 
         # Program to visualize the points
         self.program = self.ctx.load_program(
-            vertex_shader="vertex_shader_v3.glsl",
+            vertex_shader="vertex_shader_v4.glsl",
             fragment_shader="fragment_shader.glsl",
         )
 
-        self.ctx.enable_only()
+        self.ctx.enable_only(self.ctx.BLEND)
 
     def on_draw(self):
         """ Draw everything """
@@ -55,8 +58,16 @@
             burst.vao.render(self.program, mode=self.ctx.POINTS)
 
     def on_update(self, dt):
-        """ Update everything """
-        pass
+        """ Update game """
+
+        # Create a copy of our list, as we can't modify a list while iterating
+        # it. Then see if any of the items have completely faded out and need
+        # to be removed.
+        temp_list = self.burst_list.copy()
+        for burst in temp_list:
+            if time.time() - burst.start_time > MAX_FADE_TIME:
+               self.burst_list.remove(burst)
+
 
     def on_mouse_press(self, x: float, y: float, button: int, modifiers: int):
         """ User clicks mouse """
@@ -71,6 +82,8 @@
                 red = random.uniform(0.5, 1.0)
                 green = random.uniform(0, red)
                 blue = 0
+                fade_rate = random.uniform(1 / MAX_FADE_TIME, 1 / MIN_FADE_TIME)
+
                 yield initial_x
                 yield initial_y
                 yield dx
@@ -78,6 +91,7 @@
                 yield red
                 yield green
                 yield blue
+                yield fade_rate
 
         # Recalculate the coordinates from pixels to the OpenGL system with
         # 0, 0 at the center.
@@ -92,8 +106,11 @@
 
         # Create a buffer description that says how the buffer data is formatted.
         buffer_description = arcade.gl.BufferDescription(buffer,
-                                                         '2f 2f 3f',
-                                                         ['in_pos', 'in_vel', 'in_color'])
+                                                         '2f 2f 3f f',
+                                                         ['in_pos',
+                                                          'in_vel',
+                                                          'in_color',
+                                                          'in_fade_rate'])
         # Create our Vertex Attribute Object
         vao = self.ctx.geometry([buffer_description])