Game1.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 Tutorial012.Controls;
  7. namespace Tutorial012
  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 Color _backgroundColour = Color.CornflowerBlue;
  17. private List<Component> _gameComponents;
  18. public Game1()
  19. {
  20. graphics = new GraphicsDeviceManager(this);
  21. Content.RootDirectory = "Content";
  22. }
  23. /// <summary>
  24. /// Allows the game to perform any initialization it needs to before starting to run.
  25. /// This is where it can query for any required services and load any non-graphic
  26. /// related content. Calling base.Initialize will enumerate through any components
  27. /// and initialize them as well.
  28. /// </summary>
  29. protected override void Initialize()
  30. {
  31. IsMouseVisible = true;
  32. base.Initialize();
  33. }
  34. /// <summary>
  35. /// LoadContent will be called once per game and is the place to load
  36. /// all of your content.
  37. /// </summary>
  38. protected override void LoadContent()
  39. {
  40. // Create a new SpriteBatch, which can be used to draw textures.
  41. spriteBatch = new SpriteBatch(GraphicsDevice);
  42. var randomButton = new Button(Content.Load<Texture2D>("Controls/Button"), Content.Load<SpriteFont>("Fonts/Font"))
  43. {
  44. Position = new Vector2(350, 200),
  45. Text = "Random",
  46. };
  47. randomButton.Click += RandomButton_Click;
  48. var quitButton = new Button(Content.Load<Texture2D>("Controls/Button"), Content.Load<SpriteFont>("Fonts/Font"))
  49. {
  50. Position = new Vector2(350, 250),
  51. Text = "Quit",
  52. };
  53. quitButton.Click += QuitButton_Click;
  54. _gameComponents = new List<Component>()
  55. {
  56. randomButton,
  57. quitButton,
  58. };
  59. }
  60. private void QuitButton_Click(object sender, System.EventArgs e)
  61. {
  62. Exit();
  63. }
  64. private void RandomButton_Click(object sender, System.EventArgs e)
  65. {
  66. var random = new Random();
  67. _backgroundColour = new Color(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
  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. foreach (var component in _gameComponents)
  85. component.Update(gameTime);
  86. base.Update(gameTime);
  87. }
  88. /// <summary>
  89. /// This is called when the game should draw itself.
  90. /// </summary>
  91. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  92. protected override void Draw(GameTime gameTime)
  93. {
  94. GraphicsDevice.Clear(_backgroundColour);
  95. spriteBatch.Begin();
  96. foreach (var component in _gameComponents)
  97. component.Draw(gameTime, spriteBatch);
  98. spriteBatch.End();
  99. base.Draw(gameTime);
  100. }
  101. }
  102. }