开发者

Problem to move many images in c#

开发者 https://www.devze.com 2023-02-08 06:17 出处:网络
I wanna write an application to do below job: We have 15 different location (that we present them with point (x1,y1) for first location, point (x1,y2) for second location and so on point (x1,y15) for

I wanna write an application to do below job:

We have 15 different location (that we present them with point (x1,y1) for first location, point (x1,y2) for second location and so on point (x1,y15) for last location).

100 image must to be shown at this locations (with specific order) with pictureBox. Each image that shown, must start to move horizontally to reach new point (e.g. point (Xc, Yc) ) and then move vertically and so on…

When the image reach to specific point (e.g. point (Xm,Ym) ), we decide that it must continue moving or it must destroy (with probability 20%) (which means that we can create next image at that initial location).

For example, an image create at location (Xi,Yi) with pictureBox1. Then it’s not allowed to create any more image at location (Xi,Yi) until pictureBox1 destroy or return to its initial location.

What I have written so far is:

  • create 15 location.

  • move one image to reach point (Xc,Yc).

I’ve some problems:

  • I use one timer to move an image. But I want to move 100 images. (from each 15 location, we create images and move them until we destroy them and then crea开发者_高级运维te next image). So what I must to do? Use 15 timer for each location?!but How?

  • at specific point (e.g. point (Xk,Yk) ), the image must stop moving for random seconds and then continue moving. How I should do this? With another timer?!But how?

  • when image reach point (Xc,Yc) and with we decide to destroy it or not, nothing happen in my code…I don’t know why!

I’ve add an picture of what I want HERE

Here is my code so far:

public partial class Form1 : Form
{

    Random r = new Random();

    // falg to prevent create just one image at each location
    private Boolean[] createNext = {true,true,true,true,true,true,true,
                                    true,true,true,true,true,true,true,true};

    private void setImage()
    {
        int i = 1 + r.Next() % 15;

        while (createNext[i] != true)
            i = 1 + r.Next() % 15;

        switch (i)
        {
            case 1: pictureBox1.ImageLocation = "grin.png";
                break;
            case 2: pictureBox2.ImageLocation = "grin.png";
                break;
            case 3: pictureBox3.ImageLocation = "grin.png";
                break;
            case 4: pictureBox4.ImageLocation = "grin.png";
                break;
            case 5: pictureBox5.ImageLocation = "grin.png";
                break;
            case 6: pictureBox6.ImageLocation = "grin.png";
                break;
            case 7: pictureBox7.ImageLocation = "grin.png";
                break;
            case 8: pictureBox8.ImageLocation = "grin.png";
                break;
            case 9: pictureBox9.ImageLocation = "grin.png";
                break;
            case 10: pictureBox10.ImageLocation = "grin.png";
                break;
            case 11: pictureBox11.ImageLocation = "grin.png";
                break;
            case 12: pictureBox12.ImageLocation = "grin.png";
                break;
            case 13: pictureBox13.ImageLocation = "grin.png";
                break;
            case 14: pictureBox14.ImageLocation = "grin.png";
                break;
            case 15: pictureBox15.ImageLocation = "grin.png";
                break;
        }
    }

    private int k = 0;
    private Boolean destroy = true;

    // timer that move pictureBox1
    void timer_Tick(object sender, EventArgs e)
    {
        k++;

        int x = pictureBox1.Location.X;
        int y = pictureBox1.Location.Y;

        if (k <= 150)//go right 300 in 150 ticks
            pictureBox1.Location = new Point(x + 2, y);
        else if (k <= 300)
        {
            if (y < 200)
                pictureBox1.Location = new Point(x, y + 1);
            if (y > 200)
                pictureBox1.Location = new Point(x, y - 1);
        }

        else if (k <= 400)
            pictureBox1.Location = new Point(x + 2, y);

        else if (k <= 550)
        {
            if (destroy == false)
                pictureBox1.Location = new Point(x + 2, y);
            if (destroy == true)
                pictureBox1.Location = new Point(x, y - 3);
        }

        else if (k <= 650)
            pictureBox1.Location = new Point(x, y - 1);

        else if (k <= 850)
            pictureBox1.Location = new Point(x - 2, y);

        else if (k <= 950)
            pictureBox1.Location = new Point(x, y + 1);

        else
            timer1.Stop();
    }

    public Form1()
    {
        InitializeComponent();

        for (int i = 0; i < 15; i++)
        {
            setImage();
            timer1.Start();
            timer1.Interval = 15;
            timer1.Tick += new EventHandler(timer_Tick);
        }
    }

Please help me to complete this application.

Thanks in advance.


You should probably just use one timer, and make a number of instances of a class that represents your moving picturebox. (This class would contain the points it will go to, ordered to the first point is always your next destination. Also it will contain the picturebox.. etc).

enum Action
{
   None,
   Stop,
   CheckDestroy

}
class InterestingPoint
{
  Point m_Point;
  Action m_Action;
}

class MovingImage
{
 public bool DestroyMe {get; private set; }

 PictureBox m_PictureBox;
 List<InterestingPoint> m_PointsToVisit;
 int m_StoppedUntil = 0;

 MovingImage(...) 
{ 
   // Constructor
}

 void Update(int k)
 {
   if(m_StoppedUntil > k) return;

   //Move m_PictureBox towards m_PointsToVisit.First
   //when m_PictureBox.Location == m_PointToVisit.First, check if the InterestingPoint
   //has a action you need to handle, 
   //like if the action is stop, you set m_StoppedUntil to k+ the number of frames you 
   //want the picturebox to stay still. (m_StoppedUntil = k+10 => doesnt update for 10 frames)
   //if the action is checkdestroy, see if it shall be destroyed and set DestroyMe to true
}

So, you initialize your MovingImage objects with a picturebox and list of point it should move to, these points contain a action for the object to execute when it arrives there.

Keep your MovingImages in a list of some kind and iterate over them to update them. In your Timer_Tick you do something like

void timer_Tick(object sender, EventArgs e)
{
  k++
  foreach(MovingImage m in m_MyMovingImages)
  {
    m.Update(k);
    if(m.DestroyMe) 
    {
      //Destroy it.
    }
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号