Game1.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System.Collections.Generic;
  5. using Tutorial006.Sprites;
  6. namespace Tutorial006
  7. {
  8. /// <summary>
  9. /// This is the main type for your game.
  10. /// </summary>
  11. public class Game1 : Game
  12. {
  13. GraphicsDeviceManager graphics;
  14. SpriteBatch spriteBatch;
  15. private List<Sprite> _sprites;
  16. public Game1()
  17. {
  18. graphics = new GraphicsDeviceManager(this);
  19. Content.RootDirectory = "Content";
  20. }
  21. /// <summary>
  22. /// Allows the game to perform any initialization it needs to before starting to run.
  23. /// This is where it can query for any required services and load any non-graphic
  24. /// related content. Calling base.Initialize will enumerate through any components
  25. /// and initialize them as well.
  26. /// </summary>
  27. protected override void Initialize()
  28. {
  29. // TODO: Add your initialization logic here
  30. base.Initialize();
  31. }
  32. /// <summary>
  33. /// LoadContent will be called once per game and is the place to load
  34. /// all of your content.
  35. /// </summary>
  36. protected override void LoadContent()
  37. {
  38. // Create a new SpriteBatch, which can be used to draw textures.
  39. spriteBatch = new SpriteBatch(GraphicsDevice);
  40. var shipTexture = Content.Load<Texture2D>("Ship");
  41. _sprites = new List<Sprite>()
  42. {
  43. new Ship(shipTexture)
  44. {
  45. Position = new Vector2(100, 100),
  46. Bullet = new Bullet(Content.Load<Texture2D>("Bullet")),
  47. },
  48. };
  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. foreach (var sprite in _sprites.ToArray())
  66. sprite.Update(gameTime, _sprites);
  67. PostUpdate();
  68. base.Update(gameTime);
  69. }
  70. private void PostUpdate()
  71. {
  72. for (int i = 0; i < _sprites.Count; i++)
  73. {
  74. if (_sprites[i].IsRemoved)
  75. {
  76. _sprites.RemoveAt(i);
  77. i--;
  78. }
  79. }
  80. }
  81. /// <summary>
  82. /// This is called when the game should draw itself.
  83. /// </summary>
  84. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  85. protected override void Draw(GameTime gameTime)
  86. {
  87. GraphicsDevice.Clear(Color.CornflowerBlue);
  88. spriteBatch.Begin();
  89. foreach (var sprite in _sprites)
  90. sprite.Draw(spriteBatch);
  91. spriteBatch.End();
  92. base.Draw(gameTime);
  93. }
  94. }
  95. }