Game1.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Content;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using Microsoft.Xna.Framework.Input;
  7. namespace GameComponents
  8. {
  9. /// <summary>
  10. /// This is the main type for your game
  11. /// </summary>
  12. public class Game1 : Microsoft.Xna.Framework.Game
  13. {
  14. GraphicsDeviceManager graphics;
  15. SpriteBatch spriteBatch;
  16. Texture2D texture;
  17. Random randomizer;
  18. SpriteFont font;
  19. public Game1()
  20. {
  21. graphics = new GraphicsDeviceManager(this);
  22. Content.RootDirectory = "Content";
  23. IsMouseVisible = true;
  24. graphics.IsFullScreen = false;
  25. graphics.PreferredBackBufferWidth = 800;
  26. graphics.PreferredBackBufferHeight = 600;
  27. randomizer = new Random(DateTime.Now.TimeOfDay.Milliseconds);
  28. }
  29. /// <summary>
  30. /// Allows the game to perform any initialization it needs to before starting to run.
  31. /// This is where it can query for any required services and load any non-graphic
  32. /// related content. Calling base.Initialize will enumerate through any components
  33. /// and initialize them as well.
  34. /// </summary>
  35. protected override void Initialize()
  36. {
  37. // TODO: Add your initialization logic here
  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. // TODO: use this.Content to load your game content here
  49. texture = Content.Load<Texture2D>("logo");
  50. font = Content.Load<SpriteFont>("Font");
  51. for (int i=0;i<50;i++)
  52. AddSprite();
  53. Components.Add(new FPSCounterComponent(this,spriteBatch,font));
  54. }
  55. private void AddSprite()
  56. {
  57. Vector2 speed = new Vector2(5+randomizer.Next(10),5+randomizer.Next(10));
  58. Vector2 position = new Vector2(randomizer.Next(260),randomizer.Next(400));
  59. Components.Add(new Sprite(this,texture,position, speed, spriteBatch));
  60. }
  61. /// <summary>
  62. /// Allows the game to run logic such as updating the world,
  63. /// checking for collisions, gathering input, and playing audio.
  64. /// </summary>
  65. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  66. protected override void Update(GameTime gameTime)
  67. {
  68. // TODO: Add your update logic here
  69. KeyboardState currentKeyboardState = Keyboard.GetState();
  70. GamePadState currentGamePadState = GamePad.GetState (PlayerIndex.One);
  71. // Check for exit.
  72. if (currentKeyboardState.IsKeyDown(Keys.Escape) ||
  73. currentGamePadState.Buttons.Back == ButtonState.Pressed)
  74. {
  75. #if !___IOS___
  76. Exit();
  77. #endif
  78. }
  79. var mouseState = Mouse.GetState();
  80. if (mouseState.LeftButton == ButtonState.Pressed ||
  81. currentGamePadState.Buttons.A == ButtonState.Pressed ||
  82. currentKeyboardState.IsKeyDown(Keys.Space))
  83. {
  84. AddSprite();
  85. //Mouse.SetPosition(0,0);
  86. }
  87. base.Update(gameTime);
  88. }
  89. /// <summary>
  90. /// This is called when the game should draw itself.
  91. /// </summary>
  92. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  93. protected override void Draw(GameTime gameTime)
  94. {
  95. graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
  96. spriteBatch.Begin();
  97. base.Draw(gameTime);
  98. spriteBatch.DrawString(font,"Tap/Click/Space/A button to add a new sprite",new Vector2(0,25),Color.White);
  99. spriteBatch.DrawString(font,"Sprite count: " + (Components.Count-1).ToString(),new Vector2(150,0),Color.White);
  100. spriteBatch.End();
  101. }
  102. }
  103. }