Player.cs 894 B

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Graphics;
  8. using Microsoft.Xna.Framework.Input;
  9. namespace Tutorial022.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 = Vector2.Zero;
  20. if (Keyboard.GetState().IsKeyDown(Keys.W))
  21. velocity.Y = -LinearVelocity;
  22. else if (Keyboard.GetState().IsKeyDown(Keys.S))
  23. velocity.Y = LinearVelocity;
  24. if (Keyboard.GetState().IsKeyDown(Keys.A))
  25. velocity.X = -LinearVelocity;
  26. else if (Keyboard.GetState().IsKeyDown(Keys.D))
  27. velocity.X = LinearVelocity;
  28. Position += velocity;
  29. }
  30. }
  31. }