pymunk_demo_platformer_10.py Diff

pymunk_demo_platformer_10.py
--- /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/2.6.3/doc/tutorials/pymunk_platformer/pymunk_demo_platformer_09.py
+++ /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/2.6.3/doc/tutorials/pymunk_platformer/pymunk_demo_platformer_10.py
@@ -160,6 +160,15 @@
             self.texture = self.walk_textures[self.cur_texture][self.character_face_direction]
 
 
+class BulletSprite(arcade.SpriteSolidColor):
+    """ Bullet Sprite """
+    def pymunk_moved(self, physics_engine, dx, dy, d_angle):
+        """ Handle when the sprite is moved by the physics engine. """
+        # If the bullet falls below the screen, remove it
+        if self.center_y < -100:
+            self.remove_from_sprite_lists()
+
+
 class GameWindow(arcade.Window):
     """ Main Window """
 
@@ -232,6 +241,19 @@
         # Create the physics engine
         self.physics_engine = arcade.PymunkPhysicsEngine(damping=damping,
                                                          gravity=gravity)
+
+        def wall_hit_handler(bullet_sprite, _wall_sprite, _arbiter, _space, _data):
+            """ Called for bullet/wall collision """
+            bullet_sprite.remove_from_sprite_lists()
+
+        self.physics_engine.add_collision_handler("bullet", "wall", post_handler=wall_hit_handler)
+
+        def item_hit_handler(bullet_sprite, item_sprite, _arbiter, _space, _data):
+            """ Called for bullet/wall collision """
+            bullet_sprite.remove_from_sprite_lists()
+            item_sprite.remove_from_sprite_lists()
+
+        self.physics_engine.add_collision_handler("bullet", "item", post_handler=item_hit_handler)
 
         # Add the player.
         # For the player, we set the damping to a lower value, which increases
@@ -293,7 +315,7 @@
     def on_mouse_press(self, x, y, button, modifiers):
         """ Called whenever the mouse button is clicked. """
 
-        bullet = arcade.SpriteSolidColor(20, 5, arcade.color.DARK_YELLOW)
+        bullet = BulletSprite(20, 5, arcade.color.DARK_YELLOW)
         self.bullet_list.append(bullet)
 
         # Position the bullet at the player's current location