예제 #1
0
        private void MoveSnake()
        {
            while (snakeParts.Count >= snakeLength)
            {
                GameArea.Children.Remove(snakeParts[0].UiElement);
                snakeParts.RemoveAt(0);
            }
            foreach (SnakePart snakePart in snakeParts)
            {
                (snakePart.UiElement as Rectangle).Fill = snakeBodyBrush;
                snakePart.IsHead = false;
            }
            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;
            }
            snakeParts.Add(new SnakePart()
            {
                Position = new Point(nextX, nextY),
                IsHead   = true,
            });
            DrawSnake();
            DoCollisionCheck();
        }
예제 #2
0
        private void DoCollisionCheck()
        {
            SnakePart snakeHead = snakeParts[snakeParts.Count - 1];

            if ((snakeHead.Position.X == Canvas.GetLeft(snakeFood)) && (snakeHead.Position.Y == Canvas.GetTop(snakeFood)))
            {
                EatSnakeFood();
                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 snakeParts.Take(snakeParts.Count - 1))
            {
                if ((snakeHead.Position.X == snakeBodyPart.Position.X) && (snakeHead.Position.Y == snakeBodyPart.Position.Y))
                {
                    EndGame();
                }
            }
        }