// Custom methods.

        /// <summary>
        /// The method takes care of the key input and will be used for the movement of the ship as well as launching the lasers.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values</param>
        public void checkInput(GameTime gameTime)
        {
            KeyboardState newState = Keyboard.GetState();

            if (newState.IsKeyDown(Keys.Right) || newState.IsKeyDown(Keys.D))
            {
                if (!oldState.IsKeyDown(Keys.Right))
                {
                    player.MoveRight();
                    previousKeyTime = gameTime.TotalGameTime;
                }
                else
                if (gameTime.TotalGameTime - previousKeyTime > tolerance)
                {
                    player.MoveRight();
                }
            }
            else if (newState.IsKeyDown(Keys.Left) || newState.IsKeyDown(Keys.A))
            {
                if (!oldState.IsKeyDown(Keys.Left))
                {
                    player.MoveLeft();
                    previousKeyTime = gameTime.TotalGameTime;
                }
                else
                if (gameTime.TotalGameTime - previousKeyTime > tolerance)
                {
                    player.MoveLeft();
                }
            }

            if (newState.IsKeyDown(Keys.Space))
            {
                laser.Launch(player.GetBoundary(), gameTime);
            }
        }
Example #2
0
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.D)
            {
                player.MoveRight();
            }
            else if (e.KeyCode == Keys.A)
            {
                player.MoveLeft();
            }

            if (e.KeyCode == Keys.Space)
            {
                playerBullet.FireBullet(player);
            }
        }