XNA 104 – Input from Keyboard.
A game is only a game if you get to do stuff in it, and for that we’re going to need some way to give our input to the game, and with that affect change within the world. We’re going to update the 103 project so that we can move the sprite around the screen. Start by opening the project from XNA103 It’ll give us the code base to start.
Add some global variables at the top of game1.cs to hold the keyboard state and sprite position :-
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D sprite;// sprite variable
KeyboardState oldKeyboardState,
currentKeyboardState;// Keyboard state variables
Vector2 positionSprite;// sprite position variable
.
.
.
This gives us two places to store the keyboard state, one for the current cycle and one for the last cycle, that way we can compare the two values and see if a key was pressed since the last update of if it has been held down since the last update. The variable that will hold the sprite position is a structure that just contains the X & Y properties, as opposed to the KeyboardState class that contains both properties and methods.
Next go to Initialize() and add these lines before the base.Initialize();, they set-up everything ready for us:-
protected override void Initialize()
{
currentKeyboardState = new KeyboardState();
positionSprite = new Vector2(100.0f);
base.Initialize();
}
Then change the call to spriteBatch.Draw in Draw()to use the position variable:-
protected override Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(sprite, positionSprite, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
Finally change the update() method and add these lines:-
protected override void Update(GameTime gameTime)
{
oldKeyboardState = currentKeyboardState;
currentKeyboardState = Keyboard.GetState();
.
.
.
The first line puts the currentKeyboardState from the last pass through into the oldKeyboardState. The second line gets the current keyboard state.
For my first example we’re going to be checking for the arrow keys, to do this we need to add code to the update method that will check for a key being down, we don’t care when it was pressed just is it being pressed now. Luckily there’s a method for what we want to do bool IsKeyDown(Keys key) we’ll check each of the arrow keys in turn and update another Vector2 and use that to update the sprites position.
First add a local Vector2 that we can set in relation to what keys we are pressing:-
Vector2 Delta = Vector2.Zero;
Next we’ll add four if(…) statements, that will check each key in turn and update Delta accordingly:-
if (currentKeyboardState.IsKeyDown(Keys.Left))
Delta.X = -1;
if (currentKeyboardState.IsKeyDown(Keys.Right))
Delta.X = 1;
if (currentKeyboardState.IsKeyDown(Keys.Up))
Delta.Y = -1;
if (currentKeyboardState.IsKeyDown(Keys.Down))
Delta.Y = 1;
Remember the screen origin(0,0) is in the top left corner so to move up we subtract -1 from the Y axis.
Now we take Delta and update the sprite position:-
positionSprite.X += Delta.X; positionSprite.Y += Delta.Y;
If you press F5 when the game window appears pressing the arrow keys will move the sprite by your command.
Another example find the lines that read:-
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
These are checking the GamePads back button to see if you want to Exit the game when you press back it will Start the Exit process for the game. We want to do that on you releasing the Esc key that is to say don’t Exit when you press the key only when you let go:-
// Allows the game to exit
if ((currentKeyboardState.IsKeyUp(Keys.Escape)) && (oldKeyboardState.IsKeyDown(Keys.Escape)))
this.Exit();
As you can see we’re checking that the oldKeyboardState shows the key down and the new currentKeyboardState shows that it is up, so it was down but now it’s up and then we’ll quit.
Conclusion
Well I can see it’s going to take alot of lines of code to check each of the keys on a full QWERTY keyboard, we’ll have to look into that at some point. Anyway now we really should find a way to use the mouse…


