Chapter 10. Player Input and the User Interface

This chapter takes a closer look at the Input class that has been used so many times now. Then it discusses the ways to implement a good graphical user interface (GUI) for XNA Shooter. Handling the user input is very simple in XNA because, thanks to the Input namespace, you can easily get the mouse, keyboard, and gamepad states with just three lines of code:

mouseState = Mouse.GetState();
keyboardState = Keyboard.GetState();
gamePadState = GamePad.GetState(PlayerIndex.One);

If you don't have the XNA Input namespace added to the file you are currently working on, go with the text cursor over Mouse, Keyboard, or GamePad and press Ctrl+., which automatically adds the following line to your using directives:

using Microsoft.Xna.Framework.Input;

Now you can use these states and check if the gamepad is connected at all, if the spacebar is pressed on your keyboard, or where the mouse position is currently. This is exactly what you did in the first and second chapter when you wrote your first XNA game.

The more complex your games become, the more often you will have to repeat the same code over and over again. As with the classes in the graphics engine, you often do not add more functionality, but instead you make the existing functionality easier to use. For example, the Texture class was used to store the XNA texture, the texture size, and whether or not it has alpha; it also contains many useful methods to render the texture directly on the ...

Get Professional XNA™ Programming: Building Games for Xbox 360™ and Windows® with XNA Game Studio 2.0, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.