Monday, May 30, 2011

XNA Game Design: Keyboard Input

A windows game can take three types of input. These are from the Keyboard, Mouse and Xbox 360 game controller (Gamepad). This guide will not (for now) cover input from the Gamepad. This chapter in particular will focus on Keyboard input.


Input with the keyboard is relatively simple, and at this juncture you may choose to integrate keyboard input directly into the Game1 class, or making your own separate Input class to work with the Game1 class. This guide will be making it into a separate class, and therefore will require a separate .cs file.


To create a new class, right click on the Tutorial Project Icon in the Solution Explorer pane, and follow the command tree to Add, Class..., and then in the new window select Class and name it Input.cs


Remember this. We will be creating many, many more classes from here on out. This will be the only time class creation will be explicitly written out.

As far as I've seen, there doesn't seem to be an effective way of doing this. Every time you create a new class you need to re-append the necessary using statements used in all other classes. If anyone knows a way to make Visual Studio add them by default, send me a message.

These can be found already listed out at the top of the Game1 class, but I'll list them here as well. Just copy and past the following to continue using our favorite XNA framework in all our classes.


using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


Keyboard input is handled through the Keyboard class. The Keyboard class has a method called GetState. GetState returns the status of the keyboard in the form of a KeyBoardState object. KeyboardState objects contain the methods that check whether certain keys are pressed (IsKeyDown and IsKeyUp).

If your brain has since shut off in those few lines, I don't blame you. Overall however it's not as painful as it seems. Let's get started, shall we?

Our Input class will need to be static, and subsequently most other properties will be static as well. We Really just need an update method and two KeyBoardState properties.

static class Input
{
    //current state of the keyboard
    public static KeyboardState State Keyboard.GetState();
    //previous state of the keyboard
    static KeyboardState PrevState; 

    public static void Update()
    {
        PrevState = State;
        State = Keyboard.GetState();
    }
}

This simple code effectively gets the job done. Because the class is static, it can be referenced globally throughout the program (and something as ubiquitous as game input should be globally available to all components of the program). We're not making instances of Input, and we're presuming the player has only one keyboard hooked up. It all works.

Now an observant person would look at this code and ask, "Why do we have two KeyboardState properties if we would only want to use one?"  We need the PrevState property mostly for making methods down the line that handle more complex input questions that something like Input.State.IsKeyDown() can't handle.

Speaking of which, that's the homework. Tomorrow's lesson will cover Mouse input, which is considerably more difficult.

Yesterday's Homework Solution: In the Draw method, the first line of code should read
GraphicsDevice.Clear(Color.CornflowerBlue);
Every time the draw method is called, this line of code erases everything currently on the screen and replaces with our friendly Cornflower Blue color. Changing .CornflowerBlue to .Black will make the screen default to black. Microsoft has provided a number of public static color constants for us to work with. More on all this at a later chapter.


Homework: a method like Input.State.IsKeyDown() can 't tell the difference between whether a key is being pressed repeatedly, or being held down (truthfully, it just tells if a key is being pressed down in the first place). Design a method to return true if a key is being pressed and has not been held down.

No comments:

Post a Comment