HighScores.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Input;
  7. using Microsoft.Xna.Framework.Audio;
  8. using Microsoft.Xna.Framework.Graphics;
  9. namespace XNAPacMan {
  10. /// <summary>
  11. /// This is a game component that implements IUpdateable.
  12. /// </summary>
  13. public class HighScores : Microsoft.Xna.Framework.DrawableGameComponent {
  14. public HighScores(Game game)
  15. : base(game) {
  16. // TODO: Construct any child components here
  17. }
  18. /// <summary>
  19. /// Allows the game component to perform any initialization it needs to before starting
  20. /// to run. This is where it can query for any required services and load content.
  21. /// </summary>
  22. public override void Initialize() {
  23. scores_ = new List<string>(10);
  24. const string fileName = "highscores.txt";
  25. if (File.Exists(fileName)) {
  26. scores_ = File.ReadAllLines(fileName).ToList<string>();
  27. scores_.Sort((a, b) => Convert.ToInt32(a).CompareTo(Convert.ToInt32(b)));
  28. scores_.Reverse();
  29. }
  30. scoreFont_ = Game.Content.Load<SpriteFont>("Score");
  31. itemFont_ = Game.Content.Load<SpriteFont>("MenuItem");
  32. selectionArrow_ = Game.Content.Load<Texture2D>("sprites/Selection");
  33. spriteBatch_ = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
  34. graphics_ = (GraphicsDeviceManager)Game.Services.GetService(typeof(GraphicsDeviceManager));
  35. oldState_ = Keyboard.GetState();
  36. base.Initialize();
  37. }
  38. /// <summary>
  39. /// Allows the game component to update itself.
  40. /// </summary>
  41. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  42. public override void Update(GameTime gameTime) {
  43. // TODO: Add your update code here
  44. if (Keyboard.GetState().GetPressedKeys().Length > 0 && oldState_.GetPressedKeys().Length == 0) {
  45. Game.Components.Remove(this);
  46. Game.Components.Add(new Menu(Game, null));
  47. }
  48. oldState_ = Keyboard.GetState();
  49. base.Update(gameTime);
  50. }
  51. /// <summary>
  52. /// Allows the component to draw itself
  53. /// </summary>
  54. /// <param name="gameTime">Provides a snapshot of timing values</param>
  55. public override void Draw(GameTime gameTime) {
  56. base.Draw(gameTime);
  57. Vector2 position = new Vector2(graphics_.PreferredBackBufferWidth / 2 - 150, graphics_.PreferredBackBufferHeight / 2 - 200);
  58. spriteBatch_.Begin();
  59. for (int i = 0; i < 10; i++) {
  60. spriteBatch_.DrawString(scoreFont_, (i + 1).ToString() + ".", new Vector2(position.X, position.Y + (30 * i)), Color.White);
  61. if (i < scores_.Count) {
  62. spriteBatch_.DrawString(scoreFont_, scores_[i], new Vector2(position.X + 50, position.Y + (30 * i)), Color.White);
  63. }
  64. }
  65. Vector2 itemPosition;
  66. itemPosition.X = (graphics_.PreferredBackBufferWidth / 2) - 100;
  67. itemPosition.Y = (graphics_.PreferredBackBufferHeight / 2) + 200;
  68. spriteBatch_.Draw(selectionArrow_, new Vector2(itemPosition.X - 50, itemPosition.Y), Color.White);
  69. spriteBatch_.DrawString(itemFont_, "Return", itemPosition, Color.Yellow);
  70. spriteBatch_.End();
  71. }
  72. List<string> scores_;
  73. SpriteFont scoreFont_;
  74. SpriteFont itemFont_;
  75. Texture2D selectionArrow_;
  76. SpriteBatch spriteBatch_;
  77. GraphicsDeviceManager graphics_;
  78. KeyboardState oldState_;
  79. }
  80. }