static int nextRand = 0; // variable storing the next random number

        #endregion Fields

        #region Methods

        public static void ColisionWithWallsCheck(Element land, Fighter airplane, List<Element> listEl, List<Element> shots, uint score)
        {
            if ((airplane.XCoord + 1 <= land.width.Length || airplane.XCoord + 2 >= Console.WindowWidth - land.width.Length) &&
                (airplane.YCoord + 1 == land.y || airplane.YCoord == land.y || airplane.YCoord + 2 == land.y))
            {
                PlayerDeath(airplane, shots, score);
            }
        }
 private static void Controls(Fighter airplane, List<Element> shots)
 {
     if (Console.KeyAvailable)
     {
         ConsoleKeyInfo pressedKey = Console.ReadKey(true);
         while (Console.KeyAvailable) Console.ReadKey(true); //Escapes holding arrows pressed problem.
         if (pressedKey.Key == ConsoleKey.LeftArrow)
         {
             airplane.MoveLeft();
         }
         else if (pressedKey.Key == ConsoleKey.RightArrow)
         {
             airplane.MoveRight();
         }
         else if (pressedKey.Key == ConsoleKey.Spacebar)
         {
             Shoot(airplane, shots);
         }
     }
 }
 private static void Shoot(Fighter airplane, List<Element> shots)
 {
     Element shot = new Element();
     shot.x = airplane.XCoord + 1;
     shot.y = airplane.YCoord - 1;
     shot.width = "!";
     shot.color = ConsoleColor.White;
     shots.Add(shot);
 }
 private static void PlayerDeath(Fighter airplane, List<Element> shots, uint score)
 {
     try
     {
         airplane.Lives--;
         airplane.XCoord = Console.WindowWidth / 2 - 1;
         Enemies.RemoveAllEnemies();
         Console.Clear();
         shots.Clear();
     }
     catch (ArgumentOutOfRangeException)
     {
         SaveScore(score);
     }
 }
        static void Main()
        {
            DrawIntro(); //Method for drawing the ASCII art at the begining
            Console.BackgroundColor = ConsoleColor.DarkBlue; // set default background color

            //Sound initialization:
            SpeechSynthesizer synth = new SpeechSynthesizer(); // creates new synthesizer
            synth.SetOutputToDefaultAudioDevice(); // Redirect audio to default device

            //Sound demo:
            //synth.Speak("Team Robin Hood Presents: River Raid"); //Just testing audio

            Console.BufferHeight = Console.WindowHeight = ConsoleHeight; // set size of the console screen
            Console.BufferWidth = Console.WindowWidth = ConsoleWidth;
            List<Element> landmass = new List<Element>(); // creates the list containing the "shore"
            List<Element> shots = new List<Element>();
            Fighter airplane = new Fighter(Console.WindowWidth / 2, Console.WindowHeight - 8);
            int counter = 0; // count the steps for randomization purposes
            uint score = 0;
            while (true)
            {
                counter++;
                //                airplane.FuelManage();
                DrawScorefield();
                if (counter == 5)//set one enemy every 10 cycles
                {
                    Enemies.GenerateRandomEnemy(); // generate the enemy cordinates ( a new enemy every cicle)
                }
                Enemies.EnemyMoving();//enemy moving down to the console
                Enemies.EnemyMovingLeftRight(); // enemy moving left, right, with random step
                //!!       //I dont know why my enemies start moving right-left after one or two full cycles(10-20)
                int current = 0;
                //                airplane.MoveFighter();
                List<Element> newList = new List<Element>(); // new list for storing temporary objects
                for (int i = 0; i < landmass.Count; i++)
                {
                    Element oldEl = landmass[i];
                    Element el = new Element();
                    el.y = oldEl.y + 1;
                    el.x = oldEl.x;
                    el.width = oldEl.width;
                    current = el.width.Length;
                    // it doesn't work properly if I let the terrain reach the bottom of the screen
                    //we can use the bottom space for system information - score, lives, fuel etc.
                    if (el.y < ConsoleHeight - 5)
                        newList.Add(el);
                }
                landmass = newList;

                List<Element> tempShots = new List<Element>();
                for (int i = 0; i < shots.Count; i++)
                {
                    Element oldEl = shots[i];
                    Element el = new Element();
                    el.y = oldEl.y - 1;
                    el.x = oldEl.x;
                    el.width = oldEl.width;
                    if (el.y >= 0)
                        tempShots.Add(el);
                }
                shots = tempShots;

                try
                {
                    for (int i = 0; i < shots.Count; i++)
                    {
                        var enemies = Enemies.enemies;
                        for (int j = 0; j < enemies.Count; j++)
                        {
                            if ((shots[i].x >= enemies[j][0]) && (shots[i].x <= enemies[j][0] + (Enemies.enemySymbol.Length - 1))
                                && (shots[i].y == enemies[j][1]))
                            {
                                shots.RemoveAt(i);
                                enemies.RemoveAt(j);
                                score += 10;
                                Console.Beep(); //Play sound when the enemy is hit
                            }
                        }
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                Console.Clear(); // very important
                Element tempLand = new Element();
                tempLand.x = 0;
                tempLand.y = 0;
                tempLand.width = LandGenerator(counter, current);
                Element tempLandRight = tempLand; // mirror the right shore
                tempLandRight.x = ConsoleWidth - tempLandRight.width.Length; // and place it accordingly
                landmass.Add(tempLand);
                landmass.Add(tempLandRight);

                airplane.DrawFighter();

                Controls(airplane, shots);

                foreach (var missile in shots)
                {
                    PrintShots(missile.x, missile.y, missile.width);
                }

                Enemies.DrawEnemies(); //printing the enemy on the  console
                Console.ForegroundColor = ConsoleColor.White; // Set color for player information
                PrintStringAtPos("Lives: ", 2, Console.WindowHeight - 4, airplane.Lives.ToString());
                PrintStringAtPos("Score: ", 2, Console.WindowHeight - 3, score.ToString());

                foreach (var element in landmass)
                {
                    PrintLand(element.x, element.y, element.width);
                    ColisionWithWallsCheck(element, airplane, landmass, shots, score);
                }
                EnemyCollisions(airplane, shots, score);
                DrawScorefield();
                Thread.Sleep(200); // controls game speed - lower is faster
                if (counter == 10) // reset the counter when reaching 10 so it doesn't overflow at some point
                {
                    counter = 1;
                }
            }
        }
 private static void EnemyCollisions(Fighter airplane, List<Element> shots, uint score)
 {
     var enemies = Enemies.enemies;
     var enemyLenght = Enemies.enemySymbol.Length - 1;
     for (int i = 0; i < enemies.Count; i++)
     {
         if (((airplane.XCoord <= enemies[i][0] && airplane.XCoord + 2 >= enemies[i][0])
             ||
             ((airplane.XCoord <= enemies[i][0] + enemyLenght) && (airplane.XCoord + 2 >= enemies[i][0] + enemyLenght)))
             &&
             (airplane.YCoord + 1 == enemies[i][1] || airplane.YCoord == enemies[i][1] ||
              airplane.YCoord + 2 == enemies[i][1]))
         {
             PlayerDeath(airplane, shots, score);
         }
     }
 }