Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

c# - How to make method be used individually for every PictrueBox

I am making a game where you need to click on certain objects (using PictureBoxes) that are moving in a DVDscreensaver-like fashion (bouncing of the edges). The problem is when I have more than 1 PictureBox on screen. When one touches the edge, all of the other PictureBoxes change their X or Y velocity value to negative and start moving as if they touched the edge. How can I make it work individually for every PictureBox?

Movement engine:

private void MovementTimer_Tick(object sender, EventArgs e) //movement engine
    {
        foreach (Control z in this.Controls)
        {
            if (z is PictureBox)
            {
                if (z.Location.X < 0 || z.Location.X + z.Width > Size.Width) //bouncing effect in horizontal
                {
                    x = -x;
                }
                if (z.Location.Y < 0 || z.Location.Y + z.Height > Size.Height) //bouncing effect in vertical
                {
                    y = -y;
                }
                z.Location = new Point(z.Location.X + x, z.Location.Y + y);
            }
        }
    }

Spawning Pictureboxes:

private void InitializePictureBox()
    {
        PictureBox[] pb = new PictureBox[12];
        for (int i = 0; i < 12; i++)
        {
            x = rng.Next(10, 16) * (rng.Next(0, 2) * 2 - 1); //velocity in horz dim
            y = rng.Next(10, 16) * (rng.Next(0, 2) * 2 - 1);  //velocity in vert dim
            locx = rng.Next(100, 500); //random locations
            locy = rng.Next(100, 500);
            pb[i] = new PictureBox(); 
            pb[i].Image = Image.FromFile("../red/rbr.png");
            pb[i].Location = new Point(locx, locy);
            pb[i].SizeMode = PictureBoxSizeMode.StretchImage;
            Controls.Add(pb[i]);
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of global x and y variables, store a Point in the .Tag property of each PictureBox so they will each have their own values that can be changed independently of each other.

So in your InitializePictureBox() method:

private void InitializePictureBox()
{
    PictureBox[] pb = new PictureBox[12];
    for (int i = 0; i < 12; i++)
    {
        x = rng.Next(10, 16) * (rng.Next(0, 2) * 2 - 1); //velocity in horz dim
        y = rng.Next(10, 16) * (rng.Next(0, 2) * 2 - 1);  //velocity in vert dim
        locx = rng.Next(100, 500); //random locations
        locy = rng.Next(100, 500);
        pb[i] = new PictureBox(); 
        pb[i].Image = Image.FromFile("../red/rbr.png");
        pb[i].Location = new Point(locx, locy);
        pb[i].SizeMode = PictureBoxSizeMode.StretchImage;

        pb[i].Tag = new Point(x, y); // <-- Store a Point() in the Tag()!

        Controls.Add(pb[i]);
    }
}

Then in your Timer:

private void MovementTimer_Tick(object sender, EventArgs e) //movement engine
{
    foreach (Control z in this.Controls)
    {
        if (z is PictureBox)
        {

            Point pt = (Point)z.Tag; // <-- cast Tag() back to a Point()
            // Note the use of "pt" in the code below:
            if (z.Location.X < 0 || z.Location.X + z.Width > Size.Width) //bouncing effect in horizontal
            {
                pt = new Point(-pt.X, pt.Y);
            }
            if (z.Location.Y < 0 || z.Location.Y + z.Height > Size.Height) //bouncing effect in vertical
            {
                pt = new Point(pt.X, -pt.Y);
            }
            z.Location = new Point(z.Location.X + pt.X, z.Location.Y + pt.Y);
            z.Tag = pt; // <-- put updated Point() back in the Tag
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...