Game1.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. namespace Tutorial002
  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 Texture2D _texture;
  14. private Vector2 _position;
  15. public Game1()
  16. {
  17. graphics = new GraphicsDeviceManager(this);
  18. Content.RootDirectory = "Content";
  19. }
  20. /// <summary>
  21. /// Allows the game to perform any initialization it needs to before starting to run.
  22. /// This is where it can query for any required services and load any non-graphic
  23. /// related content. Calling base.Initialize will enumerate through any components
  24. /// and initialize them as well.
  25. /// </summary>
  26. protected override void Initialize()
  27. {
  28. // TODO: Add your initialization logic here
  29. base.Initialize();
  30. }
  31. /// <summary>
  32. /// LoadContent will be called once per game and is the place to load
  33. /// all of your content.
  34. /// </summary>
  35. protected override void LoadContent()
  36. {
  37. // Create a new SpriteBatch, which can be used to draw textures.
  38. spriteBatch = new SpriteBatch(GraphicsDevice);
  39. _texture = Content.Load<Texture2D>("Box");
  40. _position = new Vector2(0, 0);
  41. }
  42. /// <summary>
  43. /// UnloadContent will be called once per game and is the place to unload
  44. /// game-specific content.
  45. /// </summary>
  46. protected override void UnloadContent()
  47. {
  48. // TODO: Unload any non ContentManager content here
  49. }
  50. /// <summary>
  51. /// Allows the game to run logic such as updating the world,
  52. /// checking for collisions, gathering input, and playing audio.
  53. /// </summary>
  54. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  55. protected override void Update(GameTime gameTime)
  56. {
  57. if (Keyboard.GetState().IsKeyDown(Keys.W))
  58. {
  59. _position.Y -= 1;
  60. }
  61. if (Keyboard.GetState().IsKeyDown(Keys.S))
  62. {
  63. _position.Y += 1;
  64. }
  65. if (Keyboard.GetState().IsKeyDown(Keys.A))
  66. {
  67. _position.X -= 1;
  68. }
  69. if (Keyboard.GetState().IsKeyDown(Keys.D))
  70. {
  71. _position.X += 1;
  72. }
  73. base.Update(gameTime);
  74. }
  75. /// <summary>
  76. /// This is called when the game should draw itself.
  77. /// </summary>
  78. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  79. protected override void Draw(GameTime gameTime)
  80. {
  81. GraphicsDevice.Clear(Color.CornflowerBlue);
  82. spriteBatch.Begin();
  83. spriteBatch.Draw(_texture, _position, Color.White);
  84. spriteBatch.End();
  85. base.Draw(gameTime);
  86. }
  87. }
  88. }