Esempio n. 1
0
        private void MoveSnake()
        {
            // Removes the last part of the snake, in preparation of the new part added below
            while (snakeParts.Count >= snakeLength)
            {
                GameArea.Children.Remove(snakeParts[0].UiElement);
                snakeParts.RemoveAt(0);
            }
            // add a new element to the snake, which will be the (new) head
            // then mark all existing parts as non-head (body) elements and then
            // make sure that they use the body brush
            foreach (SnakePart snakePart in snakeParts)
            {
                (snakePart.UiElement as Rectangle).Fill = snakeBodyBrush;
                snakePart.IsHead = false;
            }

            // Determines in which direction to expand the snake, based on the current direction
            SnakePart snakeHead = snakeParts[snakeParts.Count - 1];
            double    nextX     = snakeHead.Position.X;
            double    nextY     = snakeHead.Position.Y;

            switch (snakeDirection)
            {
            case SnakeDirection.Left:
                nextX -= SnakeSquareSize;
                break;

            case SnakeDirection.Right:
                nextX += SnakeSquareSize;
                break;

            case SnakeDirection.Up:
                nextY -= SnakeSquareSize;
                break;

            case SnakeDirection.Down:
                nextY += SnakeSquareSize;
                break;
            }

            // Adds the new head part to our list of snake parts
            snakeParts.Add(new SnakePart()
            {
                Position = new Point(nextX, nextY),
                IsHead   = true
            });
            //snake is then drawn again
            DrawSnake();
            // Collision check method is then called
            DoCollisionCheck();
        }