Difference between revisions of "Adding and Processing Player Input"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
Line 28: Line 28:
  
 
<syntaxhighlight lang=csharp>
 
<syntaxhighlight lang=csharp>
if (currentKeyboardState.IsKeyDown(Keys.Left)
+
if (currentKeyboardState.IsKeyDown(Keys.Left))
 
{
 
{
 
     player.Position.X -= playerMoveSpeed;
 
     player.Position.X -= playerMoveSpeed;
 
}
 
}
  
if (currentKeyboardState.IsKeyDown(Keys.Right)
+
if (currentKeyboardState.IsKeyDown(Keys.Right))
 
{
 
{
 
     player.Position.X += playerMoveSpeed;
 
     player.Position.X += playerMoveSpeed;
 
}
 
}
 +
</syntaxhighlight>
 +
 +
Now, back in the Update method add the following lines to listen to the keyboard. Enter them just before we call UpdatePlayer:
 +
 +
<syntaxhighlight lang=csharp>
 +
// Save the previous state of the keyboardso we can determine single key presses
 +
previousKeyboardState = currentKeyboardState;
 +
 +
// Read the current state of the keyboard and store it
 +
currentKeyboardState = Keyboard.GetState();
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 17:25, 18 March 2017

You should have a game with a player created, when you run it you should see a blue background and the player sprite. This will look at settings the controls for the player. It will look at keyboard cotnrol, but you could alternatively use gamepad, touch, or mouse controls. You could even configure all of these modes at the same time, allowing the user to select the control method.

Adding Keyboard Control

With in the Game1.cs file, find the the Game1 class and find the line Player player; . After this line enter the following, this will be used to save the current and previous keyboard state and to also control the speed of movement:

// Keyboard states used to determine key presses
KeyboardState currentKeyboardState;
KeyboardState previousKeyboardState;

// A movement speed for the player
float playerMoveSpeed;

Now find the Initialize method within the Game1.cs file. You need to add the line below to set the movement speed:

// Set a constant player move speed
playerMoveSpeed = 8.0f;

The Update method within the Game1.cs file is used while the game is running. In order to make this easy to understand and read, we will create a new method to update the player and call it from the Update method. Create the UpdatePlayer method and call it from Update, see below:

Update player.png

Reading the Key and Move

Now in the UpdatePlayer method add the code to detect left and right movement:

if (currentKeyboardState.IsKeyDown(Keys.Left))
{
    player.Position.X -= playerMoveSpeed;
}

if (currentKeyboardState.IsKeyDown(Keys.Right))
{
    player.Position.X += playerMoveSpeed;
}

Now, back in the Update method add the following lines to listen to the keyboard. Enter them just before we call UpdatePlayer:

// Save the previous state of the keyboardso we can determine single key presses
previousKeyboardState = currentKeyboardState;

// Read the current state of the keyboard and store it
currentKeyboardState = Keyboard.GetState();