//Change the position of the rocks and return if the game is over. static bool ChangeRockPosition() { List <int> removingRocks = new List <int>(); for (int i = 0; i < rocksCollection.Count; i++) { var itemValue = rocksCollection.ElementAt(i).Value; if (itemValue.PositionRow > 0) { DrawOnTheConsole.RemoveFromConsole(itemValue.PositionColumn, itemValue.PositionRow); } if (itemValue.PositionRow < Console.WindowHeight - 1) { itemValue.PositionRow++; bool isGameOver = CollisionDetect(itemValue.PositionColumn, itemValue.PositionRow); if (isGameOver == true) { return(false); } DrawOnTheConsole.DrawRocks(itemValue.PositionColumn, itemValue.PositionRow, itemValue.Character, itemValue.Color); } else { removingRocks.Add(rocksCollection.ElementAt(i).Key); } } DrawOnTheConsole.PrintScore(removingRocks.Count); foreach (var rock in removingRocks) { rocksCollection.Remove(rock); } return(true); }
//The main logic of the game. static void PlayGame() { SetGameField(); DrawOnTheConsole.DrawMenu(); DrawOnTheConsole.PrintOnTheConsole(dwarfBeginPositionColumn, dwarfBeginPositionRow, "(0)", ConsoleColor.Green); while (true) { GenerateRocks(); bool isGamePlay = ChangeRockPosition(); if (isGamePlay == false) { break; } if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(true); ChangePlayerDirection(key); MoveDwarf(); DrawOnTheConsole.PrintOnTheConsole(dwarfBeginPositionColumn, dwarfBeginPositionRow, "(0)", ConsoleColor.Green); } Thread.Sleep(150); } DrawOnTheConsole.PrintGameOver(); IsGameContinue(); }
//Restart the game and print Max Scores static void ResetGame() { Console.Clear(); rocksCollection.Clear(); autonumber = 0; if (score > maxScore) { maxScore = score; } if (maxScore > 0) { DrawOnTheConsole.PrintOnTheConsole(playingScreen + 4, 7, string.Format("Max score: {0}", maxScore), ConsoleColor.DarkGreen); } score = 0; PlayGame(); }
//Change the position of the dwarf on the screen. static void MoveDwarf() { if (IsOutOfBoundry() == false) { if (dwarfDirection == right) { DrawOnTheConsole.RemoveFromConsole(dwarfBeginPositionColumn, dwarfBeginPositionRow); dwarfBeginPositionColumn++; } if (dwarfDirection == left) { DrawOnTheConsole.RemoveFromConsole(dwarfBeginPositionColumn + 2, dwarfBeginPositionRow); dwarfBeginPositionColumn--; } } }