Game1.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using Microsoft.Xna.Framework.Input;
  5. namespace Microsoft.Xna.Samples.BouncingBox
  6. {
  7. /// <summary>
  8. /// This is the main type for your game
  9. /// </summary>
  10. public class Game1 : Game
  11. {
  12. GraphicsDeviceManager graphics;
  13. SpriteBatch spriteBatch;
  14. Texture2D texture;
  15. Vector2 position;
  16. Vector2 speed;
  17. Random randomizer;
  18. Color backColor;
  19. public Game1()
  20. {
  21. randomizer = new Random(DateTime.Now.TimeOfDay.Milliseconds);
  22. speed = new Vector2(5 + randomizer.Next(10), 5 + randomizer.Next(10));
  23. position = new Vector2(250, 400);
  24. GetNewColor();
  25. graphics = new GraphicsDeviceManager(this);
  26. Content.RootDirectory = "Content";
  27. #if __MOBILE__
  28. graphics.IsFullScreen = true;
  29. #endif
  30. }
  31. private void GetNewColor ()
  32. {
  33. backColor = new Color (randomizer.Next (255), randomizer.Next (255), randomizer.Next (255), 255);
  34. }
  35. /// <summary>
  36. /// Allows the game to perform any initialization it needs to before starting to run.
  37. /// This is where it can query for any required services and load any non-graphic
  38. /// related content. Calling base.Initialize will enumerate through any components
  39. /// and initialize them as well.
  40. /// </summary>
  41. protected override void Initialize ()
  42. {
  43. // TODO: Add your initialization logic here
  44. base.Initialize ();
  45. }
  46. /// <summary>
  47. /// LoadContent will be called once per game and is the place to load
  48. /// all of your content.
  49. /// </summary>
  50. protected override void LoadContent ()
  51. {
  52. // Create a new SpriteBatch, which can be used to draw textures.
  53. spriteBatch = new SpriteBatch (GraphicsDevice);
  54. // Load the texture from the file in the Content directory
  55. try
  56. {
  57. texture = Content.Load<Texture2D> ("monogameicon");
  58. }
  59. catch
  60. {
  61. // If that fails, create a simple colored rectangle texture
  62. texture = new Texture2D(GraphicsDevice, 64, 64);
  63. Color[] data = new Color[64 * 64];
  64. for (int i = 0; i < data.Length; i++)
  65. {
  66. data[i] = Color.White;
  67. }
  68. texture.SetData(data);
  69. }
  70. }
  71. /// <summary>
  72. /// Allows the game to run logic such as updating the world,
  73. /// checking for collisions, gathering input, and playing audio.
  74. /// </summary>
  75. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  76. protected override void Update (GameTime gameTime)
  77. {
  78. KeyboardState keyState = Keyboard.GetState();
  79. // Allows the game to exit
  80. if (keyState.IsKeyDown(Keys.Escape)
  81. || GamePad.GetState(PlayerIndex.One).Buttons.Back ==
  82. ButtonState.Pressed)
  83. #if !__IOS__
  84. Exit();
  85. #endif
  86. if (texture != null) {
  87. // Keep inside the screen
  88. // right
  89. if (position.X + texture.Width + speed.X > Window.ClientBounds.Width) {
  90. GetNewColor ();
  91. speed.X = -speed.X;
  92. }
  93. // bottom
  94. if (position.Y + texture.Height + speed.Y > Window.ClientBounds.Height) {
  95. GetNewColor ();
  96. speed.Y = -speed.Y;
  97. }
  98. // left
  99. if (position.X + speed.X < 0) {
  100. GetNewColor ();
  101. speed.X = -speed.X;
  102. }
  103. // top
  104. if (position.Y + speed.Y < 0) {
  105. GetNewColor ();
  106. speed.Y = -speed.Y;
  107. }
  108. // update position
  109. position += speed;
  110. }
  111. base.Update (gameTime);
  112. }
  113. /// <summary>
  114. /// This is called when the game should draw itself.
  115. /// </summary>
  116. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  117. protected override void Draw (GameTime gameTime)
  118. {
  119. graphics.GraphicsDevice.Clear (backColor);
  120. spriteBatch.Begin ();
  121. if (texture != null)
  122. spriteBatch.Draw (texture, position, Color.White);
  123. spriteBatch.End ();
  124. base.Draw (gameTime);
  125. }
  126. }
  127. }