Bat.cs 914 B

12345678910111213141516171819202122232425262728293031323334353637
  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 Tutorial010.Sprites
  10. {
  11. public class Bat : Sprite
  12. {
  13. public Bat(Texture2D texture)
  14. : base(texture)
  15. {
  16. Speed = 5f;
  17. }
  18. public override void Update(GameTime gameTime, List<Sprite> sprites)
  19. {
  20. if (Input == null)
  21. throw new Exception("Please give a value to 'Input'");
  22. if (Keyboard.GetState().IsKeyDown(Input.Up))
  23. Velocity.Y = -Speed;
  24. else if (Keyboard.GetState().IsKeyDown(Input.Down))
  25. Velocity.Y = Speed;
  26. Position += Velocity;
  27. Position.Y = MathHelper.Clamp(Position.Y, 0, Game1.ScreenHeight - _texture.Height);
  28. Velocity = Vector2.Zero;
  29. }
  30. }
  31. }