public void Initialize() { // Get the path of the save game string fullpath = Path.Combine(Location, HighScoresFilename); // Check to see if the save exists if (!File.Exists(fullpath)) { //If the file doesn't exist, make a fake one... // Create the data to save HighScoreData data = new HighScoreData(10); data.PlayerName[0] = "Neil"; //data.Level[0] = 10; data.Score[0] = 1; data.PlayerName[1] = "Shawn"; //data.Level[1] = 10; data.Score[1] = 2; data.PlayerName[2] = "Mark"; //data.Level[2] = 9; data.Score[2] = 3; data.PlayerName[3] = "Cindy"; //data.Level[3] = 7; data.Score[3] = 4; data.PlayerName[4] = "Sam"; //data.Level[4] = 1; data.Score[4] = 5; SaveHighScores(data, HighScoresFilename); } }
public void OrderSave() { // Create the data to save HighScoreData data = LoadHighScores(HighScoresFilename); int scoreIndex = -1; for (int i = 0; i < data.Count; i++) { if (Score.score > data.Score[i]) { scoreIndex = i; break; } } if (scoreIndex > -1) { //New high score found ... do swaps for (int i = data.Count - 1; i > scoreIndex; i--) { data.PlayerName[i] = data.PlayerName[i - 1]; data.Score[i] = data.Score[i - 1]; //data.Level[i] = data.Level[i - 1]; } data.PlayerName[scoreIndex] = "Frankie"; //Retrieve User Name Here data.Score[scoreIndex] = Score.score; SaveHighScores(data, HighScoresFilename); } }
protected override void Update(GameTime gameTime) { //Console.WriteLine("DEAD: "+playerDied + " "+ Health.RemainHealth() + " "+PlayerShip.dead); GameTime = gameTime; keyboardState = Keyboard.GetState(); Input.Update(); // Allows the game to exit if (Input.WasButtonPressed(Buttons.Back) || Input.WasKeyPressed(Keys.Escape)) { this.Exit(); } if (_state != GameState.MainMenu && _state != GameState.WonGame && _state != GameState.EndOfGame && _state != GameState.Leaderboard) { EntityManager.Update(); EnemySpawner.Update(); if (playerDied) { Console.WriteLine("Saving Highscore"); HighScoreData data = new HighScoreData(10); data.PlayerName[6] = "Bob"; data.Score[6] = Score.score; a.OrderSave(); } } base.Update(gameTime); switch (_state) { case GameState.MainMenu: UpdateMainMenu(gameTime); break; case GameState.LevelOne: UpdateLevelOne(gameTime); break; case GameState.LevelTwo: UpdateLevelTwo(gameTime); break; case GameState.LevelBoss: UpdateLevelBoss(gameTime); break; case GameState.WonGame: UpdateWonGame(gameTime); break; case GameState.EndOfGame: UpdateEndOfGame(gameTime); break; case GameState.Leaderboard: UpdateLeaderboard(gameTime); break; } }
public static void SaveHighScores(HighScoreData data, String fileName) { // Get the path of the save game string fullpath = Path.Combine(Location, fileName); // Open the file, creating it if necessary FileStream stream = File.Open(fullpath, FileMode.OpenOrCreate); try { // Convert the object to XML data and put it in the stream XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData)); serializer.Serialize(stream, data); } finally { // Close the file stream.Close(); } }