Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. public PictureBox Move(PictureBox pb, System.Drawing.Point newloc, int seconds)
  2. {
  3. double distance = Math.Sqrt((newloc.X - pb.Location.X)*(newloc.X - pb.Location.X) + (newloc.Y - pb.Location.Y)*(newloc.Y - pb.Location.Y));
  4. Stopwatch SW = new Stopwatch();
  5. double interval = Stopwatch.Frequency / 30;
  6. double speed = (distance / seconds) * interval;
  7. Draw(pb, pb.Image, new Point(pb.Location.X, pb.Location.Y), "x", pb.Size);
  8. double x = 0;
  9. Thread.Sleep(1);
  10. pb.Visible = true;
  11. SW.Start();
  12. do
  13. {
  14. if (SW.ElapsedTicks >= x + interval)
  15. {
  16. x = SW.ElapsedTicks;
  17.  
  18. if (1 > newloc.X - pb.Location.X | 1 > newloc.Y - pb.Location.Y)
  19. {
  20. if (speed > -1 * (newloc.X - pb.Location.X) | speed > -1 * (newloc.Y - pb.Location.Y))
  21. {
  22. pb.Location = new System.Drawing.Point(pb.Location.X + (newloc.X - pb.Location.X), pb.Location.Y + (newloc.Y - pb.Location.Y));
  23. pb.Update();
  24. }
  25. else
  26. {
  27. pb.Location = new System.Drawing.Point(pb.Location.X + (int)speed, pb.Location.Y + (int)speed);
  28. pb.Update();
  29. }
  30. }
  31. else
  32. {
  33. if (speed > (newloc.X - pb.Location.X) | speed > (newloc.Y - pb.Location.Y))
  34. {
  35. pb.Location = new System.Drawing.Point(pb.Location.X + (newloc.X - pb.Location.X), pb.Location.Y + (newloc.Y - pb.Location.Y));
  36. pb.Update();
  37. }
  38. else
  39. {
  40. pb.Location = new System.Drawing.Point(pb.Location.X + (int)speed, pb.Location.Y + (int)speed);
  41. pb.Update();
  42. }
  43. }
  44. Thread.Sleep(1);
  45. this.Update();
  46. pb.Update();
  47.  
  48. };
  49. } while (pb.Location != newloc);
  50. SW.Stop();
  51. //Move smoothly
  52. return pb;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement