コード例 #1
0
        private static void ClearCurrentSnakePartsFromConsole(Model.Snake snake)
        {
            PrintOnConsole(_oldTail.Row, _oldTail.Col, Constants.EMPTY_SPACE);
            PrintOnConsole(_oldHead.Row, _oldHead.Col, Constants.SNAKE_BODY);

            var snakeHead = snake.BodyParts.First();

            PrintOnConsole(snakeHead.Row, snakeHead.Col, snakeHead.Symbol);
        }
コード例 #2
0
        private static void DrawSnake(Model.Snake snake)
        {
            _oldTail = snake.BodyParts.Last();
            _oldHead = snake.BodyParts.First();

            foreach (var snakeBodyPart in snake.BodyParts)
            {
                PrintOnConsole(snakeBodyPart.Row, snakeBodyPart.Col, snakeBodyPart.Symbol, ConsoleColor.Yellow);
            }
        }
コード例 #3
0
        private static void CheckSnakeCollision(Model.Snake snake)
        {
            SnakeNode head = snake.Head;

            // check field boundaries
            bool isOutOfLeftTopBorders     = head.Row < 0 || head.Col < 0;
            bool isOutOfRightBottomBorders = head.Row > RowBoundary || head.Col >= ColBoundary;

            if (isOutOfLeftTopBorders || isOutOfRightBottomBorders)
            {
                GameOver();
            }


            // check food collision
            if (_food != null && head.Row == _food.Row && head.Col == _food.Col)
            {
                _food = null;
                snake.Grow();
            }

            // check self collision
            var currentNode = head.Prev;

            while (currentNode != null)
            {
                if (head.Row == currentNode.Row && head.Col == currentNode.Col)
                {
                    GameOver();
                }

                currentNode = currentNode.Prev;
            }

            // check obstacles collision
            foreach (var obstacle in _obstacles)
            {
                if (head.Row == obstacle.Row && head.Col == obstacle.Col)
                {
                    GameOver();
                }
            }
        }
コード例 #4
0
        private static void RunGameLoop()
        {
            Console.CursorVisible = false;
            var       snake          = new Model.Snake();
            Direction snakeDirection = Direction.Right;

            DrawObstacles();

            while (isGameRunning)
            {
                ClearCurrentSnakePartsFromConsole(snake);

                DrawSnake(snake);
                DrawFood();

                snakeDirection = GetDirection(snakeDirection);
                snake.Move(snakeDirection);

                CheckSnakeCollision(snake);

                Thread.Sleep(snake.SnakeSpeedMilliSeconds);
            }
        }