Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public PictureBox Move(PictureBox pb, System.Drawing.Point newloc, int seconds)
- {
- double distance = Math.Sqrt((newloc.X - pb.Location.X)*(newloc.X - pb.Location.X) + (newloc.Y - pb.Location.Y)*(newloc.Y - pb.Location.Y));
- Stopwatch SW = new Stopwatch();
- double interval = Stopwatch.Frequency / 30;
- double speed = (distance / seconds) * interval;
- Draw(pb, pb.Image, new Point(pb.Location.X, pb.Location.Y), "x", pb.Size);
- double x = 0;
- Thread.Sleep(1);
- pb.Visible = true;
- SW.Start();
- do
- {
- if (SW.ElapsedTicks >= x + interval)
- {
- x = SW.ElapsedTicks;
- if (1 > newloc.X - pb.Location.X | 1 > newloc.Y - pb.Location.Y)
- {
- if (speed > -1 * (newloc.X - pb.Location.X) | speed > -1 * (newloc.Y - pb.Location.Y))
- {
- pb.Location = new System.Drawing.Point(pb.Location.X + (newloc.X - pb.Location.X), pb.Location.Y + (newloc.Y - pb.Location.Y));
- pb.Update();
- }
- else
- {
- pb.Location = new System.Drawing.Point(pb.Location.X + (int)speed, pb.Location.Y + (int)speed);
- pb.Update();
- }
- }
- else
- {
- if (speed > (newloc.X - pb.Location.X) | speed > (newloc.Y - pb.Location.Y))
- {
- pb.Location = new System.Drawing.Point(pb.Location.X + (newloc.X - pb.Location.X), pb.Location.Y + (newloc.Y - pb.Location.Y));
- pb.Update();
- }
- else
- {
- pb.Location = new System.Drawing.Point(pb.Location.X + (int)speed, pb.Location.Y + (int)speed);
- pb.Update();
- }
- }
- Thread.Sleep(1);
- this.Update();
- pb.Update();
- };
- } while (pb.Location != newloc);
- SW.Stop();
- //Move smoothly
- return pb;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement