private static void TryNewDirection(SnakeDirections newDirection)
        {
            switch (newDirection)
            {
            case SnakeDirections.Down:
                if (_sSnake._mSnakeDirection == SnakeDirections.Up)
                {
                    return;
                }
                break;

            case SnakeDirections.Left:
                if (_sSnake._mSnakeDirection == SnakeDirections.Right)
                {
                    return;
                }
                break;

            case SnakeDirections.Right:
                if (_sSnake._mSnakeDirection == SnakeDirections.Left)
                {
                    return;
                }
                break;

            case SnakeDirections.Up:
                if (_sSnake._mSnakeDirection == SnakeDirections.Down)
                {
                    return;
                }
                break;
            }

            UpdateSnakeDirection(newDirection);
        }
示例#2
0
    private void UpdateAnimation()
    {
        if (currentState == State.Sunk)
        {
            animator.SetBool("Sunk", true);
            return;
        }
        else
        {
            animator.SetBool("Sunk", false);
        }

        float actualRotation = transform.localEulerAngles.z - movement.angleAdjustment;

        while (actualRotation < 0.0f)
        {
            actualRotation += 360.0f;
        }

        while (actualRotation > 360.0f)
        {
            actualRotation -= 360.0f;
        }

        SnakeDirections dir = SnakeDirections.Up;

        if ((actualRotation > 45.0f) && (actualRotation < 135.0f))
        {
            dir = SnakeDirections.Up;
        }
        else if ((actualRotation > 135.0f) && (actualRotation < 225.0f))
        {
            dir = SnakeDirections.Left;
        }
        else if ((actualRotation > 225.0f) && (actualRotation < 315.0f))
        {
            dir = SnakeDirections.Down;
        }
        else if ((actualRotation > 315.0f) || (actualRotation < 45.0f))
        {
            dir = SnakeDirections.Right;
        }

        animator.SetInteger("Direction", (int)dir);
    }
示例#3
0
        public void OnKeyDown(Key e)
        {
            SnakeDirections originalDirection = SnakeDirection;

            switch (e)
            {
            case Key.Right:
                if (originalDirection != SnakeDirections.Left)
                {
                    SnakeDirection = SnakeDirections.Right;
                }
                break;

            case Key.Left:
                if (originalDirection != SnakeDirections.Right)
                {
                    SnakeDirection = SnakeDirections.Left;
                }
                break;

            case Key.Down:
                if (originalDirection != SnakeDirections.Up)
                {
                    SnakeDirection = SnakeDirections.Down;
                }
                break;

            case Key.Up:
                if (originalDirection != SnakeDirections.Down)
                {
                    SnakeDirection = SnakeDirections.Up;
                }
                break;

            case Key.Space:
                StartNewGame();
                break;
            }

            if (SnakeDirection != originalDirection)
            {
                MoveSnake();
            }
        }
        // Checks if the desired direction will cause a collision with self, wall, or other snake.
        private bool CheckForCollision(SnakeDirections desiredDirection)
        {
            // Get the starting position as our snake head.
            Position desiredPosition = _board.Snakes.Find(snake => (snake.Name == "you")).Body[0];

            // Adjust the position based on the desired direction we want to go.
            if (desiredDirection == SnakeDirections.Down || desiredDirection == SnakeDirections.Up)
            {
                desiredPosition.Y += (long)desiredDirection;
            }
            else
            {
                desiredPosition.X += (long)desiredDirection;
            }

            // If the direction collides us with the wall, return true for collision
            if (desiredPosition.X < 0 || desiredPosition.X >= _board.Width)
            {
                return(true);
            }
            if (desiredPosition.Y < 0 || desiredPosition.X >= _board.Height)
            {
                return(true);
            }


            // Look through all the snakes to see if we'll collide with it.

            /*foreach (var snake in _board.Snakes )
             * {
             *
             *  foreach (var snakePart in snake.Body)
             *  {
             *
             *  }
             * }*/

            // If we made it this far, then there will be no collision.
            return(false);
        }
        private static void UpdateSnakeDirection(SnakeDirections newDirection)
        {
            _sSnake._mSnakeDirection = newDirection;
            switch (newDirection)
            {
            case SnakeDirections.Down:
                _sSnake._mDirection = -Vector2.Up;
                break;

            case SnakeDirections.Left:
                _sSnake._mDirection = -Vector2.Right;
                break;

            case SnakeDirections.Right:
                _sSnake._mDirection = Vector2.Right;
                break;

            case SnakeDirections.Up:
                _sSnake._mDirection = Vector2.Up;
                break;
            }
        }
示例#6
0
 public void StartNewGame()
 {
     try
     {
         currentPoint   = 0;
         snakeLenght    = SnakeStartintLenght;
         SnakeDirection = SnakeDirections.Right;
         if (snakeParts != null)
         {
             foreach (SnakePart snakePart in snakeParts)
             {
                 if (_canvas.Children.Contains(snakePart.UiElement))
                 {
                     _canvas.Children.Remove(snakePart.UiElement);
                 }
             }
             snakeParts.Clear();
         }
         snakeParts.Add(new SnakePart {
             Position = new Point(_squareSize * 5, _squareSize * 5)
         });
         DrawSnake();
         if (snakeFood != null && _canvas.Children.Contains(snakeFood.UiElement))
         {
             _canvas.Children.Remove(snakeFood.UiElement);
             snakeFood = null;
         }
         DrawSnakeFood();
         OnNewGameStarted();
         UpdateGameStatus();
     }
     catch (Exception ex)
     {
         throw;
     }
 }