SavingScores savingScores; //saving scores contains the information //of the class SavingScores public HighScores(main MainValue) { main = MainValue; //passing the information that the constructor //is receiving regarding to main to the variable MainValue savingScores = new SavingScores(); //saying that savingScores is going to call a //new method savingScores() keyPressed = false; //assigning false to the bool }
public void Update(KeyboardState keyBoard) //update method that receive keys from the keyboard { points = main.PointsSaver; //giving the variable the points from the points saver if (main.LoadPointsHighScore) //if the variable is true { main.LoadPointsHighScore = false; //setting the variable to false try //exception handling { savingScores = JsonConvert.DeserializeObject <SavingScores>(File.ReadAllText("HighScores.json")); //saving the variable in the file that is converted to json file } catch (System.Exception) //catch block { Console.WriteLine("ERROR WITH THE FILE"); //if there is a problem with the file display } } if (keyBoard.IsKeyDown(Keys.Escape)) //if the key esc is pressed exit the app { main.gameState = main.GameState.Exit; } if (keyBoard.IsKeyDown(Keys.S) && !keyPressed) //if the key S is pressed and the bool is false then { keyPressed = true; //set the bool to true savingScores.HighScoresPoints.Add((double)points); //adding the points to the array savingScores.HighScoresPoints.Sort(); //sorting the array if (savingScores.HighScoresPoints.Count > 5) //if the size of the array is greater than 5 { savingScores.HighScoresPoints.RemoveAt(0); //remove the lowest score } string JsonSaver = JsonConvert.SerializeObject(savingScores); //saving the array in the file File.WriteAllText("HighScores.json", JsonSaver); //name of the file } }