static void Main() { bgrMusic.Play(); MainMenue.LoadingGame(); MainMenue.SplashScreen(); Console.Clear(); Exit(); if (exit == true) { Console.BackgroundColor = ConsoleColor.Black; return; } }
static int GetDirectionFromKeyboard(int direction) { try { ConsoleKeyInfo pressedKey = Console.ReadKey(true); if (pressedKey.Key == ConsoleKey.LeftArrow) { if (direction != (int)Commands.right) { direction = (int)Commands.left; } } else if (pressedKey.Key == ConsoleKey.RightArrow) { if (direction != (int)Commands.left) { direction = (int)Commands.right; } } else if (pressedKey.Key == ConsoleKey.UpArrow) { if (direction != (int)Commands.down) { direction = (int)Commands.up; } } else if (pressedKey.Key == ConsoleKey.DownArrow) { if (direction != (int)Commands.up) { direction = (int)Commands.down; } } else { throw new InvalidOperationException("Invalid key. To play - use only the arrow keys. Press any key to go to the main menu."); } } catch (InvalidOperationException exc) { Console.Clear(); Console.WriteLine(exc.Message); Console.ReadKey(); snakeBody.Clear(); MainMenue.StartMenueOptions(); } return(direction); }
public static void GamePlay() { bgrMusic.Stop(); InitiateGameField(); fullScore = 0; levelScore = 0; level = 1; int timeSleep = 100; int command = (int)Commands.right; bool[,] obstacleCoordinates = new bool[Console.WindowHeight, Console.WindowWidth]; GenerateObstacles(obstacleCoordinates); List <GameObject> obstacles = GetObstacles(obstacleCoordinates); PrintObstacles(obstacles); GameObject food = GenerateFood(obstacles); food.Print(foodSymbol, foodColor); showFood = DateTime.Now; GameObject poison = GeneratePoisonFood(obstacles, food); poison.Print(poisonFood, poisonColor); while (true) { if (Console.KeyAvailable) { command = GetDirectionFromKeyboard(command); } GameObject currentSnakeHead = snakeBody.Last(); if (currentSnakeHead.Equals(poison)) { SoundPlayer poisonSound = new System.Media.SoundPlayer(@"..\\..\\Sounds\\poisonSound.wav"); poisonSound.Play(); levelScore -= 50; Console.SetCursorPosition(0, 0); Console.WriteLine(new string(' ', Console.WindowWidth)); Console.SetCursorPosition(0, 0); Console.WriteLine("Scores = {0}", fullScore + levelScore); Console.SetCursorPosition(Console.WindowWidth - 11, 0); Console.WriteLine("Level = {0}", level); FeedSnake(command); poison = GenerateFood(obstacles); poison.Print(poisonFood, poisonColor); } if (currentSnakeHead.Equals(food)) { SoundPlayer eatingSound = new System.Media.SoundPlayer(@"..\\..\\Sounds\\bitingSound.wav"); eatingSound.Play(); levelScore += 50; Console.SetCursorPosition(0, 0); Console.WriteLine(new string(' ', Console.WindowWidth)); Console.SetCursorPosition(0, 0); Console.WriteLine("Scores = {0}", fullScore + levelScore); Console.SetCursorPosition(Console.WindowWidth - 11, 0); Console.WriteLine("Level = {0}", level); FeedSnake(command); food = GenerateFood(obstacles); food.Print(foodSymbol, foodColor); showFood = DateTime.Now; } bool gameOver = MoveSnake(command, obstacles) || fullScore + levelScore < 0; if (gameOver) { if (fullScore + levelScore < 0) { fullScore = 0; } else { fullScore += levelScore; } SoundPlayer gameOverSound = new System.Media.SoundPlayer(@"..\\..\\Sounds\\gameOverSound.wav"); gameOverSound.Play(); Console.SetCursorPosition(0, 0); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Game over".PadRight(Console.WindowWidth)); Console.SetCursorPosition(0, 1); Console.WriteLine("Scores: {0}".PadRight(Console.WindowWidth + 2), fullScore); Console.SetCursorPosition(0, 2); Console.Write("Enter username:"******" ", "_"); Console.WriteLine(); WriteToFile(fullScore, user); Console.Clear(); MainMenue.WriteTopScores(); gameOverSound.Stop(); snakeBody.Clear(); MainMenue.StartMenueOptions(); bgrMusic.PlayLooping(); return; } Thread.Sleep(timeSleep); bool TooOldFood = DeleteFoodAfterTime(showFood, food, obstacles); if (TooOldFood) { food = GenerateFood(obstacles); food.Print(foodSymbol, foodColor); TooOldFood = false; showFood = DateTime.Now; } if (levelScore == 100)// Not very challenging for demonstration purposes. { SoundPlayer changeLevelSound = new SoundPlayer(@"..\\..\\Sounds\\nextLevelSound.wav"); changeLevelSound.Play(); level++; fullScore += levelScore; levelScore = 0; Console.Clear(); Console.ForegroundColor = ConsoleColor.White; Console.SetCursorPosition(0, 1); Console.WriteLine(new string('-', Console.WindowWidth)); Console.SetCursorPosition(0, 0); Console.WriteLine("Scores = {0}", fullScore + levelScore); Console.SetCursorPosition(Console.WindowWidth - 11, 0); Console.WriteLine("Level = {0}", level); timeSleep -= 5; int snakeLength = snakeBody.Count; snakeBody.Clear(); InitializePrintNewSnake(snakeLength); command = 0; obstacleCoordinates = new bool[Console.WindowHeight, Console.WindowWidth]; GenerateObstacles(obstacleCoordinates); obstacles = GetObstacles(obstacleCoordinates); PrintObstacles(obstacles); food = GenerateFood(obstacles); food.Print(foodSymbol, foodColor); poison = GenerateFood(obstacles); poison.Print(poisonFood, poisonColor); Thread.Sleep(1200); showFood = DateTime.Now; } } }