Game1.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System;
  5. using System.Collections.Generic;
  6. using Tutorial007.Models;
  7. using Tutorial007.Sprites;
  8. namespace Tutorial007
  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. public static Random Random;
  18. public static int ScreenWidth = 1280;
  19. public static int ScreenHeight = 720;
  20. private List<Sprite> _sprites;
  21. private float _timer;
  22. private bool _hasStarted = false;
  23. public Game1()
  24. {
  25. graphics = new GraphicsDeviceManager(this);
  26. Content.RootDirectory = "Content";
  27. Random = new Random();
  28. ScreenWidth = graphics.PreferredBackBufferWidth;
  29. ScreenHeight = graphics.PreferredBackBufferHeight;
  30. }
  31. /// <summary>
  32. /// Allows the game to perform any initialization it needs to before starting to run.
  33. /// This is where it can query for any required services and load any non-graphic
  34. /// related content. Calling base.Initialize will enumerate through any components
  35. /// and initialize them as well.
  36. /// </summary>
  37. protected override void Initialize()
  38. {
  39. base.Initialize();
  40. }
  41. /// <summary>
  42. /// LoadContent will be called once per game and is the place to load
  43. /// all of your content.
  44. /// </summary>
  45. protected override void LoadContent()
  46. {
  47. // Create a new SpriteBatch, which can be used to draw textures.
  48. spriteBatch = new SpriteBatch(GraphicsDevice);
  49. Restart();
  50. }
  51. private void Restart()
  52. {
  53. var playerTexture = Content.Load<Texture2D>("Player");
  54. _sprites = new List<Sprite>()
  55. {
  56. new Player(playerTexture)
  57. {
  58. Position = new Vector2((ScreenWidth / 2) - (playerTexture.Width / 2), ScreenHeight - playerTexture.Height),
  59. Input = new Input()
  60. {
  61. Left = Keys.A,
  62. Right = Keys.D,
  63. },
  64. Speed = 10f,
  65. }
  66. };
  67. _hasStarted = false;
  68. }
  69. /// <summary>
  70. /// UnloadContent will be called once per game and is the place to unload
  71. /// game-specific content.
  72. /// </summary>
  73. protected override void UnloadContent()
  74. {
  75. // TODO: Unload any non ContentManager content here
  76. }
  77. /// <summary>
  78. /// Allows the game to run logic such as updating the world,
  79. /// checking for collisions, gathering input, and playing audio.
  80. /// </summary>
  81. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  82. protected override void Update(GameTime gameTime)
  83. {
  84. if (Keyboard.GetState().IsKeyDown(Keys.Space))
  85. _hasStarted = true;
  86. if (!_hasStarted)
  87. return;
  88. _timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
  89. foreach (var sprite in _sprites)
  90. sprite.Update(gameTime, _sprites);
  91. if (_timer > 0.25f)
  92. {
  93. _timer = 0f;
  94. _sprites.Add(new Bomb(Content.Load<Texture2D>("Bomb")));
  95. }
  96. for (int i = 0; i < _sprites.Count; i++)
  97. {
  98. var sprite = _sprites[i];
  99. if (sprite.IsRemoved)
  100. {
  101. _sprites.RemoveAt(i);
  102. i--;
  103. }
  104. if (sprite is Player)
  105. {
  106. var player = sprite as Player;
  107. if (player.HasDied)
  108. {
  109. Restart();
  110. }
  111. }
  112. }
  113. base.Update(gameTime);
  114. }
  115. /// <summary>
  116. /// This is called when the game should draw itself.
  117. /// </summary>
  118. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  119. protected override void Draw(GameTime gameTime)
  120. {
  121. GraphicsDevice.Clear(Color.CornflowerBlue);
  122. spriteBatch.Begin();
  123. foreach (var sprite in _sprites)
  124. sprite.Draw(spriteBatch);
  125. spriteBatch.End();
  126. base.Draw(gameTime);
  127. }
  128. }
  129. }