2
0

Game1.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. using Microsoft.Xna.Framework.Audio;
  10. using Microsoft.Xna.Framework.Media;
  11. namespace Microsoft.Xna.Samples.BouncingBox
  12. {
  13. /// <summary>
  14. /// This is the main type for your game
  15. /// </summary>
  16. public class Game1 : Microsoft.Xna.Framework.Game
  17. {
  18. GraphicsDeviceManager graphics;
  19. SpriteBatch spriteBatch;
  20. Texture2D texture;
  21. Vector2 position;
  22. Vector2 speed;
  23. Random randomizer;
  24. Color backColor;
  25. public Game1 ()
  26. {
  27. randomizer = new Random (DateTime.Now.TimeOfDay.Milliseconds);
  28. speed = new Vector2 (5 + randomizer.Next (10),5 + randomizer.Next (10));
  29. position = new Vector2 (250,400);
  30. GetNewColor ();
  31. graphics = new GraphicsDeviceManager (this);
  32. Content.RootDirectory = "Content";
  33. graphics.IsFullScreen = true;
  34. }
  35. private void GetNewColor ()
  36. {
  37. backColor = new Color ((byte)randomizer.Next (255),(byte)randomizer.Next (255),(byte)randomizer.Next (255),255);
  38. }
  39. /// <summary>
  40. /// Allows the game to perform any initialization it needs to before starting to run.
  41. /// This is where it can query for any required services and load any non-graphic
  42. /// related content. Calling base.Initialize will enumerate through any components
  43. /// and initialize them as well.
  44. /// </summary>
  45. protected override void Initialize ()
  46. {
  47. // TODO: Add your initialization logic here
  48. base.Initialize ();
  49. }
  50. /// <summary>
  51. /// LoadContent will be called once per game and is the place to load
  52. /// all of your content.
  53. /// </summary>
  54. protected override void LoadContent ()
  55. {
  56. // Create a new SpriteBatch, which can be used to draw textures.
  57. spriteBatch = new SpriteBatch (GraphicsDevice);
  58. // TODO: use this.Content to load your game content here
  59. texture = Content.Load<Texture2D> ("monogameicon");
  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. if (texture != null) {
  70. // Keep inside the screen
  71. // right
  72. if (position.X + texture.Width + speed.X > Window.ClientBounds.Width) {
  73. GetNewColor ();
  74. speed.X = -speed.X;
  75. }
  76. // bottom
  77. if (position.Y + texture.Height + speed.Y > Window.ClientBounds.Height) {
  78. GetNewColor ();
  79. speed.Y = -speed.Y;
  80. }
  81. // left
  82. if (position.X + speed.X < 0) {
  83. GetNewColor ();
  84. speed.X = -speed.X;
  85. }
  86. // top
  87. if (position.Y + speed.Y < 0) {
  88. GetNewColor ();
  89. speed.Y = -speed.Y;
  90. }
  91. // update position
  92. position += speed;
  93. }
  94. base.Update (gameTime);
  95. }
  96. /// <summary>
  97. /// This is called when the game should draw itself.
  98. /// </summary>
  99. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  100. protected override void Draw (GameTime gameTime)
  101. {
  102. graphics.GraphicsDevice.Clear (backColor);
  103. spriteBatch.Begin ();
  104. if (texture != null)
  105. spriteBatch.Draw (texture, position, Color.White);
  106. spriteBatch.End ();
  107. base.Draw (gameTime);
  108. }
  109. }
  110. }