Game1.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 Microsoft.Xna.Samples.Storage
  11. {
  12. /// <summary>
  13. /// This is the main type for your game
  14. /// </summary>
  15. public class Game1 : Microsoft.Xna.Framework.Game
  16. {
  17. GraphicsDeviceManager graphics;
  18. SpriteBatch spriteBatch;
  19. SpriteFont font;
  20. SaveGame loaded;
  21. public Game1()
  22. {
  23. graphics = new GraphicsDeviceManager(this);
  24. Content.RootDirectory = "Content";
  25. graphics.IsFullScreen = true;
  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<Microsoft.Xna.Framework.Graphics.SpriteFont>("SpriteFont1");
  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. // TODO: Add your update logic here
  66. base.Update(gameTime);
  67. }
  68. /// <summary>
  69. /// This is called when the game should draw itself.
  70. /// </summary>
  71. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  72. protected override void Draw(GameTime gameTime)
  73. {
  74. graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
  75. spriteBatch.Begin();
  76. spriteBatch.DrawString(font,"Name: " + loaded.Name,new Vector2(20,100),Color.Black);
  77. spriteBatch.DrawString(font,"Hi Score: " + loaded.HiScore.ToString(),new Vector2(20,130),Color.Black);
  78. spriteBatch.DrawString(font,"Date: " + loaded.Date.ToString(),new Vector2(20,160),Color.Black);
  79. spriteBatch.DrawString(font,"Dont Keep: " + loaded.DontKeep.ToString(),new Vector2(20,190),Color.Black);
  80. spriteBatch.End();
  81. base.Draw(gameTime);
  82. }
  83. }
  84. }