Game1.cs 2.7 KB

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