Games are useless unless the player can interact with them. In fact, there's a special name for a game that doesn't accept input -- it's called a screen saver.
The XNA Framework provides support for three categories of user input devices: the Xbox 360 Controller, the keyboard, and the mouse. This article describes the differences between each, and presents code and design guidelines to help you use player input in your game to maximum effect. In this article , we will develop a simple game project that graphically shows the state of all the buttons of the Xbox 360 Controller and turns the rumble motors on and off to give the player tactile feedback.
GamePad Overview
The XNA Framework has support for 14 digital buttons, 4 analog buttons, and 2 vibration motors when using the Xbox 360 Controller. The media button is not supported. Up to four actively connected controllers are supported, and the Xbox 360 Controller is supported on both the Windows and the Xbox 360 game console platforms. The number and variety of input sources on an Xbox 360 Controller make it the ideal input device for your games.
(The only type of controller that is supported by the XNA Framework is the Xbox 360 Controller. If you're writing a Windows game, and you want your game to support another type of controller (like the Microsoft Sidewinder Controller), you will need to access that controller via some other API.)
Polling
Input from the controller is collected by the XNA Framework via polling. During every frame of your game, you will ask the controller what the current state of its buttons is. This state information is wrapped in an XNA Framework structure known as the GamePadState, and it represents a snapshot of the state of all the controller's buttons at the time the request was made. This state information can be saved from frame to frame (so that you can determine which buttons were pressed or released since the last frame), and you can pass the state as a parameter to your methods (so that you don't need to poll the controller repeatedly, running the risk of retrieving conflicting information).
If you've done any Windows programming in the past, you've dealt with event-driven programming. Polling may seem inefficient by comparison, and you might think that it would be easy to miss player input between GetState calls. But, you need to keep in mind that your game is running at 15, 30, 60, or more frames per second. When you're gathering input data 60 times a second, it's not very likely that a player can press and release a button between any two polling requests. If you want to test this out, run the example that we develop in this chapter and try to press and release a button without the screen registering your action.
(Per the XNA Framework documentation, the state of a controller is undefined when the controller is disconnected. The API will allow you to poll for the state of a disconnected controller, and there is a boolean property in the GamePadState structure that indicates whether the controller is connected or not. All of the other controller state information is unreliable, and should not be trusted. You may want to automatically pause your game whenever there are no controllers connected.)
GamePadState information is retrieved using the GetState member of the GamePad class. Only four Xbox 360 Controllers are supported, and each must be polled separately. The following code shows how you would get the state of the controller used by player one.
// Poll the current controller state for player one GamePadState padState1 = GamePad.GetState(PlayerIndex.One);