Beispiel #1
0
        public Game()
        {
            gameObjects = new List <GameObject>();
            snakes      = new List <Snake>();
            List <Point> snakeBody1 = new List <Point>
            {
                new Point(20, 10),
                new Point(20, 11),
                new Point(20, 12)
            };
            List <Point> snakeBody2 = new List <Point>
            {
                new Point(30, 12),
                new Point(30, 13),
                new Point(30, 14)
            };

            snake1 = new Snake(snakeBody1, '*', ConsoleColor.Red);
            snake2 = new Snake(snakeBody2, '*', ConsoleColor.Blue);
            food   = new Food('o', ConsoleColor.Green);
            wall   = new Wall('#', ConsoleColor.DarkYellow);
            while (food.Point.IsCollide(snake1.Points) || food.Point.IsCollide(wall.Points) || food.Point.IsCollide(snake2.Points))
            {
                food.Generate();
            }

            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();
                    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();
                }
            }

            gameObjects.Add(snake1);
            gameObjects.Add(snake2);
            gameObjects.Add(food);
            gameObjects.Add(wall);

            snakes.Add(snake1);
            snakes.Add(snake2);

            score1    = 0;
            score2    = 0;
            Alive     = true;
            CloseGame = false;
        }
Beispiel #2
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();
            }
        }