Sprite.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 Tutorial004
  10. {
  11. public class Sprite
  12. {
  13. private Texture2D _texture;
  14. public Vector2 Position;
  15. public float Speed = 2f;
  16. public Input Input;
  17. public Sprite(Texture2D texture)
  18. {
  19. _texture = texture;
  20. }
  21. public void Update()
  22. {
  23. Move();
  24. }
  25. public void Move()
  26. {
  27. if (Input == null)
  28. return;
  29. if (Keyboard.GetState().IsKeyDown(Input.Left))
  30. {
  31. Position.X -= Speed;
  32. }
  33. if (Keyboard.GetState().IsKeyDown(Input.Right))
  34. {
  35. Position.X += Speed;
  36. }
  37. if (Keyboard.GetState().IsKeyDown(Input.Up))
  38. {
  39. Position.Y -= Speed;
  40. }
  41. if (Keyboard.GetState().IsKeyDown(Input.Down))
  42. {
  43. Position.Y += Speed;
  44. }
  45. }
  46. public void Draw(SpriteBatch spriteBatch)
  47. {
  48. spriteBatch.Draw(_texture, Position, Color.White);
  49. }
  50. }
  51. }