Sprite.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace Tutorial003
  10. {
  11. public class Sprite
  12. {
  13. private Texture2D _texture;
  14. public Vector2 Position;
  15. public float Speed = 2f;
  16. public Sprite(Texture2D texture)
  17. {
  18. _texture = texture;
  19. }
  20. public void Update()
  21. {
  22. if (Keyboard.GetState().IsKeyDown(Keys.W))
  23. {
  24. Position.Y -= Speed;
  25. }
  26. if (Keyboard.GetState().IsKeyDown(Keys.S))
  27. {
  28. Position.Y += Speed;
  29. }
  30. if (Keyboard.GetState().IsKeyDown(Keys.A))
  31. {
  32. Position.X -= Speed;
  33. }
  34. if (Keyboard.GetState().IsKeyDown(Keys.D))
  35. {
  36. Position.X += Speed;
  37. }
  38. }
  39. public void Draw(SpriteBatch spriteBatch)
  40. {
  41. spriteBatch.Draw(_texture, Position, Color.White);
  42. }
  43. }
  44. }