Game1.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. namespace Tutorial016
  5. {
  6. /// <summary>
  7. /// This is the main type for your game.
  8. /// </summary>
  9. public class Game1 : Game
  10. {
  11. GraphicsDeviceManager graphics;
  12. SpriteBatch spriteBatch;
  13. private Vector2 _position1;
  14. private Vector2 _position2;
  15. private Texture2D _texture;
  16. public Game1()
  17. {
  18. graphics = new GraphicsDeviceManager(this);
  19. Content.RootDirectory = "Content";
  20. }
  21. /// <summary>
  22. /// Allows the game to perform any initialization it needs to before starting to run.
  23. /// This is where it can query for any required services and load any non-graphic
  24. /// related content. Calling base.Initialize will enumerate through any components
  25. /// and initialize them as well.
  26. /// </summary>
  27. protected override void Initialize()
  28. {
  29. // TODO: Add your initialization logic here
  30. base.Initialize();
  31. }
  32. /// <summary>
  33. /// LoadContent will be called once per game and is the place to load
  34. /// all of your content.
  35. /// </summary>
  36. protected override void LoadContent()
  37. {
  38. // Create a new SpriteBatch, which can be used to draw textures.
  39. spriteBatch = new SpriteBatch(GraphicsDevice);
  40. _position1 = new Vector2(100, 100);
  41. _position2 = new Vector2(125, 100);
  42. _texture = Content.Load<Texture2D>("Square");
  43. }
  44. /// <summary>
  45. /// UnloadContent will be called once per game and is the place to unload
  46. /// game-specific content.
  47. /// </summary>
  48. protected override void UnloadContent()
  49. {
  50. // TODO: Unload any non ContentManager content here
  51. }
  52. /// <summary>
  53. /// Allows the game to run logic such as updating the world,
  54. /// checking for collisions, gathering input, and playing audio.
  55. /// </summary>
  56. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  57. protected override void Update(GameTime gameTime)
  58. {
  59. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  60. Exit();
  61. // TODO: Add your update logic here
  62. base.Update(gameTime);
  63. }
  64. /// <summary>
  65. /// This is called when the game should draw itself.
  66. /// </summary>
  67. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  68. protected override void Draw(GameTime gameTime)
  69. {
  70. GraphicsDevice.Clear(Color.CornflowerBlue);
  71. spriteBatch.Begin(SpriteSortMode.BackToFront);
  72. spriteBatch.Draw(_texture, _position2, null, Color.Blue, 0f, new Vector2(0, 0), 1f, SpriteEffects.None, 0.1f);
  73. spriteBatch.Draw(_texture, _position1, null, Color.Red, 0f, new Vector2(0, 0), 1f, SpriteEffects.None, 0.2f);
  74. spriteBatch.End();
  75. base.Draw(gameTime);
  76. }
  77. }
  78. }