Game1.cs 3.1 KB

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