static void Main() { #region InitializeConsoleWindowSize Console.SetWindowSize(Window_Width, Window_Height); Console.BufferHeight = Console.WindowHeight; Console.BufferWidth = Console.WindowWidth; #endregion #region InitializeGameElements int initialRowOfSnake = Window_Height / 2; int initialColoumOfSnake = Window_Width / 2; Direction currentDirection = initialDirection; int curentSnakeSpeed = initialSnakeSpeed; Queue<Position> snake = new Queue<Position>(); for (int i = 0; i < initialSnakeLength; i++) { snake.Enqueue(new Position(initialColoumOfSnake + i, initialRowOfSnake)); } Position snakeFood = GenerateFood(); DrawSingleElement(snakeFood, foodSymbol); DrawBorderElement(); Position snakeHead; Position snakeTail; Position headingOfSnake; Position newSnakeHead; #endregion #region Directions Position[] directions = new Position[] { new Position(-1, -1), //NorthWest new Position(0, -1), // North new Position(1, -1), //NorthtEast new Position(-1, 0), //West new Position(1, 0), //East new Position(-1, 1), //SouthWest new Position(0, 1), //South new Position(1, 1) //SouthEast }; #endregion while (true) { if (Console.KeyAvailable) { currentDirection = ReadKeyFromKeyboard(currentDirection); DrawSingleElement(new Position(1 + curentLevel.Length, 1), spaceSymbol); } snakeHead = snake.Last(); headingOfSnake = directions[(int)currentDirection]; newSnakeHead = new Position( snakeHead.Column + headingOfSnake.Column, snakeHead.Row + headingOfSnake.Row ); #region GameOverLogic if (newSnakeHead.Column < 1 || newSnakeHead.Row < 1 || newSnakeHead.Column >= Console.WindowWidth - 1 || newSnakeHead.Row >= Console.WindowHeight - 1 || snake.Contains(newSnakeHead)) { Console.Clear(); DrawSingleElement(new Position(initialColoumOfSnake - 3, initialRowOfSnake - 3), "Game Over!"); DrawSingleElement(new Position(initialColoumOfSnake - 5, initialRowOfSnake - 1), String.Format("Level reached: {0}", gameLevel)); DrawSingleElement(new Position(initialColoumOfSnake - 7, initialRowOfSnake), "(Press Esc to exit)"); ConsoleKeyInfo exitKey = Console.ReadKey(); while (exitKey.Key != ConsoleKey.Escape) { exitKey = Console.ReadKey(); } return; } #endregion #region SnakeGrowingLogic if (newSnakeHead == snakeFood) { snakeFood = GenerateFood(); DrawSingleElement(snakeFood, foodSymbol); if (curentSnakeSpeed > maxSnakeSpeed) { curentSnakeSpeed -= speedFactor; gameLevel++; } } else { snakeTail = snake.Dequeue(); DrawSingleElement(snakeTail, spaceSymbol); } snake.Enqueue(newSnakeHead); #endregion DrawSnakeElement(snake); curentLevel = String.Format("Level:{0}", gameLevel); DrawSingleElement(new Position(1, 1), curentLevel); Thread.Sleep(curentSnakeSpeed); } }
static void Main(string[] args) { byte right = 0; byte left = 1; byte down = 2; byte up = 3; //Environment.TickCount tova 6te broi vremeto ot kakto e pusnata programata //DateTime.Now int lastFoodTime = 0; int foodDisappearTime = 8000; // 8 sec delay lastFoodTime = Environment.TickCount; Position[] directions = new Position[]{ new Position(0,1), //right new Position(0,-1),//left new Position(1,0), // down new Position(-1,0), // up }; int sleepTime = 100; int direction = right; // 0 teku6tat posoka // saotvetno kat elementi na masiva positoin Random randomNumberGenerator = new Random(); Console.BufferHeight = Console.WindowHeight; // consolata da nqma scrollbar i buffera da bude golqm kolkoto consolata Position food = new Position(randomNumberGenerator.Next(0, Console.WindowHeight), // slaga qbalkata na random mesto randomNumberGenerator.Next(0, Console.WindowWidth)); // tuka 6te ni se generira slu4aino 4islo koeto 6te opredelq kade e qbalkata Queue<Position> snakeElements = new Queue<Position>(); // tova 6te e opa6kata TUKA SE PRAVQT ZMIQTA Position position1 = new Position(); for (int i = 0; i <= 5; i++) { snakeElements.Enqueue(new Position(0, i)); } foreach (Position position in snakeElements) // obxojda vsi4kite elementi i postavqme kursora tam kadeto se namira zmiqta { Console.SetCursorPosition(position.col, position.row); // tazi poziciq priema left top kolona Console.WriteLine('*'); } while (true) { if (Console.KeyAvailable) // proverqva dali potrebitelq e natisnal nqkakvo kop4e { ConsoleKeyInfo userInput = Console.ReadKey(); // pro4ita ne6to tam if (userInput.Key == ConsoleKey.LeftArrow) { if (direction != right) // ako posokata ne e nadqsno 4ak togava da se mesti nalqvo { direction = left; } } if (userInput.Key == ConsoleKey.RightArrow) { if (direction != left) { direction = right; } } if (userInput.Key == ConsoleKey.UpArrow) { if (direction != down) { direction = up; } } if (userInput.Key == ConsoleKey.DownArrow) { if (direction != up) // ako posokata ne e nagore 4ak togava da se mestim nadolo { direction = down; } } } Position snakeHead = snakeElements.Last(); // tozi method vru6ta posledniq element ot opa6kata Position nextDirection = directions[direction]; // s tozi red vzimame na kade da se dviji zmiqta kato directions e masiva a direction 0,1,2,3 //novata poziciq na zmiqta Position snakeNewHead = new Position(snakeHead.row + nextDirection.row, snakeHead.col + nextDirection.col);// purvo e X posle e Y kato za primer vzimame stariq direction na row i go //Position newHead = snakeElements.Last();// tova ni vru6ta glavata na na6ta zmiq i q zapisva v promenlivata new head snakeElements.Enqueue(snakeNewHead); // tozi kod slaga nova glava vseki put na novata poziciq // tova proverqva dali glavata na zmiqta ne e izlezla ot ekrana bukvalno if (snakeNewHead.row < 0 || snakeNewHead.col < 0 || snakeNewHead.row >= Console.WindowHeight || snakeNewHead.col >= Console.WindowWidth) // row = red { Console.SetCursorPosition(0, 0); Console.WriteLine(); Console.WriteLine("Game over"); Console.WriteLine("Your points are: {0}", (snakeElements.Count - 6) * 100); // tova vru6ta broq elementi na zmiqta return; // prikliu4va izpulnenieto na teku6tiq method } //TOVA E KODA AKO ZMIQTA APNE QBALKATA if (snakeNewHead.col == food.col && snakeNewHead.row == food.row) // tuka slagame statement koito proverqva dali glavata na zmiqta se zasi4a s tazi na xranata { do { food = new Position(randomNumberGenerator.Next(0, Console.WindowHeight), // slaga qbalkata na random mesto, no tozi put prosto vzima novi stoinosti za novata qbalka randomNumberGenerator.Next(0, Console.WindowWidth)); // tuka 6te ni se generira slu4aino 4islo koeto 6te opredelq kade e qbalkata } while (snakeElements.Contains(food)); sleepTime -= 5; // da stava s 20 milisekundi po-burza Random r = new Random(); Console.Beep(r.Next(100, 10000), r.Next(60, 500)); } else // samo kogato zmiqta ne udrq lqbalkata 6te se maxa edna zvezda ot opajkata { snakeElements.Dequeue(); } // garanciqta 4e 6te imame nova qbalka i zmiq e 4e console clear e predi tqx a ne sled tqx Console.Clear(); // 4isti konzolata foreach (Position position in snakeElements) // obxojda vsi4kite elementi i postavqme kursora tam kadeto se namira zmiqta { Console.SetCursorPosition(position.col, position.row); // tazi poziciq priema left top kolona Console.WriteLine('*'); } lastFoodTime = Environment.TickCount; // broq na milisekundi ot na4aloto na sistemata // TUka e koda za kazvane na programata da 4aka 8 secundi if (Environment.TickCount - lastFoodTime >= foodDisappearTime) { Console.SetCursorPosition(food.col, food.row); // TAKA MAXAME STARATA QBALKA Console.WriteLine(" "); do { food = new Position(randomNumberGenerator.Next(0, Console.WindowHeight), // slaga qbalkata na random mesto, no tozi put prosto vzima novi stoinosti za novata qbalka randomNumberGenerator.Next(0, Console.WindowWidth)); // tuka 6te ni se generira slu4aino 4islo koeto 6te opredelq kade e qbalkata } while (snakeElements.Contains(food)); } Console.SetCursorPosition(food.col, food.row); // S TOZI KOD RISUVAME QBALKATA NA RANDOM MESTO V KONZOLATA Console.Write('@'); Thread.Sleep(sleepTime); // konzolata spira prosto bavi zmiqta s opredeleno vreme } }
static void Main(string[] args) { Position[] directions = new Position[]{ new Position(0,1), //right new Position(0,-1),//left new Position(1,0), // down new Position(-1,0), // up }; int direction = 0; // 0 teku6tat posoka // saotvetno kat elementi na masiva positoin Random randomNumberGenerator = new Random(); Console.BufferHeight = Console.WindowHeight; // consolata da nqma scrollbar i buffera da bude golqm kolkoto consolata Position food = new Position(randomNumberGenerator.Next(0, Console.WindowHeight), // slaga qbalkata na random mesto randomNumberGenerator.Next(0, Console.WindowWidth)); // tuka 6te ni se generira slu4aino 4islo koeto 6te opredelq kade e qbalkata Queue<Position> snakeElements = new Queue<Position>(); // tova 6te e opa6kata TUKA SE PRAVQT ZMIQTA I QDENETO Position position1 = new Position(); for (int i = 0; i <= 5; i++) { snakeElements.Enqueue(new Position(0, i)); } foreach (Position position in snakeElements) // obxojda vsi4kite elementi i postavqme kursora tam kadeto se namira zmiqta { Console.SetCursorPosition(position.col, position.row); // tazi poziciq priema left top kolona Console.WriteLine('*'); } while (true) { if (Console.KeyAvailable) // proverqva dali potrebitelq e natisnal nqkakvo kop4e { ConsoleKeyInfo userInput = Console.ReadKey(); // pro4ita ne6to tam if (userInput.Key == ConsoleKey.LeftArrow) { direction = 1; } if (userInput.Key == ConsoleKey.RightArrow) { direction = 0; } if (userInput.Key == ConsoleKey.UpArrow) { direction = 3; } if (userInput.Key == ConsoleKey.DownArrow) { direction = 2; } } Position snakeHead = snakeElements.Last(); // tozi method vru6ta posledniq element ot opa6kata Position nextDirection = directions[direction]; // s tozi red vzimame na kade da se dviji zmiqta kato directions e masiva a direction 0,1,2,3 //novata poziciq na zmiqta Position snakeNewHead = new Position(snakeHead.row + nextDirection.row, snakeHead.col + nextDirection.col);// purvo e X posle e Y kato za primer vzimame stariq direction na row i go //Position newHead = snakeElements.Last();// tova ni vru6ta glavata na na6ta zmiq i q zapisva v promenlivata new head snakeElements.Enqueue(snakeNewHead); // tozi kod slaga nova glava vseki put na novata poziciq // tova proverqva dali glavata na zmiqta ne e izlezla ot ekrana bukvalno if (snakeNewHead.row < 0 || snakeNewHead.col < 0 || snakeNewHead.row >= Console.WindowHeight || snakeNewHead.col >= Console.WindowWidth) // row = red { Console.SetCursorPosition(0, 0); Console.WriteLine(); Console.WriteLine("Game over"); Console.WriteLine("Your points are: {0}", (snakeElements.Count - 6) * 100); // tova vru6ta broq elementi na zmiqta return; // prikliu4va izpulnenieto na teku6tiq method } //TOVA E KODA AKO ZMIQTA APNE QBALKATA if (snakeNewHead.col == food.col && snakeNewHead.row == food.row) // tuka slagame statement koito proverqva dali glavata na zmiqta se zasi4a s tazi na xranata { food = new Position(randomNumberGenerator.Next(0, Console.WindowHeight), // slaga qbalkata na random mesto, no tozi put prosto vzima novi stoinosti za novata qbalka randomNumberGenerator.Next(0, Console.WindowWidth)); // tuka 6te ni se generira slu4aino 4islo koeto 6te opredelq kade e qbalkata } else // samo kogato zmiqta ne udrq lqbalkata 6te se maxa edna zvezda ot opajkata { snakeElements.Dequeue(); } // garanciqta 4e 6te imame nova qbalka i zmiq e 4e console clear e predi tqx a ne sled tqx Console.Clear(); // 4isti konzolata foreach (Position position in snakeElements) // obxojda vsi4kite elementi i postavqme kursora tam kadeto se namira zmiqta { Console.SetCursorPosition(position.col, position.row); // tazi poziciq priema left top kolona Console.WriteLine('*'); } Console.SetCursorPosition(food.col, food.row); // S TOZI KOD RISUVAME QBALKATA NA RANDOM MESTO V KONZOLATA Console.Write('@'); Thread.Sleep(100); // konzolata spira prosto bavi zmiqta s opredeleno vreme } }
static void Main() { byte right = 0; byte left = 1; byte down = 2; byte up = 3; Position[] directions = new Position[] { new Position(0, 1), //right new Position(0, -1), //left new Position(1, 0), //down new Position(-1, 0) //up }; int sleepTime = 100; int direction = right; Random randomNumbersGenerator = new Random(); Console.BufferHeight = Console.WindowHeight; Position food = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight), randomNumbersGenerator.Next(0, Console.WindowWidth)); Queue<Position> snakeElements = new Queue<Position>(); /* Position position1 = new Position(0,0); position1.X = 0; position1.Y = 0; */ for (int i = 0; i < 7; i++) { snakeElements.Enqueue(new Position(0, i)); } foreach (Position position in snakeElements) { Console.SetCursorPosition(position.col, position.row); Console.Write("*"); } while (true) { if (Console.KeyAvailable) { ConsoleKeyInfo userInput = Console.ReadKey(); if (userInput.Key == ConsoleKey.LeftArrow) { direction = left; } if (userInput.Key == ConsoleKey.RightArrow) { direction = right; } if (userInput.Key == ConsoleKey.UpArrow) { direction = up; } if (userInput.Key == ConsoleKey.DownArrow) { direction = down; } } Position snakeHead = snakeElements.Last(); Position nextDirection = directions[direction]; Position snakeNewHead = new Position(snakeHead.row + nextDirection.row, snakeHead.col + nextDirection.col); if (snakeNewHead.row < 0 || snakeNewHead.col < 0 || snakeNewHead.row >= Console.WindowHeight || snakeNewHead.col >= Console.WindowWidth) { Console.SetCursorPosition(0, 0); Console.WriteLine("Game over!"); } snakeElements.Enqueue(snakeNewHead); if ((snakeNewHead.col == food.col) && (snakeNewHead.row == food.row)) { //feeding food = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight), randomNumbersGenerator.Next(0, Console.WindowWidth)); } else { //moving... snakeElements.Dequeue(); } Console.Clear(); foreach (Position position in snakeElements) { Console.SetCursorPosition(position.col, position.row); Console.Write("*"); } Console.SetCursorPosition(food.row, food.col); Console.Write("@"); Thread.Sleep(sleepTime); } }
static void Main() { byte right = 0, left = 1, down = 2, up = 3; int lastFoodTime = 0; int FoodDissapearTime = 8000; double NegativePoints = 0; Position[] directions = new Position[] { new Position(0, 1), // right new Position(0, -1), // left new Position(1, 0), // down new Position(-1, 0), // up }; double sleeptime = 100; int direction = 0; Random randomNumbersGenerator = new Random(); Console.BufferHeight = Console.WindowHeight; Position food = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight), randomNumbersGenerator.Next(0, Console.WindowWidth)); List<Position> Obstacles = new List<Position>() { new Position(12,12), new Position(13,12), new Position(14,12), new Position(15,12), new Position(16,12), }; foreach (Position obstacle in Obstacles) { Console.ForegroundColor = ConsoleColor.Cyan; Console.SetCursorPosition(obstacle.col, obstacle.row); Console.Write("|"); } Queue<Position> snakeElements = new Queue<Position>(); for (int i = 0; i <= 5; i++) { snakeElements.Enqueue(new Position(0, i)); } foreach (Position position in snakeElements) { Console.SetCursorPosition(position.col, position.row); Console.ForegroundColor = ConsoleColor.White; Console.Write("*"); } do { food = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight), randomNumbersGenerator.Next(0, Console.WindowWidth)); lastFoodTime = Environment.TickCount; Console.SetCursorPosition(food.col, food.row); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("@"); } while (snakeElements.Contains(food) || Obstacles.Contains(food)); while (true) { if (Console.KeyAvailable) { ConsoleKeyInfo userInput = Console.ReadKey(); while (Console.KeyAvailable) { userInput = Console.ReadKey(true); } if (userInput.Key == ConsoleKey.LeftArrow && direction != right) { direction = left; } if (userInput.Key == ConsoleKey.RightArrow && direction != left) { direction = right; } if (userInput.Key == ConsoleKey.UpArrow && direction != down) { direction = up; } if (userInput.Key == ConsoleKey.DownArrow && direction != up) { direction = down; } } Position snakeHead = snakeElements.Last(); Position nextDirection = directions[direction]; Position snakeNewHead = new Position(snakeHead.row + nextDirection.row, snakeHead.col + nextDirection.col); if (snakeNewHead.col < 0) snakeNewHead.col = Console.WindowWidth - 1; if (snakeNewHead.row < 0) snakeNewHead.row = Console.WindowHeight - 1; if (snakeNewHead.col >= Console.WindowWidth) snakeNewHead.col = 0; if (snakeNewHead.row >= Console.WindowHeight) snakeNewHead.row = 0; if (snakeElements.Contains(snakeNewHead) || Obstacles.Contains(snakeNewHead)) { Console.SetCursorPosition(0, 0); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Game over!"); int userpoints = (snakeElements.Count - 6) * 100 - (int)NegativePoints; userpoints = Math.Max(userpoints, 0); Console.WriteLine("Your points are: {0}", userpoints); Console.ReadLine(); return; } snakeElements.Enqueue(snakeNewHead); Console.SetCursorPosition(snakeNewHead.col, snakeNewHead.row); Console.ForegroundColor = ConsoleColor.Green; if (direction == left) { Console.Write("<"); } if (direction == right) { Console.Write(">"); } if (direction == down) { Console.Write("v"); } if (direction == up) { Console.Write("^"); } Console.SetCursorPosition(snakeHead.col, snakeHead.row); Console.ForegroundColor = ConsoleColor.Gray; Console.Write("*"); if (snakeNewHead.col == food.col && snakeNewHead.row == food.row) { // feeding do { food = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight), randomNumbersGenerator.Next(0, Console.WindowWidth)); } while (snakeElements.Contains(food) || Obstacles.Contains(food)); lastFoodTime = Environment.TickCount; Console.SetCursorPosition(food.col, food.row); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("@"); Position obstacle; do { obstacle = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight), randomNumbersGenerator.Next(0, Console.WindowWidth)); } while (snakeElements.Contains(obstacle) || Obstacles.Contains(obstacle) || (food.row != obstacle.row && food.col != obstacle.col)); Obstacles.Add(obstacle); Console.SetCursorPosition(obstacle.col, obstacle.row); Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("="); } else { //moving Position last = snakeElements.Dequeue(); Console.SetCursorPosition(last.col, last.row); Console.Write(" "); } if (Environment.TickCount - lastFoodTime >= FoodDissapearTime) { NegativePoints = NegativePoints + 50; Console.SetCursorPosition(food.col, food.row); Console.Write(" "); do { food = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight), randomNumbersGenerator.Next(0, Console.WindowWidth)); } while (snakeElements.Contains(food)); lastFoodTime = Environment.TickCount; Console.SetCursorPosition(food.col, food.row); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("@"); } Console.SetCursorPosition(food.col, food.row); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("@"); sleeptime -= 0.01; NegativePoints += 0.2; Thread.Sleep((int)sleeptime); } }
static void Main(string[] args) { Position[] directions = new Position[] { new Position (0, 1),//right new Position (0, -1), // left new Position (1, 0), // down new Position (-1, 0),// top }; int direction = 0; //0 Queue<Position> snakeElement = new Queue<Position>(); for (int i = 0; i <= 5; i++) { snakeElement.Enqueue(new Position(0, i)); } foreach (Position position in snakeElement) { Console.SetCursorPosition(position.Col, position.Row); Console.Write("*"); } while (true) { ConsoleKeyInfo userinput = Console.ReadKey(); if (userinput.Key == ConsoleKey.LeftArrow) { direction = 1; } if (userinput.Key == ConsoleKey.RightArrow) { direction = 0; } if (userinput.Key == ConsoleKey.UpArrow) { direction = 3; } if (userinput.Key == ConsoleKey.DownArrow) { direction = 2; } snakeElement.Dequeue(); Position snakeHead = snakeElement.Last(); snakeElement.Dequeue(); Position nextdirection = directions[direction]; Position smakenewhead = new Position(snakeHead.Row + nextdirection.Row, snakeHead.Col + nextdirection.Col); snakeElement.Enqueue(smakenewhead); foreach (Position position in snakeElement) { Console.SetCursorPosition(position.Col, position.Row); Console.Write("*"); } } }
static void Main(string[] args) { byte right = 0; byte left = 1; byte down = 2; byte up = 3; Position[] directions = new Position[] { new Position(0, 1), //right new Position(0, -1), //left new Position(1, 0), // down new Position(-1, 0), //up }; int sleepTime = 100; int direction = right; //0 Random rand = new Random(); Console.BufferHeight = Console.WindowHeight; Console.BufferWidth = Console.WindowWidth; Position food = new Position(rand.Next(0,Console.WindowHeight), rand.Next(0, Console.WindowWidth)); Console.SetCursorPosition(food.col, food.row); Console.Write("$"); Queue<Position> snakeElements = new Queue<Position>(); for (int i=0; i<=5; i++) { snakeElements.Enqueue(new Position(0, i)); } foreach (Position position in snakeElements) { Console.SetCursorPosition(position.col, position.row); Console.WriteLine("*"); } while (true) { if (Console.KeyAvailable) { ConsoleKeyInfo userInput = Console.ReadKey(); if (userInput.Key == ConsoleKey.LeftArrow) { if (direction != right) direction = left; } if (userInput.Key == ConsoleKey.RightArrow) { if (direction !=left) direction = right; } if (userInput.Key == ConsoleKey.UpArrow) { if (direction!=down) direction = up; } if (userInput.Key == ConsoleKey.DownArrow) { if (direction!=up) direction = down; } } Position snakeHead = snakeElements.Last(); Position nextDirection = directions[direction]; Position snakeNewHead = new Position(snakeHead.row + nextDirection.row, snakeHead.col + nextDirection.col); if (snakeNewHead.row < 0 || snakeNewHead.col < 0 || snakeNewHead.row >= Console.WindowHeight || snakeNewHead.col >= Console.WindowWidth || snakeElements.Contains(snakeNewHead)) { Console.SetCursorPosition(0, 0); Console.BackgroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Game Over!"); Console.WriteLine("Your points are {0}", (snakeElements.Count-6) * 100); Console.ReadKey(); return; } snakeElements.Enqueue(snakeNewHead); if (snakeNewHead.col == food.col && snakeNewHead.row == food.row) { food = new Position(rand.Next(0, Console.WindowHeight), rand.Next(0, Console.WindowWidth)); sleepTime--; } else { snakeElements.Dequeue(); } Console.Clear(); foreach (Position position in snakeElements) { Console.SetCursorPosition(position.col, position.row); Console.WriteLine("*"); } Console.SetCursorPosition(food.col, food.row); Console.Write("$"); Thread.Sleep(sleepTime); } }
static void Main(string[] args) { String[] adresses = System.IO.File.ReadAllLines("ip.txt"); // read ip's from file String[] ip = new String[adresses.Length]; String[] ports = new String[adresses.Length]; Threads = new Queue<Thread>(); // Проходим по адресам for (int i = 0; i < adresses.Length - 1; i++) { String[] result = adresses[i].Split(new Char[] { ':' }); ip[i] = result[0].ToString(); ports[i] = result[1].ToString(); } int q = 0; while (!res) { if (q < adresses.Length - 1) { string currip = ip[q], currrport = ports[q]; // Create but do not start it. Thread current = new Thread(new ThreadStart(delegate() { try { var g = new GoogleScholarDataGetter("https://scholar.google.com.ua/citations?user=b18TCTYAAAAJ&hl=uk", ref currip, ref currrport); // Вызываем парсер g.executeParser(); } catch (Exception e) { Console.WriteLine(e.Message); } return; })); current.Name = "Worker " + q.ToString(); current.IsBackground = true; current.Priority = ThreadPriority.Highest; Threads.Enqueue(current); Threads.Last().Start(); q++; } } }
static void Main(string[] args) { byte right = 0; //massive index byte left = 1; byte down = 2; byte up = 3; Position[] directions = new Position[] { new Position(0, 1), // right new Position(0, -1), // left new Position(1, 0), // down new Position(-1, 0) // up }; int sleepTime = 100; int direction = right; Random randomNumberGenerator = new Random(); Console.BufferHeight = Console.WindowHeight; Position food = new Position(randomNumberGenerator.Next(0, Console.WindowHeight), //food randomNumberGenerator.Next(0, Console.WindowWidth)); Queue<Position> snakeElements = new Queue<Position>(); for (int i = 0; i <= 5; i++) { snakeElements.Enqueue(new Position(0, i)); } foreach (Position position in snakeElements) { Console.SetCursorPosition(position.cow, position.row); Console.Write("*"); } while (true) { Position snakeHead = snakeElements.Last(); Position nextDirection = directions[direction]; Position snakeNewHead = new Position(snakeHead.row + nextDirection.row, snakeHead.cow + nextDirection.cow); if (snakeNewHead.row < 0 || snakeNewHead.cow < 0 || snakeNewHead.row >= Console.WindowHeight || snakeNewHead.cow >= Console.WindowWidth || snakeElements.Contains(snakeNewHead)) { Console.SetCursorPosition(0, 0); Console.BackgroundColor = ConsoleColor.DarkRed; Console.Beep(); Console.Beep(); Console.Beep(); Console.WriteLine("Game Over !!!"); Console.WriteLine("Your points are: {0}", snakeElements.Count - 6); return; } if (snakeNewHead.cow == food.cow && snakeNewHead.row == food.row) { // feeding the snake food = new Position(randomNumberGenerator.Next(0, Console.WindowHeight), randomNumberGenerator.Next(0, Console.WindowWidth)); Console.Beep(); sleepTime--; // speed up } else { // moving... snakeElements.Dequeue(); } if (Console.KeyAvailable) { ConsoleKeyInfo userInput = Console.ReadKey(); if (userInput.Key == ConsoleKey.LeftArrow) { if (direction != right) direction = left; } if (userInput.Key == ConsoleKey.RightArrow) { if (direction != left) direction = right; } if (userInput.Key == ConsoleKey.UpArrow) { if (direction != down) direction = up; } if (userInput.Key == ConsoleKey.DownArrow) { if(direction != up) direction = down; } } snakeElements.Enqueue(snakeNewHead); Console.Clear(); foreach (Position position in snakeElements) { Console.SetCursorPosition(position.cow, position.row); Console.Write("*"); } Console.SetCursorPosition(food.cow, food.row); Console.Write("@"); Thread.Sleep(sleepTime); } }