public static void Main() { ReadHighScore(); StartMenu(); Console.Clear(); PrintBorder(); byte right = 0; byte left = 1; byte down = 2; byte up = 3; int lastFoodTime = 0; int foodDissapearTime = 8000; Console.WindowHeight = GameHeight + 1; Console.BufferHeight = GameHeight + 1; Console.WindowWidth = GameWidth + 20; Console.BufferWidth = GameWidth + 20; 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 = right; Random randomNumbersGenerator = new Random(); lastFoodTime = Environment.TickCount; List<Position> obstacles = new List<Position>(); Queue<Position> snakeElements = new Queue<Position>(); for (int i = 1; i <= 5; i++) { snakeElements.Enqueue(new Position(1, i)); } Position food; food = PrintFood(snakeElements, obstacles, randomNumbersGenerator); foreach (Position position in snakeElements) { Console.SetCursorPosition( position.col, position.row); Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write("*"); } GameRunning(direction,right,left,up,down,obstacles,snakeElements,lastFoodTime,foodDissapearTime,randomNumbersGenerator,food,directions,nickname); }
static void Main(string[] args) { // List with snake elements Queue<Position> snakeElements = new Queue<Position>(); // Random generator for snake food Random randomGenerator = new Random(); // The snake speed double sleepTime = 100; // Available move directions Position[] moveDirections = new Position[] { new Position(1, 0), // Right new Position(0, 1), // Down new Position(-1, 0), // Left new Position(0, -1), // Up }; // Current direction int currentDirection = 0; // 0 = right, 1 = down, 2 = left, 3 = up // Console settings Console.CursorVisible = false; Console.BufferHeight = Console.WindowHeight; // First snake and screen initialize Position food = new Position( randomGenerator.Next(1, Console.WindowWidth - 1), randomGenerator.Next(1, Console.WindowHeight - 1)); for (int i = 0; i <= 6; i++) { snakeElements.Enqueue(new Position(i, 0)); } foreach (var item in snakeElements) { Console.SetCursorPosition(item.X, item.Y); Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write("*"); } Position snakeHead = snakeElements.Last(); Console.SetCursorPosition(snakeHead.X, snakeHead.Y); Console.ForegroundColor = ConsoleColor.Gray; Console.Write("@"); while (true) { // Read user key if (Console.KeyAvailable) { ConsoleKeyInfo pressedKey = Console.ReadKey(); if (pressedKey.Key == ConsoleKey.RightArrow) if (currentDirection != 2) currentDirection = 0; if (pressedKey.Key == ConsoleKey.DownArrow) if (currentDirection != 3) currentDirection = 1; if (pressedKey.Key == ConsoleKey.LeftArrow) if (currentDirection != 0) currentDirection = 2; if (pressedKey.Key == ConsoleKey.UpArrow) if (currentDirection != 1) currentDirection = 3; } // Write snake food Console.SetCursorPosition(food.X, food.Y); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("+"); // Previous snake head Position prevSnakeHead = snakeElements.Last(); // New snake head Position newSnakeHead = new Position( prevSnakeHead.X + moveDirections[currentDirection].X, prevSnakeHead.Y + moveDirections[currentDirection].Y); // Check constraints if (newSnakeHead.X >= Console.WindowWidth || newSnakeHead.X < 0 || newSnakeHead.Y >= Console.WindowHeight || newSnakeHead.Y < 0 || snakeElements.Contains(newSnakeHead)) { // Game is over Console.SetCursorPosition(0, 0); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("Game over!!! Your points: {0}", snakeElements.Count); Console.WriteLine("Press enter to continue..."); Console.ReadLine(); return; } // Write new snake head Console.SetCursorPosition(prevSnakeHead.X, prevSnakeHead.Y); Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write("*"); // Add new snake element and draw it on the console snakeElements.Enqueue(newSnakeHead); Console.SetCursorPosition(newSnakeHead.X, newSnakeHead.Y); Console.ForegroundColor = ConsoleColor.Gray; Console.Write("@"); // Check if the snake is on food if (newSnakeHead.X == food.X && newSnakeHead.Y == food.Y) { // Feed the snake (the snake is eating) food = new Position( randomGenerator.Next(1, Console.WindowWidth - 1), randomGenerator.Next(1, Console.WindowHeight - 1)); Console.SetCursorPosition(food.X, food.Y); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("+"); } else { // Remove last snake element (the snake is moving) Position p = snakeElements.Dequeue(); Console.SetCursorPosition(p.X, p.Y); Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write(" "); } // Slow the motion Thread.Sleep((int)sleepTime); // Change the speed sleepTime -= 0.05; } }
static Position PrintFood(Queue<Position> snakeElements, List<Position> obstacles, Random randomNumbersGenerator) { Position food; do { food = new Position( randomNumbersGenerator.Next(1, GameHeight - 3), randomNumbersGenerator.Next(1, GameWidth - 3)); } while (snakeElements.Contains(food) || obstacles.Contains(food)); Console.SetCursorPosition(food.col, food.row); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("@"); return food; }
static void GameRunning(int direction, byte right, byte left, byte up, byte down , List<Position> obstacles, Queue<Position> snakeElements, int lastFoodTime, int foodDissapearTime ,Random randomNumbersGenerator,Position food,Position[] directions,string nickname) { while (true) { direction = keyAvailable(direction, right, left, up, down); Position snakeHead = snakeElements.Last(); Position nextDirection = directions[direction]; Position snakeNewHead = new Position( snakeHead.row + nextDirection.row, snakeHead.col + nextDirection.col); snakeNewHead = GameField(snakeNewHead); EndGame(snakeElements, obstacles, nickname, snakeNewHead); PrintScoreAndLevel(userScore, level); Console.SetCursorPosition( snakeHead.col, snakeHead.row); Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write("*"); //snake head in any directions snakeElements.Enqueue(snakeNewHead); Console.SetCursorPosition( snakeNewHead.col, snakeNewHead.row); Console.ForegroundColor = ConsoleColor.Gray; if (direction == right) Console.Write(">"); if (direction == left) Console.Write("<"); if (direction == up) Console.Write("^"); if (direction == down) Console.Write("v"); if (snakeNewHead.col == food.col && snakeNewHead.row == food.row) { // feeding the snake do { food = new Position( randomNumbersGenerator.Next(1, GameHeight - 3), randomNumbersGenerator.Next(1, GameWidth - 3)); } while (snakeElements.Contains(food) || obstacles.Contains(food)); lastFoodTime = Environment.TickCount; Console.SetCursorPosition( food.col, food.row); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("@"); sleepTime--; //update score userScore += 20; PlayerLevel(userScore, randomNumbersGenerator, snakeElements, obstacles, food); } else { // moving... Position last = snakeElements.Dequeue(); Console.SetCursorPosition(last.col, last.row); Console.Write(" "); } if (Environment.TickCount - lastFoodTime >= foodDissapearTime) { Console.SetCursorPosition(food.col, food.row); Console.Write(" "); do { food = new Position( randomNumbersGenerator.Next(1, GameHeight - 3), randomNumbersGenerator.Next(1, GameWidth - 3)); } while (snakeElements.Contains(food) || obstacles.Contains(food)); lastFoodTime = Environment.TickCount; } Console.SetCursorPosition(food.col, food.row); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("@"); sleepTime -= 0.05; Thread.Sleep((int)sleepTime); } }
static void PlayerLevel(int userPoints, Random randomNumbersGenerator , Queue<Position> snakeElements, List<Position> obstacles, Position food) { int rolls = 0; if (userPoints == 40) { level++; rolls = 5; AddObstacle(randomNumbersGenerator, snakeElements, obstacles, food, rolls); } else if (userPoints == 80) { level++; rolls = 6; AddObstacle(randomNumbersGenerator, snakeElements, obstacles, food, rolls); } else if (userPoints == 120) { level++; rolls = 7; AddObstacle(randomNumbersGenerator, snakeElements, obstacles, food, rolls); } else if (userPoints == 160) { level++; rolls = 8; AddObstacle(randomNumbersGenerator, snakeElements, obstacles, food, rolls); } else if (userPoints == 200) { level++; rolls = 9; AddObstacle(randomNumbersGenerator, snakeElements, obstacles, food, rolls); } else if ((userPoints == 240) || (userPoints == 280) || (userPoints == 320) || (userPoints == 360) || (userPoints == 400)) { level++; rolls = 3; AddObstacle(randomNumbersGenerator, snakeElements, obstacles, food, rolls); } }
static Position GameField(Position snakeNewHead) { if (snakeNewHead.col <= 0) { snakeNewHead.col = 1; } if (snakeNewHead.row <= 0) { snakeNewHead.row = 1; } if (snakeNewHead.row >= GameHeight - 2) { snakeNewHead.row = GameHeight - 2; } if (snakeNewHead.col >= GameWidth - 2) { snakeNewHead.col = GameWidth - 2; } return snakeNewHead; }
static void EndGame(Queue<Position> snakeElements, List<Position> obstacles, string nickname, Position snakeNewHead) { if (snakeElements.Contains(snakeNewHead) || obstacles.Contains(snakeNewHead)) { Console.Clear(); Console.CursorVisible = false; Console.SetCursorPosition(GameWidth / 2 + 5, GameHeight / 2); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Game over!"); Console.ReadLine(); UpdateScore(userScore, nickname); Environment.Exit(0); } }
static void AddObstacle(Random randomNumbersGenerator, Queue<Position> snakeElements, List<Position> obstacles, Position food, int rolls) { for (int i = 0; i < rolls; i++) { Position obstacle = new Position(); do { obstacle = new Position( randomNumbersGenerator.Next(1, GameHeight - 3), randomNumbersGenerator.Next(1, GameWidth - 3)); } while (snakeElements.Contains(obstacle) || (food.row != obstacle.row && food.col != obstacle.row)); obstacles.Add(obstacle); Console.SetCursorPosition( obstacle.col, obstacle.row); Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("="); } }
static void Main(string[] args) { byte right = 0; byte left = 1; byte down = 2; byte up = 3; int lastFoodTime = 0; int foodDissapearTime = 8000; int 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 = right; Random randomNumbersGenerator = new Random(); Console.BufferHeight = Console.WindowHeight; lastFoodTime = Environment.TickCount; List<Position> obstacles = new List<Position>() { new Position(12, 12), new Position(14, 20), new Position(7, 7), new Position(19, 19), new Position(6, 9), }; 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)); } Position food; do { food = new Position( randomNumbersGenerator.Next(0, Console.WindowHeight), randomNumbersGenerator.Next(0, Console.WindowWidth)); } while (snakeElements.Contains(food) || obstacles.Contains(food)); Console.SetCursorPosition(food.col, food.row); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("@"); foreach (Position position in snakeElements) { Console.SetCursorPosition( position.col, position.row); Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write("*"); } while (true) { negativePoints++; 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.col < 0) snakeNewHead.col = Console.WindowWidth - 1; if (snakeNewHead.row < 0) snakeNewHead.row = Console.WindowHeight - 1; if (snakeNewHead.row >= Console.WindowHeight) snakeNewHead.row = 0; if (snakeNewHead.col >= Console.WindowWidth) snakeNewHead.col = 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 - negativePoints; //if (userPoints < 0) userPoints = 0; userPoints = Math.Max(userPoints, 0); Console.WriteLine("Your points are: {0}", userPoints); Console.ReadLine(); return; } Console.SetCursorPosition( snakeHead.col, snakeHead.row); Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write("*"); snakeElements.Enqueue(snakeNewHead); Console.SetCursorPosition( snakeNewHead.col, snakeNewHead.row); Console.ForegroundColor = ConsoleColor.Gray; if (direction == right) Console.Write(">"); if (direction == left) Console.Write("<"); if (direction == up) Console.Write("^"); if (direction == down) Console.Write("v"); if (snakeNewHead.col == food.col && snakeNewHead.row == food.row) { // feeding the snake 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("@"); sleepTime--; Position obstacle = new Position(); 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.row)); 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) || obstacles.Contains(food)); lastFoodTime = Environment.TickCount; } Console.SetCursorPosition(food.col, food.row); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("@"); sleepTime -= 0.01; Thread.Sleep((int)sleepTime); } }
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); } }