#region File Description //----------------------------------------------------------------------------- // LoadingScreen.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #endregion #region Using Statements using System; using System.Collections.Generic; using System.IO.IsolatedStorage; using System.IO; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework; using GameStateManagement; using Microsoft.Xna.Framework.Input.Touch; #endregion namespace MemoryMadness { class HighScoreScreen : GameScreen { #region Fields // define default highscore table const int highscorePlaces = 10; public static List> highScore = new List>(highscorePlaces) { new KeyValuePair("Jasper",10), new KeyValuePair("Ellen",9), new KeyValuePair("Terry",8), new KeyValuePair("Lori",7), new KeyValuePair("Michael",6), new KeyValuePair("Carol",5), new KeyValuePair("Toni",4), new KeyValuePair("Cassie",3), new KeyValuePair("Luca",2), new KeyValuePair("Brian",1) }; SpriteFont highScoreFont; #endregion #region Initialzations public HighScoreScreen() { EnabledGestures = GestureType.Tap; } #endregion #region Loading /// /// Load screen resources. /// public override void LoadContent() { highScoreFont = Load(@"Fonts\HighScoresFont"); base.LoadContent(); } #endregion #region Handle Input /// /// Handles user input as a part of screen logic update /// /// public override void HandleInput(InputState input) { if (input == null) throw new ArgumentNullException("input"); if (input.IsPauseGame(null)) { Exit(); } // Return to the main menu when a tap gesture is recognized if (input.Gestures.Count > 0) { GestureSample sample = input.Gestures[0]; if (sample.GestureType == GestureType.Tap) { Exit(); input.Gestures.Clear(); } } } /// /// Exit this screen /// private void Exit() { this.ExitScreen(); ScreenManager.AddScreen(new BackgroundScreen(false), null); ScreenManager.AddScreen(new MainMenuScreen(), null); } #endregion #region Render /// /// Renders the screen /// /// public override void Draw(Microsoft.Xna.Framework.GameTime gameTime) { ScreenManager.SpriteBatch.Begin(); // Draw the title string text = "High Scores"; var textSize = highScoreFont.MeasureString(text); var position = new Vector2( ScreenManager.GraphicsDevice.Viewport.Width / 2 - textSize.X / 2, 340); ScreenManager.SpriteBatch.DrawString(highScoreFont, text, position, Color.Red); // Draw the highscores table for (int i = 0; i < highScore.Count; i++) { ScreenManager.SpriteBatch.DrawString(highScoreFont, String.Format("{0,2}. {1}", i + 1, highScore[i].Key), new Vector2(50, i * 40 + position.Y + 40), Color.YellowGreen); ScreenManager.SpriteBatch.DrawString(highScoreFont, highScore[i].Value.ToString(), new Vector2(370, i * 40 + position.Y + 40), Color.YellowGreen); } ScreenManager.SpriteBatch.End(); base.Draw(gameTime); } #endregion #region Highscore loading/saving logic /// /// Check if a score belongs on the high score table /// /// public static bool IsInHighscores(int level) { // If the score is higher than the worst score in the table return level > highScore[highscorePlaces - 1].Value; } /// /// Put high score on highscores table /// /// Name of the player who achieved the highscore. /// The level the player reached. public static void PutHighScore(string playerName, int level) { if (IsInHighscores(level)) { highScore[highscorePlaces - 1] = new KeyValuePair(playerName, level); OrderGameScore(); } } /// /// Order the high scores table. /// private static void OrderGameScore() { highScore.Sort(CompareScores); } /// /// Comparison method used to compare two highscore entries. /// /// First highscore entry. /// Second highscore entry. /// 1 if the first highscore is smaller than the second, 0 if both /// are equal and -1 otherwise. private static int CompareScores(KeyValuePair score1, KeyValuePair score2) { if (score1.Value < score2.Value) { return 1; } if (score1.Value == score2.Value) { return 0; } return -1; } /// /// Saves the current highscore to a text file. /// public static void SaveHighscore() { // Get the place to store data using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { // Create a file to save the highscore data /*using (IsolatedStorageFileStream isfs = isf.CreateFile("highscores.txt")) { using (StreamWriter writer = new StreamWriter(isfs)) { for (int i = 0; i < highScore.Count; i++) { // Write the scores writer.WriteLine(highScore[i].Key); writer.WriteLine(highScore[i].Value.ToString()); } } }*/ } } /// /// Loads the high scores from a text file. /// public static void LoadHighscores() { // Get the place where data is stored /*using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { // Try to open the file if (isf.FileExists("highscores.txt")) { using (IsolatedStorageFileStream isfs = isf.OpenFile("highscores.txt", FileMode.Open)) { // Get the stream to read the data using (StreamReader reader = new StreamReader(isfs)) { // Read the highscores int i = 0; while (!reader.EndOfStream) { string name = reader.ReadLine(); int score = int.Parse(reader.ReadLine()); highScore[i++] = new KeyValuePair( name, score); } } } } } OrderGameScore(); */ } #endregion } }