Difference between revisions of "Keyboard Input"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Timers)
 
Line 71: Line 71:
 
Your game will potentially register the key press many times, even if you are really quick. The best option to overcome this is to use a timer, pressing a key will start the timer for say 100 milliseconds and while the timer is running no other key presses are processed. When the time elapses we can then have key presses again.
 
Your game will potentially register the key press many times, even if you are really quick. The best option to overcome this is to use a timer, pressing a key will start the timer for say 100 milliseconds and while the timer is running no other key presses are processed. When the time elapses we can then have key presses again.
  
To do this see [Using Timers].
+
To do this see [[Using Timers]].

Latest revision as of 11:12, 31 January 2019

Adding Keyboard Control

With in the Game1.cs file, add the following variable declarations, 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;
Vector2 Position;

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;
Position = new Vector2(0,0);

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))
{
    Position.X -= playerMoveSpeed;
}

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

To handle the Up and Down movement you can adapt the code above to:

if (currentKeyboardState.IsKeyDown(Keys.Up))
{
    Position.Y -= playerMoveSpeed;
}

if (currentKeyboardState.IsKeyDown(Keys.Down))
{
    Position.Y += 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 keyboard so we can determine single key presses
previousKeyboardState = currentKeyboardState;

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

UpdatePlayer(currentKeyboardState);

Using Position

Obviously we are changing Position, so we need to draw a texture to the screen at Position to actually see the movement working.

Timers

Your game will potentially register the key press many times, even if you are really quick. The best option to overcome this is to use a timer, pressing a key will start the timer for say 100 milliseconds and while the timer is running no other key presses are processed. When the time elapses we can then have key presses again.

To do this see Using Timers.