//Show player name and his score public static void ScoreField(int x, int y, Player player) { Console.SetCursorPosition(x, y); Console.ForegroundColor = ConsoleColor.White; Console.Write("---------------------------------"); Console.SetCursorPosition(x + 1, y + 1); Console.Write("Player: {0} ", player.Name); Console.SetCursorPosition(x + 1, y + 3); Console.Write("Score: {0} ", player.Score); Console.SetCursorPosition(20, 35); }
//Save info about the players with the highest scores in a file public static void SaveHighestScores(string filePath, Player playerCurrent, int topScoreCount) { List<Player> highScoreList = new List<Player>(); //Get info about the players with the highest scores highScoreList = GetHighestScores(filePath); try { StreamWriter writer = new StreamWriter(filePath); using (writer) { bool ifPlayerExist = false; if (highScoreList.Count != 0) { foreach (Player player in highScoreList) { if (player.Name == playerCurrent.Name) { if (player.Score < playerCurrent.Score) { player.Score = playerCurrent.Score; } ifPlayerExist = true; } } if (!ifPlayerExist) { highScoreList.Add(new Player { Name = playerCurrent.Name, Score = playerCurrent.Score }); } var sortedList = highScoreList.OrderByDescending(x => x.Score); highScoreList = sortedList.ToList<Player>(); for (int i = 0; i < highScoreList.Count; i++) { if (i < topScoreCount) { writer.WriteLine(highScoreList[i].Name + " " + highScoreList[i].Score); } } } else { writer.WriteLine(playerCurrent.Name + " " + playerCurrent.Score); } } } catch (Exception) { Console.Clear(); throw; } }
//Save current game, player and score in a file public static void SaveGame(string filePath, PlayField playField, Player player) { try { StreamWriter writer = new StreamWriter(filePath); using (writer) { writer.WriteLine(player.Name); writer.WriteLine(player.Score); for (int i = 0; i < playField.GetLength(0); i++) { for (int j = 0; j < playField.GetLength(1); j++) { writer.Write(playField[i, j].X + " "); writer.Write(playField[i, j].Y + " "); writer.Write(playField[i, j].Symbol + " "); writer.Write(playField[i, j].Color + " "); } writer.WriteLine(); } } } catch (Exception) { Console.Clear(); throw; } }
//Load the last saved game, player and score from a file public static void LoadGame(string filePath, PlayField playField, Player player) { try { StreamReader reader = new StreamReader(filePath); using (reader) { player.Name = reader.ReadLine(); player.Score = int.Parse(reader.ReadLine()); for (int i = 0; i < playField.GetLength(0); i++) { string line = reader.ReadLine(); string[] currentLine = line.Trim().Split(' '); int counter = 0; for (int j = 0; j < currentLine.Length; j += 4) { int x = int.Parse(currentLine[j]); int y = int.Parse(currentLine[j + 1]); char symbol = char.Parse(currentLine[j + 2]); ConsoleColor color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), currentLine[j + 3]); Box box = new Box(x, y, symbol, color); box.InitBox(symbol); playField[i, counter] = box; counter++; } } } } catch (Exception) { Console.Clear(); throw; } }
//The Main Menu of the Game private static void Menu() { Methods.Settings(PLAYFIELDWIDTH, PLAYFIELDHIGHT); Methods.DrawMenu(player.Name, gameoverFlag); bool isCommand = false; while (!isCommand) { ConsoleKeyInfo keyPressed = Console.ReadKey(true); switch (keyPressed.Key) { //Load a new game case ConsoleKey.N: { gameoverFlag = false; player = new Player(Methods.LoadPlayer(), 0); Console.Clear(); Console.SetWindowSize(PLAYFIELDWIDTH + 7, PLAYFIELDHIGHT); Console.SetBufferSize(PLAYFIELDWIDTH + 7, PLAYFIELDHIGHT); playField = new PlayField(); playField.InitPlayField(CHARBASE); playField.DrawPlayField(); Methods.Settings(PLAYFIELDWIDTH, PLAYFIELDHIGHT); Methods.VisualizeSound(soundflag); FallDownAndGenerateNewJewels(); Methods.ScoreField(SCOREFIELDWIDTH, SCOREFIELDHIGHT, player); //Draw a progress bar showing how near is the maximum score Methods.DrawProgress(player.Score, MAXSCORE, PROGRESSBARWIDTH, PROGRESSBARHIGHT); escapeFlag = 0; Engine(); isCommand = true; } break; //Return to the game or go to the Menu case ConsoleKey.Escape: { if ((player.Name != "") && !gameoverFlag) { if (escapeFlag == 0) { Console.Clear(); Console.SetWindowSize(PLAYFIELDWIDTH + 7, PLAYFIELDHIGHT); Console.SetBufferSize(PLAYFIELDWIDTH + 7, PLAYFIELDHIGHT); playField.DrawPlayField(); Methods.Settings(PLAYFIELDWIDTH, PLAYFIELDHIGHT); Methods.ScoreField(SCOREFIELDWIDTH, SCOREFIELDHIGHT, player); //Draw a progress bar showing how near is the maximum score Methods.DrawProgress(player.Score, MAXSCORE, PROGRESSBARWIDTH, PROGRESSBARHIGHT); Engine(); escapeFlag = 1; } else { escapeFlag = 0; Menu(); } isCommand = true; } }; break; //Save the game in a file case ConsoleKey.S: { if ((player.Name != "") && !gameoverFlag) { Methods.SaveHighestScores(HIGHSCOREFILEPATH, player, TOPSCORECOUNT); Methods.SaveGame(GAMEFILEPATH, playField, player); string saveMessage = "The Game has been saved!"; Console.SetCursorPosition((Console.WindowWidth - saveMessage.Length) / 2, Console.WindowHeight - 2); Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine(saveMessage); } isCommand = false; }; break; //Load a game from a file case ConsoleKey.L: { gameoverFlag = false; Console.Clear(); Console.SetWindowSize(PLAYFIELDWIDTH + 7, PLAYFIELDHIGHT); Console.SetBufferSize(PLAYFIELDWIDTH + 7, PLAYFIELDHIGHT); playField = new PlayField(); Methods.LoadGame(GAMEFILEPATH, playField, player); playField.DrawPlayField(); Methods.Settings(PLAYFIELDWIDTH, PLAYFIELDHIGHT); Methods.ScoreField(SCOREFIELDWIDTH, SCOREFIELDHIGHT, player); Methods.VisualizeSound(soundflag); //Draw a progress bar showing how near is the maximum score Methods.DrawProgress(player.Score, MAXSCORE, PROGRESSBARWIDTH, PROGRESSBARHIGHT); escapeFlag = 0; Engine(); isCommand = true; }; break; case ConsoleKey.T: { topscoreFlag = true; Methods.ShowTopScores(HIGHSCOREFILEPATH); Engine(); //MessageBox.Show(message, "TOP SCORES!!!", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); }; break; //Quit the game case ConsoleKey.Q: { Environment.Exit(0); isCommand = true; }; break; default: { isCommand = false; }; break; } } }
//When the max scores are reached, show message and call the Main Menu private static void GameOver() { Methods.SaveHighestScores(HIGHSCOREFILEPATH, player, TOPSCORECOUNT); player = new Player(); gameoverFlag = true; Console.Clear(); Console.SetCursorPosition(Console.WindowWidth / 3 - 2, Console.WindowHeight / 3); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("LEVEL COMPLETE!"); Console.SetCursorPosition(Console.WindowWidth / 11 - 1, Console.WindowHeight / 3 + 2); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("You've reached the max score!"); Thread.Sleep(2000); Console.CursorVisible = false; Menu(); }