HighScoreScreen.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // LoadingScreen.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO.IsolatedStorage;
  13. using System.IO;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using Microsoft.Xna.Framework;
  16. using GameStateManagement;
  17. using Microsoft.Xna.Framework.Input.Touch;
  18. #endregion
  19. namespace MemoryMadness
  20. {
  21. class HighScoreScreen : GameScreen
  22. {
  23. #region Fields
  24. // define default highscore table
  25. const int highscorePlaces = 10;
  26. public static List<KeyValuePair<string, int>> highScore =
  27. new List<KeyValuePair<string, int>>(highscorePlaces)
  28. {
  29. new KeyValuePair<string, int>("Jasper",10),
  30. new KeyValuePair<string, int>("Ellen",9),
  31. new KeyValuePair<string, int>("Terry",8),
  32. new KeyValuePair<string, int>("Lori",7),
  33. new KeyValuePair<string, int>("Michael",6),
  34. new KeyValuePair<string, int>("Carol",5),
  35. new KeyValuePair<string, int>("Toni",4),
  36. new KeyValuePair<string, int>("Cassie",3),
  37. new KeyValuePair<string, int>("Luca",2),
  38. new KeyValuePair<string, int>("Brian",1)
  39. };
  40. SpriteFont highScoreFont;
  41. #endregion
  42. #region Initialzations
  43. public HighScoreScreen()
  44. {
  45. EnabledGestures = GestureType.Tap;
  46. }
  47. #endregion
  48. #region Loading
  49. /// <summary>
  50. /// Load screen resources.
  51. /// </summary>
  52. public override void LoadContent()
  53. {
  54. highScoreFont = Load<SpriteFont>(@"Fonts\HighScoresFont");
  55. base.LoadContent();
  56. }
  57. #endregion
  58. #region Handle Input
  59. /// <summary>
  60. /// Handles user input as a part of screen logic update
  61. /// </summary>
  62. /// <param name="input"></param>
  63. public override void HandleInput(InputState input)
  64. {
  65. if (input == null)
  66. throw new ArgumentNullException("input");
  67. if (input.IsPauseGame(null))
  68. {
  69. Exit();
  70. }
  71. // Return to the main menu when a tap gesture is recognized
  72. if (input.Gestures.Count > 0)
  73. {
  74. GestureSample sample = input.Gestures[0];
  75. if (sample.GestureType == GestureType.Tap)
  76. {
  77. Exit();
  78. input.Gestures.Clear();
  79. }
  80. }
  81. }
  82. /// <summary>
  83. /// Exit this screen
  84. /// </summary>
  85. private void Exit()
  86. {
  87. this.ExitScreen();
  88. ScreenManager.AddScreen(new BackgroundScreen(false), null);
  89. ScreenManager.AddScreen(new MainMenuScreen(), null);
  90. }
  91. #endregion
  92. #region Render
  93. /// <summary>
  94. /// Renders the screen
  95. /// </summary>
  96. /// <param name="gameTime"></param>
  97. public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
  98. {
  99. ScreenManager.SpriteBatch.Begin();
  100. // Draw the title
  101. string text = "High Scores";
  102. var textSize = highScoreFont.MeasureString(text);
  103. var position = new Vector2(
  104. ScreenManager.GraphicsDevice.Viewport.Width / 2 - textSize.X / 2,
  105. 340);
  106. ScreenManager.SpriteBatch.DrawString(highScoreFont, text,
  107. position, Color.Red);
  108. // Draw the highscores table
  109. for (int i = 0; i < highScore.Count; i++)
  110. {
  111. ScreenManager.SpriteBatch.DrawString(highScoreFont,
  112. String.Format("{0,2}. {1}", i + 1, highScore[i].Key),
  113. new Vector2(50, i * 40 + position.Y + 40), Color.YellowGreen);
  114. ScreenManager.SpriteBatch.DrawString(highScoreFont,
  115. highScore[i].Value.ToString(),
  116. new Vector2(370, i * 40 + position.Y + 40),
  117. Color.YellowGreen);
  118. }
  119. ScreenManager.SpriteBatch.End();
  120. base.Draw(gameTime);
  121. }
  122. #endregion
  123. #region Highscore loading/saving logic
  124. /// <summary>
  125. /// Check if a score belongs on the high score table
  126. /// </summary>
  127. /// <returns></returns>
  128. public static bool IsInHighscores(int level)
  129. {
  130. // If the score is higher than the worst score in the table
  131. return level > highScore[highscorePlaces - 1].Value;
  132. }
  133. /// <summary>
  134. /// Put high score on highscores table
  135. /// </summary>
  136. /// <param name="name">Name of the player who achieved the highscore.</param>
  137. /// <param name="level">The level the player reached.</param>
  138. public static void PutHighScore(string playerName, int level)
  139. {
  140. if (IsInHighscores(level))
  141. {
  142. highScore[highscorePlaces - 1] =
  143. new KeyValuePair<string, int>(playerName, level);
  144. OrderGameScore();
  145. }
  146. }
  147. /// <summary>
  148. /// Order the high scores table.
  149. /// </summary>
  150. private static void OrderGameScore()
  151. {
  152. highScore.Sort(CompareScores);
  153. }
  154. /// <summary>
  155. /// Comparison method used to compare two highscore entries.
  156. /// </summary>
  157. /// <param name="score1">First highscore entry.</param>
  158. /// <param name="score2">Second highscore entry.</param>
  159. /// <returns>1 if the first highscore is smaller than the second, 0 if both
  160. /// are equal and -1 otherwise.</returns>
  161. private static int CompareScores(KeyValuePair<string, int> score1,
  162. KeyValuePair<string, int> score2)
  163. {
  164. if (score1.Value < score2.Value)
  165. {
  166. return 1;
  167. }
  168. if (score1.Value == score2.Value)
  169. {
  170. return 0;
  171. }
  172. return -1;
  173. }
  174. /// <summary>
  175. /// Saves the current highscore to a text file.
  176. /// </summary>
  177. public static void SaveHighscore()
  178. {
  179. // Get the place to store data
  180. using (IsolatedStorageFile isf =
  181. IsolatedStorageFile.GetUserStoreForApplication())
  182. {
  183. // Create a file to save the highscore data
  184. /*using (IsolatedStorageFileStream isfs =
  185. isf.CreateFile("highscores.txt"))
  186. {
  187. using (StreamWriter writer = new StreamWriter(isfs))
  188. {
  189. for (int i = 0; i < highScore.Count; i++)
  190. {
  191. // Write the scores
  192. writer.WriteLine(highScore[i].Key);
  193. writer.WriteLine(highScore[i].Value.ToString());
  194. }
  195. }
  196. }*/
  197. }
  198. }
  199. /// <summary>
  200. /// Loads the high scores from a text file.
  201. /// </summary>
  202. public static void LoadHighscores()
  203. {
  204. // Get the place where data is stored
  205. /*using (IsolatedStorageFile isf =
  206. IsolatedStorageFile.GetUserStoreForApplication())
  207. {
  208. // Try to open the file
  209. if (isf.FileExists("highscores.txt"))
  210. {
  211. using (IsolatedStorageFileStream isfs =
  212. isf.OpenFile("highscores.txt", FileMode.Open))
  213. {
  214. // Get the stream to read the data
  215. using (StreamReader reader = new StreamReader(isfs))
  216. {
  217. // Read the highscores
  218. int i = 0;
  219. while (!reader.EndOfStream)
  220. {
  221. string name = reader.ReadLine();
  222. int score = int.Parse(reader.ReadLine());
  223. highScore[i++] = new KeyValuePair<string, int>(
  224. name, score);
  225. }
  226. }
  227. }
  228. }
  229. }
  230. OrderGameScore();
  231. */
  232. }
  233. #endregion
  234. }
  235. }