Game1.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 Tutorial008.Models;
  7. using Tutorial008.Sprites;
  8. namespace Tutorial008
  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;
  19. public static int ScreenHeight;
  20. private List<Sprite> _sprites;
  21. private SpriteFont _font;
  22. private float _timer;
  23. private Texture2D _appleTexture;
  24. public Game1()
  25. {
  26. graphics = new GraphicsDeviceManager(this);
  27. Content.RootDirectory = "Content";
  28. Random = new Random();
  29. ScreenWidth = graphics.PreferredBackBufferWidth;
  30. ScreenHeight = graphics.PreferredBackBufferHeight;
  31. }
  32. /// <summary>
  33. /// Allows the game to perform any initialization it needs to before starting to run.
  34. /// This is where it can query for any required services and load any non-graphic
  35. /// related content. Calling base.Initialize will enumerate through any components
  36. /// and initialize them as well.
  37. /// </summary>
  38. protected override void Initialize()
  39. {
  40. base.Initialize();
  41. }
  42. /// <summary>
  43. /// LoadContent will be called once per game and is the place to load
  44. /// all of your content.
  45. /// </summary>
  46. protected override void LoadContent()
  47. {
  48. // Create a new SpriteBatch, which can be used to draw textures.
  49. spriteBatch = new SpriteBatch(GraphicsDevice);
  50. var playerTexture = Content.Load<Texture2D>("Player");
  51. _sprites = new List<Sprite>()
  52. {
  53. new Player(playerTexture)
  54. {
  55. Input = new Input()
  56. {
  57. Left = Keys.A,
  58. Right = Keys.D,
  59. Up = Keys.W,
  60. Down = Keys.S,
  61. },
  62. Position = new Vector2(100, 100),
  63. Colour = Color.Blue,
  64. Speed = 5f,
  65. },
  66. new Player(playerTexture)
  67. {
  68. Input = new Input()
  69. {
  70. Left = Keys.Left,
  71. Right = Keys.Right,
  72. Up = Keys.Up,
  73. Down = Keys.Down,
  74. },
  75. Position = new Vector2(ScreenWidth - 100 - playerTexture.Width, 100),
  76. Colour = Color.Green,
  77. Speed = 5f,
  78. },
  79. };
  80. _font = Content.Load<SpriteFont>("Font");
  81. _appleTexture = Content.Load<Texture2D>("Apple");
  82. }
  83. /// <summary>
  84. /// UnloadContent will be called once per game and is the place to unload
  85. /// game-specific content.
  86. /// </summary>
  87. protected override void UnloadContent()
  88. {
  89. // TODO: Unload any non ContentManager content here
  90. }
  91. /// <summary>
  92. /// Allows the game to run logic such as updating the world,
  93. /// checking for collisions, gathering input, and playing audio.
  94. /// </summary>
  95. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  96. protected override void Update(GameTime gameTime)
  97. {
  98. _timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
  99. foreach (var sprite in _sprites)
  100. sprite.Update(gameTime, _sprites);
  101. PostUpdate();
  102. SpawnApple();
  103. base.Update(gameTime);
  104. }
  105. private void SpawnApple()
  106. {
  107. if (_timer > 1)
  108. {
  109. _timer = 0;
  110. var xPos = Random.Next(0, ScreenWidth - _appleTexture.Width);
  111. var yPos = Random.Next(0, ScreenHeight - _appleTexture.Height);
  112. _sprites.Add(new Sprite(_appleTexture)
  113. {
  114. Position = new Vector2(xPos, yPos),
  115. });
  116. }
  117. }
  118. private void PostUpdate()
  119. {
  120. for (int i = 0; i < _sprites.Count; i++)
  121. {
  122. if (_sprites[i].IsRemoved)
  123. {
  124. _sprites.RemoveAt(i);
  125. i--;
  126. }
  127. }
  128. }
  129. /// <summary>
  130. /// This is called when the game should draw itself.
  131. /// </summary>
  132. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  133. protected override void Draw(GameTime gameTime)
  134. {
  135. GraphicsDevice.Clear(Color.CornflowerBlue);
  136. spriteBatch.Begin();
  137. foreach (var sprite in _sprites)
  138. sprite.Draw(spriteBatch);
  139. var fontY = 10;
  140. var i = 0;
  141. foreach(var sprite in _sprites)
  142. {
  143. if (sprite is Player)
  144. spriteBatch.DrawString(_font, string.Format("Player {0}: {1}", ++i, ((Player)sprite).Score), new Vector2(10, fontY += 20), Color.Black);
  145. }
  146. spriteBatch.End();
  147. base.Draw(gameTime);
  148. }
  149. }
  150. }