Game1.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System;
  5. using System.Linq;
  6. using Tutorial015.Controls;
  7. using Tutorial015.Managers;
  8. namespace Tutorial015
  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. private Button _button;
  18. private SpriteFont _font;
  19. private int _score;
  20. private ScoreManager _scoreManager;
  21. private float _timer;
  22. public static Random Random;
  23. public Game1()
  24. {
  25. graphics = new GraphicsDeviceManager(this);
  26. Content.RootDirectory = "Content";
  27. }
  28. /// <summary>
  29. /// Allows the game to perform any initialization it needs to before starting to run.
  30. /// This is where it can query for any required services and load any non-graphic
  31. /// related content. Calling base.Initialize will enumerate through any components
  32. /// and initialize them as well.
  33. /// </summary>
  34. protected override void Initialize()
  35. {
  36. Random = new Random();
  37. IsMouseVisible = true;
  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. _scoreManager = ScoreManager.Load();
  49. _font = Content.Load<SpriteFont>("Font");
  50. _button = new Button(Content.Load<Texture2D>("Button"), _font)
  51. {
  52. Text = "Click me!",
  53. };
  54. _button.Click += Button_Click;
  55. SetButtonPosition(_button);
  56. _timer = 5; // 5 seconds a round
  57. }
  58. private void Button_Click(object sender, EventArgs e)
  59. {
  60. SetButtonPosition((Button)sender);
  61. _score++;
  62. }
  63. private void SetButtonPosition(Button button)
  64. {
  65. var x = Random.Next(0, graphics.PreferredBackBufferWidth - button.Rectangle.Width);
  66. var y = Random.Next(0, graphics.PreferredBackBufferHeight - button.Rectangle.Height);
  67. button.Position = new Vector2(x, y);
  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. _timer -= (float)gameTime.ElapsedGameTime.TotalSeconds;
  85. // If the round is over
  86. if (_timer <= 0)
  87. {
  88. SetButtonPosition(_button);
  89. _scoreManager.Add(new Models.Score()
  90. {
  91. PlayerName = "Bob",
  92. Value = _score,
  93. }
  94. );
  95. ScoreManager.Save(_scoreManager);
  96. _timer = 5;
  97. _score = 0;
  98. }
  99. _button.Update(gameTime);
  100. base.Update(gameTime);
  101. }
  102. /// <summary>
  103. /// This is called when the game should draw itself.
  104. /// </summary>
  105. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  106. protected override void Draw(GameTime gameTime)
  107. {
  108. GraphicsDevice.Clear(Color.CornflowerBlue);
  109. spriteBatch.Begin();
  110. _button.Draw(gameTime, spriteBatch);
  111. spriteBatch.DrawString(_font, "Score: " + _score, new Vector2(10, 10), Color.Red);
  112. spriteBatch.DrawString(_font, "Time: " + _timer.ToString("N2"), new Vector2(10, 30), Color.Red);
  113. spriteBatch.DrawString(_font, "Highscores:\n" + string.Join("\n", _scoreManager.Highscores.Select(c => c.PlayerName + ": "+ c.Value).ToArray()), new Vector2(680, 10), Color.Red);
  114. spriteBatch.End();
  115. base.Draw(gameTime);
  116. }
  117. }
  118. }