Player.cs 847 B

123456789101112131415161718192021222324252627282930313233343536373839
  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 Tutorial014.Sprites
  10. {
  11. public class Player : Sprite
  12. {
  13. public Player(Texture2D texture)
  14. : base(texture)
  15. {
  16. }
  17. public override void Update(GameTime gameTime)
  18. {
  19. var velocity = new Vector2();
  20. var speed = 3f;
  21. if (Keyboard.GetState().IsKeyDown(Keys.W))
  22. velocity.Y = -speed;
  23. else if (Keyboard.GetState().IsKeyDown(Keys.S))
  24. velocity.Y = speed;
  25. if (Keyboard.GetState().IsKeyDown(Keys.A))
  26. velocity.X = -speed;
  27. else if (Keyboard.GetState().IsKeyDown(Keys.D))
  28. velocity.X = speed;
  29. Position += velocity;
  30. }
  31. }
  32. }