示例#1
0
        static void Main(string[] args)
        {
            Console.SetBufferSize(120, 50);

            /*
             * Инициализация змейки с длиной тела, равной 3 и игрового поля.
             * Начальное положение змейки в центре игрового поля.
             */
            GameBorders gameBorders = new GameBorders(120, 50);

            Models.Snake snake = new Models.Snake(new Point(60, 25),
                                                  Direction.Right, 3
                                                  );

            FoodGenerator gen       = new FoodGenerator(120, 50);
            Point         foodPoint = gen.GenerateFood();

            foodPoint.Draw(gen.symbol);

            /*
             * Обработка нажатий клавиш управления пользователем
             */
            while (true)
            {
                if (snake.IsHitWall(gameBorders.borders) || snake.IsHitTail())
                {
                    break;
                }

                if (snake.Eat(foodPoint))
                {
                    foodPoint = gen.GenerateFood();
                    foodPoint.Draw(gen.symbol);
                }
                else
                {
                    snake.Move();
                }

                Thread.Sleep(100);

                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    snake.ButtonHandler(key.Key);
                }
            }

            WriteGameOver();
            Console.ReadLine();
        }
示例#2
0
        private void CheckForFood()
        {
            Point head = new Point(Canvas.GetLeft(SnakeParts[0]), Canvas.GetTop(SnakeParts[0]));
            Point food = new Point(Canvas.GetLeft(_foodGenerator.Food), Canvas.GetTop(_foodGenerator.Food));

            if (head == food)
            {
                _playField.Children.Remove(_foodGenerator.Food);

                _foodGenerator.GenerateFood();
                GenerateSnakePart();
                SetScore();
            }
        }