Game1.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. namespace Tutorial001
  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. /// <summary>
  14. /// The texture is what we view on the screen
  15. /// </summary>
  16. private Texture2D _texture;
  17. /// <summary>
  18. /// The position is where our object is relative to the top-left corner
  19. /// </summary>
  20. private Vector2 _position;
  21. public Game1()
  22. {
  23. graphics = new GraphicsDeviceManager(this);
  24. Content.RootDirectory = "Content";
  25. }
  26. /// <summary>
  27. /// Allows the game to perform any initialization it needs to before starting to run.
  28. /// This is where it can query for any required services and load any non-graphic
  29. /// related content. Calling base.Initialize will enumerate through any components
  30. /// and initialize them as well.
  31. /// </summary>
  32. protected override void Initialize()
  33. {
  34. // TODO: Add your initialization logic here
  35. base.Initialize();
  36. }
  37. /// <summary>
  38. /// LoadContent will be called once per game and is the place to load
  39. /// all of your content.
  40. /// </summary>
  41. protected override void LoadContent()
  42. {
  43. // Create a new SpriteBatch, which can be used to draw textures.
  44. spriteBatch = new SpriteBatch(GraphicsDevice);
  45. // Use the content manager to load our texture
  46. _texture = Content.Load<Texture2D>("Box");
  47. // (0, 0) is the top-left corner
  48. _position = new Vector2(0, 0);
  49. }
  50. /// <summary>
  51. /// UnloadContent will be called once per game and is the place to unload
  52. /// game-specific content.
  53. /// </summary>
  54. protected override void UnloadContent()
  55. {
  56. // TODO: Unload any non ContentManager content here
  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. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  66. Exit();
  67. // TODO: Add your update logic here
  68. base.Update(gameTime);
  69. }
  70. /// <summary>
  71. /// This is called when the game should draw itself.
  72. /// </summary>
  73. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  74. protected override void Draw(GameTime gameTime)
  75. {
  76. GraphicsDevice.Clear(Color.CornflowerBlue);
  77. // Let's MonoGame know we're about to add some sprites to the batcher
  78. spriteBatch.Begin();
  79. // The most basic sprite we can add.
  80. spriteBatch.Draw(_texture, _position, Color.White);
  81. // Can only be called after 'Begin'. Once called, draws everything added to the batcher
  82. spriteBatch.End();
  83. base.Draw(gameTime);
  84. }
  85. }
  86. }