Game1.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System.Collections.Generic;
  5. using Tutorial009.Models;
  6. using Tutorial009.Sprites;
  7. namespace Tutorial009
  8. {
  9. /// <summary>
  10. /// This is the main type for your game.
  11. /// </summary>
  12. public class Game1 : Game
  13. {
  14. GraphicsDeviceManager graphics;
  15. SpriteBatch spriteBatch;
  16. private List<Sprite> _sprites;
  17. public Game1()
  18. {
  19. graphics = new GraphicsDeviceManager(this);
  20. Content.RootDirectory = "Content";
  21. }
  22. /// <summary>
  23. /// Allows the game to perform any initialization it needs to before starting to run.
  24. /// This is where it can query for any required services and load any non-graphic
  25. /// related content. Calling base.Initialize will enumerate through any components
  26. /// and initialize them as well.
  27. /// </summary>
  28. protected override void Initialize()
  29. {
  30. // TODO: Add your initialization logic here
  31. base.Initialize();
  32. }
  33. /// <summary>
  34. /// LoadContent will be called once per game and is the place to load
  35. /// all of your content.
  36. /// </summary>
  37. protected override void LoadContent()
  38. {
  39. // Create a new SpriteBatch, which can be used to draw textures.
  40. spriteBatch = new SpriteBatch(GraphicsDevice);
  41. var playerTexture = Content.Load<Texture2D>("Block");
  42. _sprites = new List<Sprite>()
  43. {
  44. new Player(playerTexture)
  45. {
  46. Input = new Input()
  47. {
  48. Left = Keys.A,
  49. Right = Keys.D,
  50. Up = Keys.W,
  51. Down = Keys.S,
  52. },
  53. Position = new Vector2(100, 100),
  54. Colour = Color.Blue,
  55. Speed = 5,
  56. },
  57. new Player(playerTexture)
  58. {
  59. Input = new Input()
  60. {
  61. Left = Keys.Left,
  62. Right = Keys.Right,
  63. Up = Keys.Up,
  64. Down = Keys.Down,
  65. },
  66. Position = new Vector2(300, 100),
  67. Colour = Color.Red,
  68. Speed = 5,
  69. },
  70. };
  71. }
  72. /// <summary>
  73. /// UnloadContent will be called once per game and is the place to unload
  74. /// game-specific content.
  75. /// </summary>
  76. protected override void UnloadContent()
  77. {
  78. // TODO: Unload any non ContentManager content here
  79. }
  80. /// <summary>
  81. /// Allows the game to run logic such as updating the world,
  82. /// checking for collisions, gathering input, and playing audio.
  83. /// </summary>
  84. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  85. protected override void Update(GameTime gameTime)
  86. {
  87. foreach (var sprite in _sprites)
  88. sprite.Update(gameTime, _sprites);
  89. base.Update(gameTime);
  90. }
  91. /// <summary>
  92. /// This is called when the game should draw itself.
  93. /// </summary>
  94. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  95. protected override void Draw(GameTime gameTime)
  96. {
  97. GraphicsDevice.Clear(Color.CornflowerBlue);
  98. spriteBatch.Begin();
  99. foreach (var sprite in _sprites)
  100. sprite.Draw(spriteBatch);
  101. spriteBatch.End();
  102. base.Draw(gameTime);
  103. }
  104. }
  105. }