Game1.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. namespace Tutorial005
  5. {
  6. /// <summary>
  7. /// This is the main type for your game.
  8. /// </summary>
  9. public class Game1 : Game
  10. {
  11. GraphicsDeviceManager graphics;
  12. SpriteBatch spriteBatch;
  13. private Sprite _sprite;
  14. public Game1()
  15. {
  16. graphics = new GraphicsDeviceManager(this);
  17. Content.RootDirectory = "Content";
  18. }
  19. /// <summary>
  20. /// Allows the game to perform any initialization it needs to before starting to run.
  21. /// This is where it can query for any required services and load any non-graphic
  22. /// related content. Calling base.Initialize will enumerate through any components
  23. /// and initialize them as well.
  24. /// </summary>
  25. protected override void Initialize()
  26. {
  27. // TODO: Add your initialization logic here
  28. base.Initialize();
  29. }
  30. /// <summary>
  31. /// LoadContent will be called once per game and is the place to load
  32. /// all of your content.
  33. /// </summary>
  34. protected override void LoadContent()
  35. {
  36. // Create a new SpriteBatch, which can be used to draw textures.
  37. spriteBatch = new SpriteBatch(GraphicsDevice);
  38. var texture = Content.Load<Texture2D>("Sprite");
  39. _sprite = new Sprite(texture)
  40. {
  41. Position = new Vector2(100, 100),
  42. Origin = new Vector2(texture.Width / 2, texture.Height - 25),
  43. };
  44. }
  45. /// <summary>
  46. /// UnloadContent will be called once per game and is the place to unload
  47. /// game-specific content.
  48. /// </summary>
  49. protected override void UnloadContent()
  50. {
  51. // TODO: Unload any non ContentManager content here
  52. }
  53. /// <summary>
  54. /// Allows the game to run logic such as updating the world,
  55. /// checking for collisions, gathering input, and playing audio.
  56. /// </summary>
  57. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  58. protected override void Update(GameTime gameTime)
  59. {
  60. _sprite.Update();
  61. base.Update(gameTime);
  62. }
  63. /// <summary>
  64. /// This is called when the game should draw itself.
  65. /// </summary>
  66. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  67. protected override void Draw(GameTime gameTime)
  68. {
  69. GraphicsDevice.Clear(Color.CornflowerBlue);
  70. spriteBatch.Begin();
  71. _sprite.Draw(spriteBatch);
  72. spriteBatch.End();
  73. base.Draw(gameTime);
  74. }
  75. }
  76. }