Game1.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.Collections.ObjectModel;
  6. using Tutorial018.Sprites;
  7. namespace Tutorial018
  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 KeyboardState _currentKey;
  17. private KeyboardState _previousKey;
  18. private bool _showBorders = false;
  19. private List<Sprite> _sprites;
  20. public Game1()
  21. {
  22. graphics = new GraphicsDeviceManager(this);
  23. Content.RootDirectory = "Content";
  24. }
  25. /// <summary>
  26. /// Allows the game to perform any initialization it needs to before starting to run.
  27. /// This is where it can query for any required services and load any non-graphic
  28. /// related content. Calling base.Initialize will enumerate through any components
  29. /// and initialize them as well.
  30. /// </summary>
  31. protected override void Initialize()
  32. {
  33. // TODO: Add your initialization logic here
  34. base.Initialize();
  35. }
  36. /// <summary>
  37. /// LoadContent will be called once per game and is the place to load
  38. /// all of your content.
  39. /// </summary>
  40. protected override void LoadContent()
  41. {
  42. // Create a new SpriteBatch, which can be used to draw textures.
  43. spriteBatch = new SpriteBatch(GraphicsDevice);
  44. _sprites = new List<Sprite>
  45. {
  46. new Sprite(graphics.GraphicsDevice, Content.Load<Texture2D>("Square"))
  47. {
  48. Position = new Vector2(100, 100),
  49. },
  50. new Player(graphics.GraphicsDevice, Content.Load<Texture2D>("Apple"))
  51. {
  52. Position = new Vector2(200, 100),
  53. },
  54. new Sprite(graphics.GraphicsDevice, Content.Load<Texture2D>("Bomb"))
  55. {
  56. Position = new Vector2(200, 200),
  57. }
  58. };
  59. }
  60. /// <summary>
  61. /// UnloadContent will be called once per game and is the place to unload
  62. /// game-specific content.
  63. /// </summary>
  64. protected override void UnloadContent()
  65. {
  66. // TODO: Unload any non ContentManager content here
  67. }
  68. /// <summary>
  69. /// Allows the game to run logic such as updating the world,
  70. /// checking for collisions, gathering input, and playing audio.
  71. /// </summary>
  72. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  73. protected override void Update(GameTime gameTime)
  74. {
  75. _previousKey = _currentKey;
  76. _currentKey = Keyboard.GetState();
  77. if (_currentKey.IsKeyUp(Keys.F1) && _previousKey.IsKeyDown(Keys.F1))
  78. _showBorders = !_showBorders;
  79. foreach (var sprite in _sprites)
  80. sprite.Update(gameTime);
  81. base.Update(gameTime);
  82. }
  83. /// <summary>
  84. /// This is called when the game should draw itself.
  85. /// </summary>
  86. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  87. protected override void Draw(GameTime gameTime)
  88. {
  89. GraphicsDevice.Clear(Color.CornflowerBlue);
  90. spriteBatch.Begin();
  91. foreach (var sprite in _sprites)
  92. {
  93. sprite.ShowRectangle = _showBorders;
  94. sprite.Draw(gameTime, spriteBatch);
  95. }
  96. spriteBatch.End();
  97. base.Draw(gameTime);
  98. }
  99. }
  100. }