Creating a Simple 3D Game with XNA/Adding controls

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Taking Keyboard Input[edit | edit source]

To handle the input to the game we will be taking input from the keyboard, which will then be used to move the fish around the landscape, and escape the game.

Start by creating a new method in your Game1 file called 'HandleInput', and create a new keystate variable. When this method is called, the current state of the keyboard is stored as part of the keystate variable, which then can be accessed by the gamelogic.

//Handles input
private void HandleInput(GameTime gameTime)
{
    KeyboardState currentKeyboardState = Keyboard.GetState();
}

Next add the following conditional.

// Check for exit.
if (currentKeyboardState.IsKeyDown(Keys.Escape))
{
    Exit();
}

This provides the simplest way to explain how this process works, you can construct as part of any conditional, a way to monitor the position of any key. Place a call to this method in your update code, using the 'gameTime' variable as an argument (which we will use later). With this code in place, you should be able to exit the code at any time by pushing the escape code.

Next, we will place in a similar conditional to use the arrow keys. Add the following lines in your 'HandleInput' method.

//Move the fish with the arrow keys
float speed = 0.02f; //Dictates the speed
if (currentKeyboardState.IsKeyDown(Keys.Left))
{
    FishMen.Translation.Y -= speed * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
}
if (currentKeyboardState.IsKeyDown(Keys.Right))
{
    FishMen.Translation.Y += speed * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
}
if (currentKeyboardState.IsKeyDown(Keys.Up))
{
    FishMen.Translation.X += speed * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
}
if (currentKeyboardState.IsKeyDown(Keys.Down))
{
    FishMen.Translation.X -= speed * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
}

In order to make the effect more visible change the following lines in the Update method.

cameraPosition = cameraTarget = FishMen.Translation;
cameraPosition.Z += 20.0f;

to

cameraPosition = cameraTarget = FishMen.Translation / 2;
cameraPosition.Z -= 20.0f;

And run the code. This should produce an over the shoulder view of your fish as it moves around. The logic behind using the 'ElapsedGameTime' variable, which monitors the difference in time between calls of the update method, compensating for any changes in processing time between frames. If it wasn't for this, the faster your computer the more erratic and faster the controls would be. As it is, it should be consistent between computers. Run the code, and you should see it move around with pushes of the arrow keys.

Finally, to dictate the boundarys of your fish, create a new 'Vector2' variable called 'boundarys', which will dictate the maximum position of the fish in 2D space. Change your arrow handler code as follows.

//Move the fish with the arrow keys
float speed = 0.02f; //Dictates the speed
Vector2 boundarys = new Vector2(14f);

if (currentKeyboardState.IsKeyDown(Keys.Left) && FishMen.Translation.Y > -boundarys.Y)
{
    FishMen.Translation.Y -= speed * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
}
if (currentKeyboardState.IsKeyDown(Keys.Right) && FishMen.Translation.Y < boundarys.Y)
{
    FishMen.Translation.Y += speed * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
}
if (currentKeyboardState.IsKeyDown(Keys.Up) && FishMen.Translation.X < boundarys.X)
{
    FishMen.Translation.X += speed * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
}
if (currentKeyboardState.IsKeyDown(Keys.Down) && FishMen.Translation.X > -boundarys.X)
{
    FishMen.Translation.X -= speed * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
}

This should produce a game where the fish only moves to a certain position and stops. Feel free to adjust the movement by monitoring the 'boundary' and 'speed' variable to taste.