pymunk_demo_platformer_07.py Diff

pymunk_demo_platformer_07.py
--- /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/2.6.12/doc/tutorials/pymunk_platformer/pymunk_demo_platformer_06.py
+++ /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/2.6.12/doc/tutorials/pymunk_platformer/pymunk_demo_platformer_07.py
@@ -48,6 +48,13 @@
 
 # Force applied while on the ground
 PLAYER_MOVE_FORCE_ON_GROUND = 8000
+
+# Force applied when moving left/right in the air
+PLAYER_MOVE_FORCE_IN_AIR = 900
+
+# Strength of a jump
+PLAYER_JUMP_IMPULSE = 1800
+
 
 class GameWindow(arcade.Window):
     """ Main Window """
@@ -164,6 +171,12 @@
             self.left_pressed = True
         elif key == arcade.key.RIGHT:
             self.right_pressed = True
+        elif key == arcade.key.UP:
+            # find out if player is standing on ground
+            if self.physics_engine.is_on_ground(self.player_sprite):
+                # She is! Go ahead and jump
+                impulse = (0, PLAYER_JUMP_IMPULSE)
+                self.physics_engine.apply_impulse(self.player_sprite, impulse)
 
     def on_key_release(self, key, modifiers):
         """Called when the user releases a key. """
@@ -176,16 +189,23 @@
     def on_update(self, delta_time):
         """ Movement and game logic """
 
+        is_on_ground = self.physics_engine.is_on_ground(self.player_sprite)
         # Update player forces based on keys pressed
         if self.left_pressed and not self.right_pressed:
             # Create a force to the left. Apply it.
-            force = (-PLAYER_MOVE_FORCE_ON_GROUND, 0)
+            if is_on_ground:
+                force = (-PLAYER_MOVE_FORCE_ON_GROUND, 0)
+            else:
+                force = (-PLAYER_MOVE_FORCE_IN_AIR, 0)
             self.physics_engine.apply_force(self.player_sprite, force)
             # Set friction to zero for the player while moving
             self.physics_engine.set_friction(self.player_sprite, 0)
         elif self.right_pressed and not self.left_pressed:
             # Create a force to the right. Apply it.
-            force = (PLAYER_MOVE_FORCE_ON_GROUND, 0)
+            if is_on_ground:
+                force = (PLAYER_MOVE_FORCE_ON_GROUND, 0)
+            else:
+                force = (PLAYER_MOVE_FORCE_IN_AIR, 0)
             self.physics_engine.apply_force(self.player_sprite, force)
             # Set friction to zero for the player while moving
             self.physics_engine.set_friction(self.player_sprite, 0)