Player.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 Tutorial008.Sprites
  10. {
  11. public class Player : Sprite
  12. {
  13. public int Score;
  14. public Player(Texture2D texture)
  15. : base(texture)
  16. {
  17. }
  18. public override void Update(GameTime gameTime, List<Sprite> sprites)
  19. {
  20. Move();
  21. foreach(var sprite in sprites)
  22. {
  23. if (sprite is Player)
  24. continue;
  25. if(sprite.Rectangle.Intersects(this.Rectangle))
  26. {
  27. Score++;
  28. sprite.IsRemoved = true;
  29. }
  30. }
  31. }
  32. private void Move()
  33. {
  34. if (Keyboard.GetState().IsKeyDown(Input.Left))
  35. Position.X -= Speed;
  36. else if (Keyboard.GetState().IsKeyDown(Input.Right))
  37. Position.X += Speed;
  38. if (Keyboard.GetState().IsKeyDown(Input.Up))
  39. Position.Y -= Speed;
  40. else if (Keyboard.GetState().IsKeyDown(Input.Down))
  41. Position.Y += Speed;
  42. Position = Vector2.Clamp(Position, new Vector2(0, 0), new Vector2(Game1.ScreenWidth - this.Rectangle.Width, Game1.ScreenHeight - this.Rectangle.Height));
  43. }
  44. }
  45. }