Home > Software engineering >  Problem with pictureboxes spawning and overlapping WINFORMS C#
Problem with pictureboxes spawning and overlapping WINFORMS C#

Time:03-22

Im trying to make a small Space Invader type game for a school project. I am pretty happy with what I did till this point, but now im struggling with the enemyspawn. I dont want the pictureboxes to spawn in each other or to overlap.

public void Gegnerspawn()
    {
        int tempx = 1;
        for (int x = 0; x <= 5; x  )
        {
            Random random = new Random();
            tempx = random.Next(1, 701);
            
            PictureBox Gegner = new PictureBox();
            Gegnerlist.Add(Gegner); 
            Gegnerlist[x].Image = Properties.Resources.enemy;
            Gegnerlist[x].SizeMode = PictureBoxSizeMode.StretchImage;
            Gegnerlist[x].Tag = "Gegner";
            Gegnerlist[x].BackColor = Color.Transparent;
            Gegnerlist[x].Size = new Size(50, 50);

            foreach(int xcoords in xcordlist) 
            {
                //if (xcoords   50 >= tempx && xcoords - 50 <= tempx)
                //{

                //}
                //else
                //{
                //tempx = random.Next(1, 701);
                //}
                while(tempx >= xcoords && tempx <= xcoords   50 || tempx   50 >= xcoords && tempx   50 <= xcoords   50) //tempx >= xcoords && xcoords   50 <= tempx
                {
                    tempx = random.Next(1, 701);
                }

            }
            xcordlist.Add(tempx);
            Console.WriteLine(tempx);
            Gegnerlist[x].Location = new Point(tempx, 12);
            this.Controls.Add(Gegnerlist[x]);
            Gegnerlist[x].BringToFront();
        } 
    }

Sorry for the variable names. Thank you!

CodePudding user response:

Here's how you can create a loop that picks a random location that doesn't intersect with any of your other existing enemies:

Rectangle rc1;
Rectangle rc2;
bool intersected;
do
{
    intersected = false;
    tempx = random.Next(1, 701);
    Gegner.Location = new Point(tempx, 12);
    rc1 = new Rectangle(Gegner.Location, Gegner.Size);
    foreach(PictureBox pb in gengerList)
    {
        rc2 = new Rectangle(pb.Location, pb.Size);
        if (rc1.IntersectsWith(rc2))
        {
            intersected = true;
            break;
        }
    }
} while (intersected);
// don't add new PB to list until AFTER 
//you've made sure it doesn't intersect with any others
gengerList.Add(Gegner); 

Note the comment at the bottom. Make sure you don't add the new PB to the List until AFTER you have made sure it doesn't intersect with of the other ones already there. If you add it beforehand then it will intersect with itself and get stuck in the loop.

If it still gets stuck then there is no way to place that number of PBs in the area you've specified, given the random locations they were placed. In that case, re-think the sizes and/or use a different method to place them.

  • Related