Пример #1
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Attempts to eat an int from the given int. </summary>
        ///
        /// <remarks>   Jonna, 03-Jul-18. </remarks>
        ///
        /// <param name="a">    An int to process. </param>
        /// <param name="b">    An int to process. </param>
        ///
        /// <returns>   True if it succeeds, false if it fails. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        private bool TryEat(int a, int b)
        {
            if (m_Snake.CanEat(a, b, m_Food))
            {
                m_Snake.Eat(m_Food);
                GenerateFood();
                return(true);
            }
            m_Snake.MoveTo(a, b);
            return(false);
        }
Пример #2
0
        static void Main(string[] args)
        {
            Thread t = new Thread(MoveSnakeThread);

            t.Start();

            Snake snake = new Snake();
            Food  food  = new Food();
            Wall  Wall  = new Wall();

            Console.CursorVisible = false;
            Console.SetWindowSize(103, 30);

            while (true)
            {
                snake.Draw();
                food.Draw();
                Wall.Draw();
                Console.SetCursorPosition(0, 1);
                Console.WriteLine("Score :");
                Console.WriteLine(snake.a);


                ConsoleKeyInfo btn = Console.ReadKey();
                switch (btn.Key)
                {
                case ConsoleKey.UpArrow:
                    direction = 4;
                    break;

                case ConsoleKey.DownArrow:
                    direction = 3;
                    break;

                case ConsoleKey.LeftArrow:
                    direction = 2;
                    break;

                case ConsoleKey.RightArrow:
                    direction = 1;
                    break;
                }


                if (snake.body[0].x < 14)
                {
                    snake.body[0].x = 96;
                }
                if (snake.body[0].x > 96)
                {
                    snake.body[0].x = 14;
                }
                if (snake.body[0].y < 7)
                {
                    snake.body[0].y = 21;
                }
                if (snake.body[0].y > 21)
                {
                    snake.body[0].y = 7;
                }

                if (snake.GameOver(Wall))
                {
                    snake.a = 0;

                    Console.Clear();

                    snake.body = new List <Point>();
                    snake.body.Add(new Point(16, 7));
                    snake.body.Add(new Point(15, 7));
                    snake.body.Add(new Point(14, 7));
                }
                if (snake.CanEat(food))
                {
                    food.setRandomPosition();
                }
            }
        }