// handle player input from the keyboard public void ProcessInput(double elapsed, Player player, KeyboardState key) { if (!player.IsActive && key.IsKeyDown(Keys.Enter)) { // start or join the game player.Init(); player.IsActive = true; } else if (player.IsActive) { // change in location Vector2 delta = Vector2.Zero; // moving left or right? if (key.IsKeyDown(Keys.Left)) { delta.X = (float)(-player.MovePixelsPerSecond * elapsed); } else if (key.IsKeyDown(Keys.Right)) { delta.X = (float)(player.MovePixelsPerSecond * elapsed); } // moving up or down? if (key.IsKeyDown(Keys.Up)) { delta.Y = (float)(-player.MovePixelsPerSecond * elapsed); } else if (key.IsKeyDown(Keys.Down)) { delta.Y = (float)(player.MovePixelsPerSecond * elapsed); } // actually move the player player.Location += delta; // if the player is pressing the action // button, try to fire a bullet if (key.IsKeyDown(Keys.Space) && player.Fire()) { GameObjectManager.AddBullet(player, true); } } }
// handle game pad input public void ProcessInput(double elapsed, Player player, GamePadState pad) { if (!player.IsActive && pad.Buttons.Start == ButtonState.Pressed) { // start or join the game player.Init(); player.IsActive = true; } else if (player.IsActive) { // change in location Vector2 delta = Vector2.Zero; // moving left or right? if (pad.ThumbSticks.Left.X < 0) { delta.X = (float)(-player.MovePixelsPerSecond * elapsed); } else if (pad.ThumbSticks.Left.X > 0) { delta.X = (float)(player.MovePixelsPerSecond * elapsed); } // moving up or down? if (pad.ThumbSticks.Left.Y > 0) { delta.Y = (float)(-player.MovePixelsPerSecond * elapsed); } else if (pad.ThumbSticks.Left.Y < 0) { delta.Y = (float)(player.MovePixelsPerSecond * elapsed); } // actually move the player player.Location += delta; // if the player is pressing the action // button, try to fire a bullet if (pad.Buttons.A == ButtonState.Pressed && player.Fire()) { GameObjectManager.AddBullet(player, true); } } }