Step 3 - Add User Control

Now we need to be able to get the user to move around.

First, at the top of the program add a constant that controls how many pixels per update our character travels:

03_user_control.py - Player Move Speed Constant
# Movement speed of player, in pixels per frame
PLAYER_MOVEMENT_SPEED = 5

Next, at the end of our setup method, we are need to create a physics engine that will move our player and keep her from running through walls. The PhysicsEngineSimple class takes two parameters: The moving sprite, and a list of sprites the moving sprite can’t move through.

For more information about the physics engine we are using here, see PhysicsEngineSimple class

Note

It is possible to have multiple physics engines, one per moving sprite. These are very simple, but easy physics engines. See Pymunk Platformer for a more advanced physics engine.

03_user_control.py - Create Physics Engine
# Movement speed of player, in pixels per frame
PLAYER_MOVEMENT_SPEED = 5

Each sprite has center_x and center_y attributes. Changing these will change the location of the sprite. (There are also attributes for top, bottom, left, right, and angle that will move the sprite.)

Each sprite has change_x and change_y variables. These can be used to hold the velocity that the sprite is moving with. We will adjust these based on what key the user hits. If the user hits the right arrow key we want a positive value for change_x. If the value is 5, it will move 5 pixels per frame.

In this case, when the user presses a key we’ll change the sprites change x and y. The physics engine will look at that, and move the player unless she’ll hit a wall.

03_user_control.py - Handle key-down
 1    def on_key_press(self, key, modifiers):
 2        """Called whenever a key is pressed. """
 3
 4        if key == arcade.key.UP or key == arcade.key.W:
 5            self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED
 6        elif key == arcade.key.DOWN or key == arcade.key.S:
 7            self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED
 8        elif key == arcade.key.LEFT or key == arcade.key.A:
 9            self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
10        elif key == arcade.key.RIGHT or key == arcade.key.D:
11            self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED

On releasing the key, we’ll put our speed back to zero.

03_user_control.py - Handle key-up
 1    def on_key_release(self, key, modifiers):
 2        """Called when the user releases a key. """
 3
 4        if key == arcade.key.UP or key == arcade.key.W:
 5            self.player_sprite.change_y = 0
 6        elif key == arcade.key.DOWN or key == arcade.key.S:
 7            self.player_sprite.change_y = 0
 8        elif key == arcade.key.LEFT or key == arcade.key.A:
 9            self.player_sprite.change_x = 0
10        elif key == arcade.key.RIGHT or key == arcade.key.D:
11            self.player_sprite.change_x = 0

Note

This method of tracking the speed to the key the player presses is simple, but isn’t perfect. If the player hits both left and right keys at the same time, then lets off the left one, we expect the player to move right. This method won’t support that. If you want a slightly more complex method that does, see Better Move By Keyboard.

Our on_update method is called about 60 times per second. We’ll ask the physics engine to move our player based on her change_x and change_y.

03_user_control.py - Update the sprites
1    def on_update(self, delta_time):
2        """ Movement and game logic """
3
4        # Move the player with the physics engine
5        self.physics_engine.update()