Datasheet
Changing the Code
Instead of giving up here, think about some ways you can modify the code in a useful manner. First of all,
you should be able to quit your program by pressing Esc. By default, XNA just adds the following lines to
the
Update method, which quits the program when the Xbox 360 controller’s Back button is being pressed:
// Allows the default game to exit on Xbox 360 and Windows
if (GamePad.GetState(PlayerIndex.One).Buttons.Back ==
ButtonState.Pressed)
this.Exit();
To learn more about the Input classes, see Chapter 3, “Helper Classes.” For now, you will just use a
quick and dirty way to access the keyboard. If you modify the code in the following way, you can also
press Escape now to quit the application:
// Get current gamepad and keyboard states
GamePadState gamePad = GamePad.GetState(PlayerIndex.One);
KeyboardState keyboard = Keyboard.GetState();
// Back or Escape exits our game on Xbox 360 and Windows
if (gamePad.Buttons.Back == ButtonState.Pressed ||
keyboard.IsKeyDown(Keys.Escape))
this.Exit();
As you can see, you put the gamepad and keyboard states in extra variables to have easier access. If you
press F5 again, you can quit the game with Escape now.
Next you are going to jump a little bit ahead and load a graphic into your game. More details about
sprites are discussed in Chapter 2. The idea is to display a small background texture and tile it over the
whole screen. Then you implement some simple keyboard and gamepad controls to move the back-
ground as in a top-down racing game or a tile engine, which is often used in 2D role playing games. For
a more complex tile engine you would need more textures like stones, grass, water, mud, and so on, and
then even transition textures you can put between grass and water, for example. This requires more tex-
tures and custom code, but overall this is not a very hard topic. You can also find a lot of information
about this topic on the Internet if you are really interested in tile engines.
For your simple first project, you just add the texture shown in Figure 1-12.
Figure 1-12
17
Chapter 1: Introducing XNA
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 17