Beispiel #1
0
        /// <summary>
        /// Updates the player
        /// </summary>
        /// <param name="gameTime">The game's GameTime</param>
        public void Update(GameTime gameTime)
        {
            _currentKeyboardState = Keyboard.GetState();

            // Move left
            if (_currentKeyboardState.IsKeyDown(Keys.A) && _previousKeyboardState.IsKeyUp(Keys.A))
            {
                if (count > 0)
                {
                    state = PlayStateNorth.West;
                    count--;
                    Bounds.X = Bounds.X - offset;
                }
            }
            // Move Right
            else if (_currentKeyboardState.IsKeyDown(Keys.D) && _previousKeyboardState.IsKeyUp(Keys.D))
            {
                // move right
                if (count < 4)
                {
                    state = PlayStateNorth.East;
                    count++;
                    Bounds.X = Bounds.X + offset;
                }
            }
            else if (_currentKeyboardState.IsKeyDown(Keys.S))
            {
                state = PlayStateNorth.Idle;
                //shoot
            }
            // Update the player animation timer when the player is moving
            if (state != PlayStateNorth.Idle)
            {
                timer += gameTime.ElapsedGameTime;
            }

            // Determine the frame should increase.  Using a while
            // loop will accomodate the possiblity the animation should
            // advance more than one frame.
            while (timer.TotalMilliseconds > ANIMATION_FRAME_RATE)
            {
                // increase by one frame
                frame++;
                // reduce the timer by one frame duration
                timer -= new TimeSpan(0, 0, 0, 0, ANIMATION_FRAME_RATE);
            }

            // Keep the frame within bounds (there are four frames)
            frame %= 2;

            _previousKeyboardState = _currentKeyboardState;
        }
Beispiel #2
0
        /// <summary>
        /// Initializes the player, setting its initial size
        /// and centering it on the left side of the screen.
        /// </summary>
        public void Initialize(int of, int distance)
        {
            state          = PlayStateNorth.Idle;
            timer          = new TimeSpan(0);
            count          = 2;
            offset         = of;
            borderDistance = distance;
            Bounds.Width   = 40;
            Bounds.Height  = 40;
            Bounds.Y       = 120;
            Bounds.X       = borderDistance + count * offset;

            life = 30;
        }