pymunk_demo_platformer_10.py Diff

pymunk_demo_platformer_10.py
--- /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/development/doc/tutorials/pymunk_platformer/pymunk_demo_platformer_09.py
+++ /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/development/doc/tutorials/pymunk_platformer/pymunk_demo_platformer_10.py
@@ -158,6 +158,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 """
 
@@ -230,6 +239,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
@@ -291,7 +313,7 @@
     def on_mouse_press(self, x, y, button, modifiers):
         """ Called whenever the mouse button is clicked. """
 
-        bullet = arcade.SpriteSolidColor(width=20, height=5, color=arcade.color.DARK_YELLOW)
+        bullet = BulletSprite(width=20, height=5, color=arcade.color.DARK_YELLOW)
         self.bullet_list.append(bullet)
 
         # Position the bullet at the player's current location