XNA GameStudio 2.0
Drawing & Moving The Ship

Now that we have created a class to help us draw and manage objects in our game, we should use it to draw something on the screen.

Return to the code window for the main game class. There are 2 fields that are created automatically when you start up a new project. We will need to add another one to keep track of the player's ship in our game. This field will be of the type GameObject,

GameObject ship;

Drawing The Ship

We can't draw the ship on the screen without loading up the graphic and setting up where we want to draw it. We do this in the LoadContent() method of the main game class.

// TODO: use this.Content to load your game content here
ship = new GameObject(Content.Load<Texture2D>("Sprites\\rocket"));
ship.position = new Vector2(380.0f, 500.0f);

To draw the ship on the screen, we need to add some lines to the Draw() method of the main game class.

// TODO: Add your drawing code here
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
spriteBatch.Draw(ship.sprite, ship.position, Color.White);
spriteBatch.End();

At this point, rather than just copy and test the code, compare it with the way that we drew objects on the screen in the First Game. You should see some consistency in our approach and start to make sense of what we are doing.

You can also adjust the numbers used to set up the starting position of the ship.

Moving The Ship

All of the keyboard interaction in our game is concerned with the ship. We will create a new method to deal with the keyboard input. This method will be roughly as you see below.

private void HandleInput()
{
   KeyboardState kbState = Keyboard.GetState();
   if (kbState.IsKeyDown(Keys.Left))
   {
      ship.position.X -= 5.0f;
   }
   if (kbState.IsKeyDown(Keys.Right))
   {
      ship.position.X += 5.0f;
   }
}

All we need to do now is make sure that this code is executed during the Update() method. Locate that method and add the following line underneath the comment.

// TODO: Add your update logic here
HandleInput();

Test the game and make sure that you can see and move the ship on the screen. You will notice that the ship can be moved off the edges of the screen. This isn't what we want to happen and we will have to address that problem next.

Keeping The Ship On Screen

We need to adjust our HandleInput() method to make sure that the ship cannot be moved out of the game window. Each time we change the position of the ship, we need to make sure that the new position keeps the ship on screen.

private void HandleInput()
{
   KeyboardState kbState = Keyboard.GetState();
   if (kbState.IsKeyDown(Keys.Left))
   {
      ship.position.X -= 5.0f;
      if (ship.position.X < 0)
         ship.position.X = 0;
   }
   if (kbState.IsKeyDown(Keys.Right))
   {
      ship.position.X += 5.0f;
      if (ship.position.X > graphics.GraphicsDevice.Viewport.Width - ship.sprite.Width)
         ship.position.X = graphics.GraphicsDevice.Viewport.Width - ship.sprite.Width;
   }
}

To prevent the ship from moving off the left hand side, we check that the X coordinate has not fallen below 0. If it has, we make it 0. To stop the ship flying off the right hand side, we need to take account of the width of the sprite.

Notice that we only check the position of the ship when we move and only for the direction in which we are moving it.