Exemplo n.º 1
0
 static void Move(object obj)
 {
     if (walls.IsHit(snake.GetHead()) || snake.IsHit(snake.GetHead()))
     {
         time.Change(0, Timeout.Infinite);
     }
     else if (snake.Eat(food.food))
     {
         food.CreateFood();
     }
     else
     {
         snake.Move();
     }
 }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.SetBufferSize(80, 25);
            //рисуем границы
            Walls walls = new Walls(80, 25);

            walls.Draw();

            Point pStart = new Point(4, 5, 'O');
            Snake snake  = new Snake(pStart, 4, Directions.RIGHT);

            snake.Draw();

            FoodCreator foodCreator = new FoodCreator(80, 25, '$');
            Point       food        = foodCreator.CreateFood();

            food.Draw();


            while (true)
            {
                if (walls.IsHit(snake) || snake.IsHitTail())
                {
                    break;
                }
                if (snake.Eat(food))
                {
                    food = foodCreator.CreateFood();
                    food.Draw();
                }
                else
                {
                    snake.Move();
                }

                Thread.Sleep(100);

                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    snake.HandleKey(key.Key);
                }
            }
            WriteGameOver();
            Console.ReadLine();
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.SetBufferSize(80, 25);

            //отрисовка рамки
            HorizontalLine upline = new HorizontalLine(0, 78, 0, '+');
            upline.Drow();
            HorizontalLine downline = new HorizontalLine(0, 78, 24, '+');
            downline.Drow();
            VerticalLine leftline = new VerticalLine(0, 0, 24, '+');
            leftline.Drow();
            VerticalLine rigthline = new VerticalLine(78, 0, 24, '+');
            rigthline.Drow();

            //отрисовка точек
            Point p1 = new Point(7, 18, '*');
            Snake snake = new Snake(p1, 4, Direction.RIGTH);
            snake.Drow();

            //генерация еды
            FoodCreator foodCreator = new FoodCreator(80, 25, '%');
            Point food = foodCreator.CreateFood();
            food.Draw();

            //движение змейки
            while (true)
            {
                if (snake.Eat(food))
                {
                    food = foodCreator.CreateFood();
                    food.Draw();
                }

                else
                {
                    snake.Move();
                }
                Thread.Sleep(120);
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    snake.Handle(key.Key);
                }
            }          
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            Console.SetBufferSize(81, 41);
            Console.CursorVisible = false;
            Console.WindowWidth   = Console.BufferWidth;
            Console.WindowHeight  = Console.BufferHeight;

            Wall w = new Wall(Console.BufferWidth, Console.BufferHeight);

            Snake snake = new Snake(new Point(4, 2, '*'), 4, Direction.RIGHT);

            Food food = new Food(Console.BufferWidth, Console.BufferHeight, '$', snake);

            food.Create();

            new Point(40, 2, '%').Draw();

            while (snake.Collide(w.pList))
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    snake.Controle(key.Key);
                }
                Thread.Sleep(100);
                if (snake.Eat(food))
                {
                    food.Create();
                }
                else
                {
                    snake.Move();
                }
            }
            //Console.SetCursorPosition(65, 2);
            //Console.ReadLine();
        }
Exemplo n.º 5
0
        public void MoveSnake()
        {
            while (!CloseGame)
            {
                if (!Alive)
                {
                    ShowMenu();
                    Thread.Sleep(1000);
                    continue;
                }
                snake1.Move();
                snake2.Move();

                if (food.Point.IsCollide(snake1.Points))
                {
                    snake1.Points.Add(new Point(0, 0));
                    score1++;
                    //Thread.Sleep(n/2);
                    //Accelerate(n/2);
                }
                if (food.Point.IsCollide(snake2.Points))
                {
                    snake2.Points.Add(new Point(0, 0));
                    score2++;
                    //Accelerate(n/2);
                    //Thread.Sleep(n/2);
                }
                //if (food.Point.IsCollide(snake2.Points) || food.Point.IsCollide(snake1.Points))
                //        Accelerate(256);

                if (snake1.Points.Count % 7 == 0 || snake2.Points.Count % 7 == 0)
                {
                    SuperFruit();
                }
                if (food.Point.IsCollide(snake1.Points) || food.Point.IsCollide(wall.Points) || food.Point.IsCollide(snake2.Points))
                {
                    food.Generate();
                    if (snake1.Points.Count % 5 == 0 || snake2.Points.Count % 5 == 0)
                    {
                        wall.NewLevel();
                        speed++;
                        if (Point.IsCollide(snake1.Points, wall.Points) || Point.IsCollide(snake2.Points, wall.Points))
                        {
                            Random random = new Random();
                            int    a2     = random.Next(1, Game.Width);
                            int    b2     = random.Next(1, Game.Height);
                            snake1.SnakeGenerate(a2, b2, snake1.Points.Count);
                            snake2.SnakeGenerate(a2, b2, snake2.Points.Count);
                        }
                        snake1.NoDirection();
                        snake2.NoDirection();
                    }
                }

                for (int i = 0; i < snakes.Count; i++)
                {
                    bool collideWithWall = Point.IsCollide(wall.Points, snakes[i].Points);
                    bool collideToItSelf = snakes[i].Points.Count > 3 && snakes[i].Points[0].IsCollide(snakes[i].Points.GetRange(3, snakes[i].Points.Count - 3));
                    bool collideSnakes   = Point.IsCollide(snake1.Points, snake2.Points);
                    if (collideWithWall || collideToItSelf || collideSnakes)
                    {
                        Alive = false;
                    }
                }
                Draw();
                DrawResult();
                //SnakeAccelerate();

                Accelerate();
            }
        }