StorageGame.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Content;
  5. //using Microsoft.Xna.Framework.GamerServices;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using Microsoft.Xna.Framework.Input;
  8. using Microsoft.Xna.Framework.Storage;
  9. // Thanks to Ziggyware tutorial found in: http://www.ziggyware.com/readarticle.php?article_id=72
  10. namespace Storage
  11. {
  12. /// <summary>
  13. /// This is the main type for your game
  14. /// </summary>
  15. public class StorageGame : Game
  16. {
  17. GraphicsDeviceManager graphics;
  18. SpriteBatch spriteBatch;
  19. SpriteFont font;
  20. SaveGame loaded;
  21. public StorageGame()
  22. {
  23. graphics = new GraphicsDeviceManager(this);
  24. Content.RootDirectory = "Content";
  25. graphics.IsFullScreen = false;
  26. }
  27. /// <summary>
  28. /// Allows the game to perform any initialization it needs to before starting to run.
  29. /// This is where it can query for any required services and load any non-graphic
  30. /// related content. Calling base.Initialize will enumerate through any components
  31. /// and initialize them as well.
  32. /// </summary>
  33. protected override void Initialize()
  34. {
  35. // TODO: Add your initialization logic here
  36. base.Initialize();
  37. // Save Game Data
  38. SaveGameStorage storage = new SaveGameStorage();
  39. SaveGame sg = new SaveGame();
  40. sg.Name = "Ziggy";
  41. sg.HiScore = 1000;
  42. sg.Date = DateTime.Now;
  43. sg.DontKeep = 123;
  44. storage.Save(sg);
  45. //load the data back in to test if it was successful
  46. loaded = storage.Load();
  47. }
  48. /// <summary>
  49. /// LoadContent will be called once per game and is the place to load
  50. /// all of your content.
  51. /// </summary>
  52. protected override void LoadContent()
  53. {
  54. // Create a new SpriteBatch, which can be used to draw textures.
  55. spriteBatch = new SpriteBatch(GraphicsDevice);
  56. font = Content.Load<SpriteFont>("font");
  57. }
  58. /// <summary>
  59. /// Allows the game to run logic such as updating the world,
  60. /// checking for collisions, gathering input, and playing audio.
  61. /// </summary>
  62. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  63. protected override void Update(GameTime gameTime)
  64. {
  65. // Allows the game to exit
  66. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed
  67. || Keyboard.GetState().IsKeyDown(Keys.Escape))
  68. Exit();
  69. base.Update(gameTime);
  70. }
  71. /// <summary>
  72. /// This is called when the game should draw itself.
  73. /// </summary>
  74. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  75. protected override void Draw(GameTime gameTime)
  76. {
  77. graphics.GraphicsDevice.Clear(Color.MonoGameOrange);
  78. spriteBatch.Begin();
  79. spriteBatch.DrawString(font,"Name: " + loaded.Name,new Vector2(20,100),Color.Black);
  80. spriteBatch.DrawString(font,"Hi Score: " + loaded.HiScore.ToString(),new Vector2(20,130),Color.Black);
  81. spriteBatch.DrawString(font,"Date: " + loaded.Date.ToString(),new Vector2(20,160),Color.Black);
  82. spriteBatch.DrawString(font,"Dont Keep: " + loaded.DontKeep.ToString(),new Vector2(20,190),Color.Black);
  83. spriteBatch.End();
  84. base.Draw(gameTime);
  85. }
  86. }
  87. }