Esempio n. 1
0
        private void MoveSnake()
        {
            while (snakeBodyParts.Count >= snakeLength)
            {
                GameArea.Children.Remove(snakeBodyParts[0].UiElement);
                snakeBodyParts.RemoveAt(0);
            }

            foreach (snakePart snakeBody in snakeBodyParts)
            {
                (snakeBody.UiElement as Ellipse).Fill = snakeBodyColor;
                snakeBody.IsHead = false;
            }

            snakePart snakeHead = snakeBodyParts[snakeBodyParts.Count - 1];
            double    nextX     = snakeHead.Position.X;
            double    nextY     = snakeHead.Position.Y;

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

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

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

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

            snakeBodyParts.Add(new snakePart()
            {
                Position = new Point(nextX, nextY),
                IsHead   = true
            });
            DrawSnake();
            CollisionCheck();
        }
Esempio n. 2
0
        private void CollisionCheck()
        {
            snakePart snakeHead = snakeBodyParts[snakeBodyParts.Count - 1];

            if ((snakeHead.Position.X == Canvas.GetLeft(food)) && (snakeHead.Position.Y == Canvas.GetTop(food)))
            {
                SnakeEats();
                return;
            }

            if ((snakeHead.Position.Y < 0) || (snakeHead.Position.Y >= GameArea.ActualHeight) ||
                (snakeHead.Position.X < 0) || (snakeHead.Position.X >= GameArea.ActualWidth))
            {
                EndGame();
            }

            foreach (snakePart snakeBodyPart in snakeBodyParts.Take(snakeBodyParts.Count - 1))
            {
                if ((snakeHead.Position.X == snakeBodyPart.Position.X) && (snakeHead.Position.Y == snakeBodyPart.Position.Y))
                {
                    EndGame();
                }
            }
        }