pymunk_demo_platformer_06.py Diff

pymunk_demo_platformer_06.py
--- C:\Users\craven\Desktop\WebServer\arcade\doc\tutorials\pymunk_platformer\pymunk_demo_platformer_05.py
+++ C:\Users\craven\Desktop\WebServer\arcade\doc\tutorials\pymunk_platformer\pymunk_demo_platformer_06.py
@@ -45,6 +45,9 @@
 # Keep player from going too fast
 PLAYER_MAX_HORIZONTAL_SPEED = 450
 PLAYER_MAX_VERTICAL_SPEED = 1600
+
+# Force applied while on the ground
+PLAYER_MOVE_FORCE_ON_GROUND = 8000
 
 class GameWindow(arcade.Window):
     """ Main Window """
@@ -154,14 +157,41 @@
 
     def on_key_press(self, key, modifiers):
         """Called whenever a key is pressed. """
-        pass
+
+        if key == arcade.key.LEFT:
+            self.left_pressed = True
+        elif key == arcade.key.RIGHT:
+            self.right_pressed = True
 
     def on_key_release(self, key, modifiers):
         """Called when the user releases a key. """
-        pass
+
+        if key == arcade.key.LEFT:
+            self.left_pressed = False
+        elif key == arcade.key.RIGHT:
+            self.right_pressed = False
 
     def on_update(self, delta_time):
         """ Movement and game logic """
+
+        # 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)
+            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)
+            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)
+        else:
+            # Player's feet are not moving. Therefore up the friction so we stop.
+            self.physics_engine.set_friction(self.player_sprite, 1.0)
+
+        # Move items in the physics engine
         self.physics_engine.step()
 
     def on_draw(self):