This week, we’ll be using the accelerometer to move the player around. For those of you without a device, this one is going to be a bit tricky, as the emulator has no support for the accelerometer at this time. To do this, we’ll put in a check to see whether we are running on a device or an emulator, and use the accelerometer if it’s the device, and multi-touch if it’s the emulator. To begin, grab the latest version of the code from the SkyDrive share.

In order to use the accelerometer, we’ll need to add an assembly reference to Microsoft.Devices.Sensors, and to test if the game is running on the emulator or a device, we add a reference to Microsoft.Phone. To add the reference, right-click on the references folder in the TriangleShooter project, choose Add Reference, and choose the two references from the list. Additionally, the following Using statement will need to be added for easy access to the Accelerometer:

Assembly for Accelerometer
  1. using Microsoft.Devices.Sensors;

We’ll create a variable for the Acceleromter called accelerometer:

Accelerometer Variable
  1. Accelerometer accelerometer;

We’ll check to see if we are running on the device, and if so, initialize the variable, set up a method that will handle the accelerometer, and start the monitoring.

Initializing the Accelerometer
  1. if (Microsoft.Devices.Environment.DeviceType == Microsoft.Devices.DeviceType.Device)
  2. {
  3.     accelerometer = new Accelerometer();
  4.     accelerometer.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs>(accelerometer_ReadingChanged);
  5.     try
  6.     {
  7.         accelerometer.Start();
  8.     }
  9.     catch (AccelerometerFailedException)
  10.     {
  11.     }
  12. }

The method monitoring the reading changed event will update the position and rotation similar to the method we used around multi-touch.

Accelerometer ReadingChanged
  1. void accelerometer_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
  2. {
  3.     if (!isPlayerDead && player != null)
  4.     {
  5.         Vector2 direction = new Vector2(Math.Max(-.5f, Math.Min(.5f, (float)-e.Y)), Math.Max(-.5f, Math.Min(.5f, (float)-e.X)));
  6.  
  7.         player.Rotation = (float)(Math.Atan2(direction.Y, direction.X));
  8.  
  9.         player.Position += direction * player.Speed;
  10.  
  11.         player.Position = new Vector2(Math.Min(Math.Max(player.Position.X, 0), 800), Math.Min(Math.Max(player.Position.Y, 0), 480));
  12.     }
  13. }

If you make only these changes, the accelerometer will work, but the player won’t move around, because we never actually set the player speed. We can update the player initialization to the following to make that happen.

Player Initialization
  1. player = new Player() { Avatar = txPlayer, Position = new Vector2(100f, 240f), Rotation = 0f, Speed = 15f };

Finally, we’ll update the UpdatePlayer method to check if the game is running on the emulator, and use the multi-touch if it is.

UpdatePlayer
  1. private void UpdatePlayer()
  2. {
  3.     if (Microsoft.Devices.Environment.DeviceType == Microsoft.Devices.DeviceType.Emulator)
  4.     {
  5.         foreach (TouchLocation tl in TouchPanel.GetState())
  6.         {
  7.             if (tl.State == TouchLocationState.Pressed)
  8.             {
  9.                 if (movementId == 0)
  10.                 {
  11.                     movementId = tl.Id;
  12.                 }
  13.             }
  14.  
  15.             if (tl.Id == movementId)
  16.             {
  17.                 Vector2 direction = tl.Position - player.Position;
  18.  
  19.                 if (direction.LengthSquared() > 100)
  20.                 {
  21.                     direction.Normalize();
  22.  
  23.                     player.Rotation = (float)(Math.Atan2(direction.Y, direction.X));
  24.  
  25.                     player.Position += direction * 10;
  26.                 }
  27.             }
  28.  
  29.             if (tl.State == TouchLocationState.Released)
  30.             {
  31.                 if (tl.Id == movementId)
  32.                 {
  33.                     movementId = 0;
  34.                 }
  35.             }
  36.         }
  37.     }
  38. }

Now we run with multi-touch on the emulator, and the accelerometer on the device.

At this point, we’re pretty complete as far as the game goes. There are a few things we can do to clean up the code, and to improve the gameplay, and I will post updates as I continue with the project, but next week, we’ll be introducing a new project, so stay tuned.

Download the latest version of the source code.