Game1.cs 3.5 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.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> ("logo");
  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 (Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit();
  70. if (texture != null) {
  71. // Keep inside the screen
  72. // right
  73. if (position.X + texture.Width + speed.X > Window.ClientBounds.Width) {
  74. GetNewColor ();
  75. speed.X = -speed.X;
  76. }
  77. // bottom
  78. if (position.Y + texture.Height + speed.Y > Window.ClientBounds.Height) {
  79. GetNewColor ();
  80. speed.Y = -speed.Y;
  81. }
  82. // left
  83. if (position.X + speed.X < 0) {
  84. GetNewColor ();
  85. speed.X = -speed.X;
  86. }
  87. // top
  88. if (position.Y + speed.Y < 0) {
  89. GetNewColor ();
  90. speed.Y = -speed.Y;
  91. }
  92. // update position
  93. position += speed;
  94. }
  95. base.Update (gameTime);
  96. }
  97. /// <summary>
  98. /// This is called when the game should draw itself.
  99. /// </summary>
  100. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  101. protected override void Draw (GameTime gameTime)
  102. {
  103. graphics.GraphicsDevice.Clear (backColor);
  104. spriteBatch.Begin ();
  105. if (texture != null)
  106. spriteBatch.Draw (texture, position, Color.White);
  107. spriteBatch.End ();
  108. base.Draw (gameTime);
  109. }
  110. }
  111. }