Game1.cs 4.5 KB

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