Friday, October 10, 2008

XNA Part 4 - Controls

Now that we have drawn a texture on the screen and made it move on a sine wave, lets make it move in response to our input.

There are 3 basic input devices that you can access. The Keyboard, The Mouse and a Gamepad. The wired XBox 360 controller is USB, so you can plug one straight into your computer. But alas I do not have one (unless someone has one they want to send me), so I'll only mention it here a little bit, if you are writing games for XBox360, you will need to program with it in mind. I'm going to focus on the keyboard, since it ubiquitous on PCs.

We access the keyboard during the update method and look at the keyboard 'state'

KeyboardState state = Keyboard.GetState();

now the object state will contain information about the keyboard at that particular time slice. We can look at that object and based on information in it, make updates to our game. We will pass 4 different keys to the "IsKeyDown" method of the state object which returns true if the key we pass it is indeed pressed at that moment. We pass the IsKeyDown method a variable from the "Keys" enumeration which contains a list of all the keys that we could be pressing. Here is the code for checking and updating.

if (state.IsKeyDown(Keys.Left)) { position.X -= 5; }
if (state.IsKeyDown(Keys.Right)) { position.X += 5; }
if (state.IsKeyDown(Keys.Up)) { position.Y -= 5; }
if (state.IsKeyDown(Keys.Down)) { position.Y += 5; }

So we check each of the 4 keys and update the position accordingly.

Now if we compile and run our game, our arrow keys will move our texture.

To access gamepad information we would do this

GamePadState pad = GamePad.GetState(PlayerIndex.One);
position.X += pad.ThumbSticks.Right.X * 5.0f;
position.Y += pad.ThumbSticks.Right.Y * 5.0f;

pad is a GamePadState object that we get from GamePad.GetState and we give it the player index (One through Four). Then we get the value of the Right thumbstick (which goes from -1 to 1) and use that as a multiplier for the addition to our position. That way the further we press the thumbstick the faster it will move! We do not have that degree of control on a keyboard.

Here is the code:

using System;
using System.Collections.Generic;
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.Net;
using Microsoft.Xna.Framework.Storage;

namespace TutorialGame
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D alien;
Vector2 position = Vector2.Zero;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

protected override void Initialize()
{
position.Y = graphics.GraphicsDevice.Viewport.Height / 2;
base.Initialize();
}

protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
alien = Content.Load
("sprites\\alien");
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

KeyboardState state = Keyboard.GetState();

if (state.IsKeyDown(Keys.Left)) { position.X -= 5; }
if (state.IsKeyDown(Keys.Right)) { position.X += 5; }
if (state.IsKeyDown(Keys.Up)) { position.Y -= 5; }
if (state.IsKeyDown(Keys.Down)) { position.Y += 5; }

base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(alien, position, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}

Post a Comment