Game1.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 Tutorial020.Sprites;
  7. using Tutorial020.States;
  8. namespace Tutorial020
  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 State _currentState;
  21. private State _nextState;
  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. Random = new Random();
  36. graphics.PreferredBackBufferWidth = ScreenWidth;
  37. graphics.PreferredBackBufferHeight = ScreenHeight;
  38. graphics.ApplyChanges();
  39. IsMouseVisible = true;
  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. _currentState = new MenuState(this, Content);
  51. _currentState.LoadContent();
  52. _nextState = null;
  53. }
  54. /// <summary>
  55. /// UnloadContent will be called once per game and is the place to unload
  56. /// game-specific content.
  57. /// </summary>
  58. protected override void UnloadContent()
  59. {
  60. // TODO: Unload any non ContentManager content here
  61. }
  62. /// <summary>
  63. /// Allows the game to run logic such as updating the world,
  64. /// checking for collisions, gathering input, and playing audio.
  65. /// </summary>
  66. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  67. protected override void Update(GameTime gameTime)
  68. {
  69. if(_nextState != null)
  70. {
  71. _currentState = _nextState;
  72. _currentState.LoadContent();
  73. _nextState = null;
  74. }
  75. _currentState.Update(gameTime);
  76. _currentState.PostUpdate(gameTime);
  77. base.Update(gameTime);
  78. }
  79. public void ChangeState(State state)
  80. {
  81. _nextState = state;
  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(new Color(55, 55, 55));
  90. _currentState.Draw(gameTime, spriteBatch);
  91. base.Draw(gameTime);
  92. }
  93. }
  94. }