03_user_control.py Diff

03_user_control.py
--- C:\Users\craven\Desktop\WebServer\arcade\arcade\examples\platform_tutorial\02_draw_sprites.py
+++ C:\Users\craven\Desktop\WebServer\arcade\arcade\examples\platform_tutorial\03_user_control.py
@@ -12,6 +12,9 @@
 CHARACTER_SCALING = 1
 TILE_SCALING = 0.5
 COIN_SCALING = 0.5
+
+# Movement speed of player, in pixels per frame
+PLAYER_MOVEMENT_SPEED = 5
 
 
 class MyGame(arcade.Window):
@@ -32,6 +35,9 @@
 
         # Separate variable that holds the player sprite
         self.player_sprite = None
+
+        # Our physics engine
+        self.physics_engine = None
 
         arcade.set_background_color(arcade.csscolor.CORNFLOWER_BLUE)
 
@@ -69,6 +75,9 @@
             wall.position = coordinate
             self.wall_list.append(wall)
 
+        # Create the 'physics engine'
+        self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list)
+
     def on_draw(self):
         """ Render the screen. """
 
@@ -80,6 +89,36 @@
         self.coin_list.draw()
         self.player_list.draw()
 
+    def on_key_press(self, key, modifiers):
+        """Called whenever a key is pressed. """
+
+        if key == arcade.key.UP or key == arcade.key.W:
+            self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED
+        elif key == arcade.key.DOWN or key == arcade.key.S:
+            self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED
+        elif key == arcade.key.LEFT or key == arcade.key.A:
+            self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
+        elif key == arcade.key.RIGHT or key == arcade.key.D:
+            self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
+
+    def on_key_release(self, key, modifiers):
+        """Called when the user releases a key. """
+
+        if key == arcade.key.UP or key == arcade.key.W:
+            self.player_sprite.change_y = 0
+        elif key == arcade.key.DOWN or key == arcade.key.S:
+            self.player_sprite.change_y = 0
+        elif key == arcade.key.LEFT or key == arcade.key.A:
+            self.player_sprite.change_x = 0
+        elif key == arcade.key.RIGHT or key == arcade.key.D:
+            self.player_sprite.change_x = 0
+
+    def on_update(self, delta_time):
+        """ Movement and game logic """
+
+        # Move the player with the physics engine
+        self.physics_engine.update()
+
 
 def main():
     """ Main method """