You now know the basics of computer game programming. You know that a program is actually a sequence of statements, each of which performs a single action. You have seen that statements are held inside methods, each of which performs a particular task, and that methods are held in classes along with data. The program itself works on data values, which are held in variables of a particular type, and the program can make decisions based on the values that the variables have. (If none of this makes much sense, reread 2, Programs, Data, and Pretty Colors, until it does.) Now you are going to expand your understanding to include how to receive input from the outside world so that games can actually react to what the player does. You shall see that once we have done this, a number of possibilities open up, and you can create some truly silly games, including Color Nerve, Mind Reader, The Thing That Goes Bump in the Night, and Gamepad Racer.

Using Barcode encoder for ASP.NET Control to generate, create Quick Response Code image in ASP.NET applications.

www.OnBarcode.com

In 2, you created a light that changes color over time. I also mentioned that this is the kind of thing that will be used in the starships of the future. A color-changing light is not all that useful for reading books, but it s great for setting moods; what our starship captain really needs is a light that she can set to any color. So now you are going to make a lamp that can be controlled by an Xbox gamepad. The user presses the red, blue, green, and yellow buttons on the gamepad to increase the amount of that color in the light. To make this work, you have to discover how to read the gamepad.

Before you start looking at gamepads, though, you need to decide how the program will actually work. Consider the following statement of C# from the previous mood-light program, which is part of the Update method:

Using Barcode creation for .NET Control to generate, create ECC200 image in .NET applications.

www.OnBarcode.com

This is one of the tests that controls the intensity of the red part of the color. What it is saying is If the Boolean value redCountingUp is True, increase the value of redIntensity by 1. The statement is processed each time Update is called (at the moment that is 60 times a second), so this means that if redCountingUp is True, the red intensity of the screen gets progressively brighter over time. You want to write some code that says, If the red button on Gamepad 1 is being pressed, increase the value of redIntensity by 1. Then, if the player holds down the button, the screen gets redder. So all you have to do is change this test to read the button on the gamepad, and you can create a user-controlled light easily.

Using Barcode creator for Word Control to generate, create Code128 image in Word applications.

www.OnBarcode.com

The gamepads are actually very complex devices. They are connected to the host device either by a universal serial bus (USB) cable or by a wireless connection. As far as you are concerned, the way that programs work with gamepads does not depend on how they are connected. The connection to a gamepad can be used to read the buttons and joysticks and can also be used to send commands to the gamepad for example, to turn the vibration effect on and off. The Xbox and XNA provide support for up to four gamepads connected simultaneously.

The gamepad information is represented in XNA by means of a class called GamePadState. The job of this class is to provide the connection between the program and the physical gamepad that the player is holding. To understand how you are going to use this, you have to learn a bit more about how classes work. You have already seen what a class is in the section Games and Classes in 2. A class contains data (variables that can hold stuff) and methods (code that can do stuff). You can think of a class as an office, with a desk holding the variables and people acting as the methods. Figure 3-1 shows the office plan for the class Game1, which you have seen is the basis of an XNA game. This class contains some variables on the desk (in this case, the background color intensities) and two methods, which we have called Mr. Draw and Mrs. Update. Each method has a corresponding telephone. Programs can place calls to the telephones to request that the method perform the required task.