Game1.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Tutorial021.Sprites;
  7. namespace Tutorial021
  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. public static int ScreenWidth = 1280;
  17. public static int ScreenHeight = 720;
  18. private List<Sprite> _sprites;
  19. public Game1()
  20. {
  21. graphics = new GraphicsDeviceManager(this);
  22. Content.RootDirectory = "Content";
  23. }
  24. /// <summary>
  25. /// Allows the game to perform any initialization it needs to before starting to run.
  26. /// This is where it can query for any required services and load any non-graphic
  27. /// related content. Calling base.Initialize will enumerate through any components
  28. /// and initialize them as well.
  29. /// </summary>
  30. protected override void Initialize()
  31. {
  32. graphics.PreferredBackBufferWidth = ScreenWidth;
  33. graphics.PreferredBackBufferHeight = ScreenHeight;
  34. graphics.ApplyChanges();
  35. base.Initialize();
  36. }
  37. /// <summary>
  38. /// LoadContent will be called once per game and is the place to load
  39. /// all of your content.
  40. /// </summary>
  41. protected override void LoadContent()
  42. {
  43. // Create a new SpriteBatch, which can be used to draw textures.
  44. spriteBatch = new SpriteBatch(GraphicsDevice);
  45. var texture = Content.Load<Texture2D>("Square");
  46. _sprites = new List<Sprite>()
  47. {
  48. new Sprite(Content.Load<Texture2D>("Backgrounds/Sky"))
  49. {
  50. Position = new Vector2(ScreenWidth / 2, ScreenHeight / 2),
  51. CollisionType = CollisionTypes.None,
  52. },
  53. new Player(Content.Load<Texture2D>("Player/boy"))
  54. {
  55. Position = new Vector2(100, 100),
  56. CollisionType = CollisionTypes.Full,
  57. },
  58. new Sprite(texture)
  59. {
  60. Position = new Vector2(50, 350),
  61. Colour = Color.Black,
  62. CollisionType = CollisionTypes.Full,
  63. },
  64. new Sprite(texture)
  65. {
  66. Position = new Vector2(50, 400),
  67. Colour = Color.Black,
  68. CollisionType = CollisionTypes.Full,
  69. },
  70. new Sprite(texture)
  71. {
  72. Position = new Vector2(100, 400),
  73. Colour = Color.Black,
  74. CollisionType = CollisionTypes.Full,
  75. },
  76. new Sprite(texture)
  77. {
  78. Position = new Vector2(150, 400),
  79. Colour = Color.Black,
  80. CollisionType = CollisionTypes.Full,
  81. },
  82. new Sprite(texture)
  83. {
  84. Position = new Vector2(200, 400),
  85. Colour = Color.Black,
  86. CollisionType = CollisionTypes.Full,
  87. },
  88. new Sprite(texture)
  89. {
  90. Position = new Vector2(200, 350),
  91. Colour = Color.Black,
  92. CollisionType = CollisionTypes.Full,
  93. },
  94. new Sprite(texture)
  95. {
  96. Position = new Vector2(250, 350),
  97. Colour = Color.Black,
  98. CollisionType = CollisionTypes.Full,
  99. },
  100. new Sprite(texture)
  101. {
  102. Position = new Vector2(300, 350),
  103. Colour = Color.Black,
  104. CollisionType = CollisionTypes.Full,
  105. },
  106. new Sprite(texture)
  107. {
  108. Position = new Vector2(350, 350),
  109. Colour = Color.Black,
  110. CollisionType = CollisionTypes.Full,
  111. },
  112. new Sprite(texture)
  113. {
  114. Position = new Vector2(400, 350),
  115. Colour = Color.Black,
  116. CollisionType = CollisionTypes.Full,
  117. },
  118. new Sprite(texture)
  119. {
  120. Position = new Vector2(450, 350),
  121. Colour = Color.Black,
  122. CollisionType = CollisionTypes.Full,
  123. },
  124. new Sprite(texture)
  125. {
  126. Position = new Vector2(500, 350),
  127. Colour = Color.Black,
  128. CollisionType = CollisionTypes.Full,
  129. },
  130. new Sprite(texture)
  131. {
  132. Position = new Vector2(550, 350),
  133. Colour = Color.Black,
  134. CollisionType = CollisionTypes.Full,
  135. },
  136. new Sprite(texture)
  137. {
  138. Position = new Vector2(600, 350),
  139. Colour = Color.Black,
  140. CollisionType = CollisionTypes.Full,
  141. },
  142. };
  143. }
  144. /// <summary>
  145. /// UnloadContent will be called once per game and is the place to unload
  146. /// game-specific content.
  147. /// </summary>
  148. protected override void UnloadContent()
  149. {
  150. // TODO: Unload any non ContentManager content here
  151. }
  152. /// <summary>
  153. /// Allows the game to run logic such as updating the world,
  154. /// checking for collisions, gathering input, and playing audio.
  155. /// </summary>
  156. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  157. protected override void Update(GameTime gameTime)
  158. {
  159. foreach (var sprite in _sprites)
  160. sprite.Update(gameTime);
  161. CheckCollision(gameTime);
  162. foreach (var sprite in _sprites)
  163. sprite.ApplyPhysics(gameTime);
  164. base.Update(gameTime);
  165. }
  166. public void CheckCollision(GameTime gameTime)
  167. {
  168. var collidableSprites = _sprites.Where(c => c.CollisionType != CollisionTypes.None);
  169. foreach (var spriteA in collidableSprites)
  170. {
  171. foreach (var spriteB in collidableSprites)
  172. {
  173. // Don't do anything if they're the same sprite!
  174. if (spriteA == spriteB)
  175. continue;
  176. if (spriteA.WillIntersect(spriteB))
  177. //if (spriteA.Rectangle.Intersects(spriteB.Rectangle))
  178. spriteA.OnCollide(spriteB);
  179. }
  180. }
  181. }
  182. /// <summary>
  183. /// This is called when the game should draw itself.
  184. /// </summary>
  185. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  186. protected override void Draw(GameTime gameTime)
  187. {
  188. GraphicsDevice.Clear(Color.CornflowerBlue);
  189. spriteBatch.Begin();
  190. foreach (var sprite in _sprites)
  191. sprite.Draw(gameTime, spriteBatch);
  192. spriteBatch.End();
  193. base.Draw(gameTime);
  194. }
  195. }
  196. }