Game1.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 Tutorial010.Models;
  7. using Tutorial010.Sprites;
  8. namespace Tutorial010
  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 int ScreenWidth;
  18. public static int ScreenHeight;
  19. public static Random Random;
  20. private Score _score;
  21. private List<Sprite> _sprites;
  22. public Game1()
  23. {
  24. graphics = new GraphicsDeviceManager(this);
  25. Content.RootDirectory = "Content";
  26. }
  27. /// <summary>
  28. /// Allows the game to perform any initialization it needs to before starting to run.
  29. /// This is where it can query for any required services and load any non-graphic
  30. /// related content. Calling base.Initialize will enumerate through any components
  31. /// and initialize them as well.
  32. /// </summary>
  33. protected override void Initialize()
  34. {
  35. ScreenWidth = graphics.PreferredBackBufferWidth;
  36. ScreenHeight = graphics.PreferredBackBufferHeight;
  37. Random = new Random();
  38. base.Initialize();
  39. }
  40. /// <summary>
  41. /// LoadContent will be called once per game and is the place to load
  42. /// all of your content.
  43. /// </summary>
  44. protected override void LoadContent()
  45. {
  46. // Create a new SpriteBatch, which can be used to draw textures.
  47. spriteBatch = new SpriteBatch(GraphicsDevice);
  48. var batTexture = Content.Load<Texture2D>("Bat");
  49. var ballTexture = Content.Load<Texture2D>("Ball");
  50. _score = new Score(Content.Load<SpriteFont>("Font"));
  51. _sprites = new List<Sprite>()
  52. {
  53. new Sprite(Content.Load<Texture2D>("Background")),
  54. new Bat(batTexture)
  55. {
  56. Position = new Vector2(20, (ScreenHeight / 2) - (batTexture.Height / 2)),
  57. Input = new Input()
  58. {
  59. Up = Keys.W,
  60. Down = Keys.S,
  61. }
  62. },
  63. new Bat(batTexture)
  64. {
  65. Position = new Vector2(ScreenWidth - 20 - batTexture.Width, (ScreenHeight / 2) - (batTexture.Height / 2)),
  66. Input = new Input()
  67. {
  68. Up = Keys.Up,
  69. Down = Keys.Down,
  70. }
  71. },
  72. new Ball(ballTexture)
  73. {
  74. Position = new Vector2((ScreenWidth / 2) - (ballTexture.Width / 2), (ScreenHeight / 2) - (ballTexture.Height / 2)),
  75. Score = _score,
  76. }
  77. };
  78. }
  79. /// <summary>
  80. /// UnloadContent will be called once per game and is the place to unload
  81. /// game-specific content.
  82. /// </summary>
  83. protected override void UnloadContent()
  84. {
  85. // TODO: Unload any non ContentManager content here
  86. }
  87. /// <summary>
  88. /// Allows the game to run logic such as updating the world,
  89. /// checking for collisions, gathering input, and playing audio.
  90. /// </summary>
  91. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  92. protected override void Update(GameTime gameTime)
  93. {
  94. foreach(var sprite in _sprites)
  95. {
  96. sprite.Update(gameTime, _sprites);
  97. }
  98. base.Update(gameTime);
  99. }
  100. /// <summary>
  101. /// This is called when the game should draw itself.
  102. /// </summary>
  103. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  104. protected override void Draw(GameTime gameTime)
  105. {
  106. GraphicsDevice.Clear(Color.CornflowerBlue);
  107. spriteBatch.Begin();
  108. foreach (var sprite in _sprites)
  109. sprite.Draw(spriteBatch);
  110. _score.Draw(spriteBatch);
  111. spriteBatch.End();
  112. base.Draw(gameTime);
  113. }
  114. }
  115. }