XNA GameStudio 2.0
Drawing The Bat On Screen

Displaying The Player 1 Bat

Now that we have written some code to describe how a bat will behave in the game, we need to actually make it happen. We will do this in the Game1.cs file - make sure that this file is in the code editor window.

Find the section near the top of the code window where the fields have been defined for the Game1 class - add the two items shown below in bold.

GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D bgTexture;
Bat player1;
Bat player2;

Next go to the LoadContent() procedure and immediately below the statement we used to load the background texture, write the following statements,

player1 = new Bat(Content.Load<Texture2D>("Sprites\\bat"));
player1.position = new Vector2(10.0f,280.0f);

The second line specifies the starting position of player 1's bat on the screen - you can adjust this if necessary.

To make the bat appear on the screen, we have to add some code to the Draw() section of the code window. Look for the statement you wrote earlier that drew the background image on the screen. Add the following line of code,

spriteBatch.Draw(player1.sprite, player1.position, Color.White);

Test that the bat appears on the screen as you expect - make any necessary adjustments.

Allowing The Player To Move The Bat

When we deal with user input in XNA, we examine the state of the keyboard or gamepad - that is, we check which keys are pressed.

We are going to create a new procedure that contains all of the code that we need to use for dealing with user input in the game. To start with we will add code for the movement of player 1's bat.

   public void HandleInput()
   {
      KeyboardState kbState = Keyboard.GetState();
      if (kbState.IsKeyDown(Keys.W))
      {
         player1.MoveUp();
      }
      if (kbState.IsKeyDown(Keys.S))
      {
         player1.MoveDown();
      }
   }

The two if statements are the key to this.

In order to test this, we need to add one more statement to the Update() procedure in our code. Locate this procedure and, directly below the line which reads // TODO: Add your update logic here, add the following line,

HandleInput();

Test the bat movement - you should be able to move the player 1 bat up and down using the keys that we specified in the HandleInput() method.

Keeping The Bat On Screen

OK - you will have noticed that it is possible to move the bat right off the screen - this doesn't really help the game play. We need to stop this from happening.

public void HandleInput()
{
   KeyboardState kbState = Keyboard.GetState();
   if (kbState.IsKeyDown(Keys.W))
   {
      player1.MoveUp();
      if (player1.position.Y < 0)
         player1.position.Y = 0;
   }
   if (kbState.IsKeyDown(Keys.S))
   {
      player1.MoveDown();
      if (player1.position.Y > 600.0f - player1.sprite.Height)
         player1.position.Y = 600.0f - player1.sprite.Height;
   }
}

The numbers used here are based on the dimensions of the screen that we set earlier - if you change the screen height, you need to think about this. This code keeps the bat on screen - you may wish to stop the bat from going right up to the edge - and touching the white lines of the background graphic - or you might not. You may also want to allow your bat to appear at the bottom of the screen - adjustments made here will allow that to happen.

Adding The Player 2 Bat

The code for the second bat will be very similar. Look back at the top of the page and follow the steps that you used to make the first bat. The starting position of the second bat will need a different X coordinate. You also need to use different keys for the movement - the Up and Down arrows are on the other side of the keyboard and could be used for this. The movement code should be added after the two IF statements in the HandleInput() method.

Bear in mind that, to this point, you have been able to create your program by copying code directly from these pages - now you have to think a little and create some lines of code based on what you have done before.