Game1.cs 3.1 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 Tutorial022.Sprites;
  6. namespace Tutorial022
  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 texture = Content.Load<Texture2D>("Square");
  41. var player = new Player(texture)
  42. {
  43. Colour = Color.Green,
  44. Position = new Vector2(100, 100),
  45. };
  46. _sprites = new List<Sprite>()
  47. {
  48. player,
  49. new Sprite(texture)
  50. {
  51. Colour = Color.Blue,
  52. Position = new Vector2(200, 200),
  53. }.SetFollowTarget(player, 75f),
  54. new Sprite(texture)
  55. {
  56. Colour = Color.Orange,
  57. Position = new Vector2(400, 200),
  58. FollowTarget = player,
  59. FollowDistance = 150f,
  60. }
  61. };
  62. }
  63. /// <summary>
  64. /// UnloadContent will be called once per game and is the place to unload
  65. /// game-specific content.
  66. /// </summary>
  67. protected override void UnloadContent()
  68. {
  69. // TODO: Unload any non ContentManager content here
  70. }
  71. /// <summary>
  72. /// Allows the game to run logic such as updating the world,
  73. /// checking for collisions, gathering input, and playing audio.
  74. /// </summary>
  75. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  76. protected override void Update(GameTime gameTime)
  77. {
  78. foreach (var sprite in _sprites)
  79. sprite.Update(gameTime);
  80. base.Update(gameTime);
  81. }
  82. /// <summary>
  83. /// This is called when the game should draw itself.
  84. /// </summary>
  85. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  86. protected override void Draw(GameTime gameTime)
  87. {
  88. GraphicsDevice.Clear(Color.CornflowerBlue);
  89. spriteBatch.Begin();
  90. foreach (var sprite in _sprites)
  91. sprite.Draw(gameTime, spriteBatch);
  92. spriteBatch.End();
  93. base.Draw(gameTime);
  94. }
  95. }
  96. }