Game1.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System.Collections.Generic;
  5. namespace Tutorial004
  6. {
  7. /// <summary>
  8. /// This is the main type for your game.
  9. /// </summary>
  10. public class Game1 : Game
  11. {
  12. GraphicsDeviceManager graphics;
  13. SpriteBatch spriteBatch;
  14. private List<Sprite> _sprites;
  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. _sprites = new List<Sprite>()
  41. {
  42. new Sprite(texture)
  43. {
  44. Position = new Vector2(100, 100),
  45. Input = new Input()
  46. {
  47. Up = Keys.W,
  48. Down = Keys.S,
  49. Left = Keys.A,
  50. Right = Keys.D,
  51. },
  52. },
  53. new Sprite(texture)
  54. {
  55. Position = new Vector2(200, 100),
  56. Input = new Input()
  57. {
  58. Up = Keys.Up,
  59. Down = Keys.Down,
  60. Left = Keys.Left,
  61. Right = Keys.Right,
  62. },
  63. },
  64. };
  65. }
  66. /// <summary>
  67. /// UnloadContent will be called once per game and is the place to unload
  68. /// game-specific content.
  69. /// </summary>
  70. protected override void UnloadContent()
  71. {
  72. // TODO: Unload any non ContentManager content here
  73. }
  74. /// <summary>
  75. /// Allows the game to run logic such as updating the world,
  76. /// checking for collisions, gathering input, and playing audio.
  77. /// </summary>
  78. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  79. protected override void Update(GameTime gameTime)
  80. {
  81. foreach (var sprite in _sprites)
  82. sprite.Update();
  83. base.Update(gameTime);
  84. }
  85. /// <summary>
  86. /// This is called when the game should draw itself.
  87. /// </summary>
  88. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  89. protected override void Draw(GameTime gameTime)
  90. {
  91. GraphicsDevice.Clear(Color.CornflowerBlue);
  92. spriteBatch.Begin();
  93. foreach (var sprite in _sprites)
  94. sprite.Draw(spriteBatch);
  95. spriteBatch.End();
  96. base.Draw(gameTime);
  97. }
  98. }
  99. }