Game1.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Content;
  5. using Microsoft.Xna.Framework.GamerServices;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using Microsoft.Xna.Framework.Input;
  8. using Microsoft.Xna.Framework.Storage;
  9. namespace Microsoft.Xna.Samples.GameComponents
  10. {
  11. /// <summary>
  12. /// This is the main type for your game
  13. /// </summary>
  14. public class Game1 : Microsoft.Xna.Framework.Game
  15. {
  16. GraphicsDeviceManager graphics;
  17. SpriteBatch spriteBatch;
  18. Texture2D texture;
  19. Random randomizer;
  20. SpriteFont font;
  21. public Game1()
  22. {
  23. graphics = new GraphicsDeviceManager(this);
  24. Content.RootDirectory = "Content";
  25. graphics.IsFullScreen = true;
  26. randomizer = new Random(DateTime.Now.TimeOfDay.Milliseconds);
  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. // TODO: Add your initialization logic here
  37. base.Initialize();
  38. }
  39. /// <summary>
  40. /// LoadContent will be called once per game and is the place to load
  41. /// all of your content.
  42. /// </summary>
  43. protected override void LoadContent()
  44. {
  45. // Create a new SpriteBatch, which can be used to draw textures.
  46. spriteBatch = new SpriteBatch(GraphicsDevice);
  47. // TODO: use this.Content to load your game content here
  48. texture = Content.Load<Texture2D>("monogameicon");
  49. font = Content.Load<SpriteFont>("font");
  50. for (int i=0;i<50;i++)
  51. AddSprite();
  52. Components.Add(new FPSCounterComponent(this,spriteBatch,font));
  53. }
  54. private void AddSprite()
  55. {
  56. Vector2 speed = new Vector2(5+randomizer.Next(10),5+randomizer.Next(10));
  57. Vector2 position = new Vector2(randomizer.Next(260),randomizer.Next(400));
  58. Components.Add(new Sprite(this,texture,position, speed, spriteBatch));
  59. }
  60. /// <summary>
  61. /// Allows the game to run logic such as updating the world,
  62. /// checking for collisions, gathering input, and playing audio.
  63. /// </summary>
  64. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  65. protected override void Update(GameTime gameTime)
  66. {
  67. // TODO: Add your update logic here
  68. if ((Mouse.GetState().X != 0) || (Mouse.GetState().Y != 0))
  69. {
  70. AddSprite();
  71. //Mouse.SetPosition(0,0);
  72. }
  73. base.Update(gameTime);
  74. }
  75. /// <summary>
  76. /// This is called when the game should draw itself.
  77. /// </summary>
  78. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  79. protected override void Draw(GameTime gameTime)
  80. {
  81. graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
  82. spriteBatch.Begin();
  83. base.Draw(gameTime);
  84. spriteBatch.DrawString(font,"Tap to add a new sprite",new Vector2(0,25),Color.White);
  85. spriteBatch.DrawString(font,"Sprite count: " + (Components.Count-1).ToString(),new Vector2(150,0),Color.White);
  86. spriteBatch.End();
  87. }
  88. }
  89. }