Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

112
Vistas
How to let pictureboxes not overlap with each other?

So I made a game for a school project in windows forms. Only problem is, is that my pictures are overlapping with each other. So my question how do I get them all on a different location where they don't touch each other or not overlap?

In this Method I create the zombies and here I just choose for random locations between -100 and 0 on the x-as

public void ZombieMaker(int aantal, Form formInstance, string ZombieDik)
{
    for (int i = 0; i < aantal; i++)
    {
            PictureBox picture = new PictureBox();

            picture.Image = Properties.Resources.ZombieDik;
            picture.Size = new Size(200, 200);
            picture.Location = new Point(random.Next(1500), random.Next(-100,0));
            picture.SizeMode = PictureBoxSizeMode.Zoom;
            picture.Click += zombie_Click;
            picture.BackColor = Color.Transparent;
            formInstance.Controls.Add(picture);
            picture.Tag = zombies[i];
    }
}

pic of zombies overlapping

11 months ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Keep track of the already placed pictureboxes, and validate if the bound would overlap.

//List of all pictureBoxes
private List<PictureBox> _pictureBoxes = new List<PictureBox>();

public void ZombieMaker(int aantal, Form formInstance, string ZombieDik)
{
    for (int i = 0; i < aantal; i++)
    {
        Rectangle newPosition;

        //loop till you found a new position
        while (true)
        {
            var newPoint = new Point(random.Next(1500), random.Next(-100,0));
            var newSize = new Size(200, 200);

            newPosition = new Rectangle(newPoint, newSize);

            //validate the newPosition
            if (!_pictureBoxes.Any(x => x.Bounds.IntersectsWith(newPosition)))
            {
                //break the loop when there isn't an overlapping rectangle found
                break;
            }
        }

        PictureBox picture = new PictureBox();
        _pictureBoxes.Add(picture);
 
        picture.Image = Properties.Resources.ZombieDik;
        picture.Size = newPosition.Size;
        picture.Location = newPosition.Location;
        ...
      
    }
}

To validate the overlapping I am using the IntersectWith method of the Rectangle class

https://docs.microsoft.com/en-us/dotnet/api/system.drawing.rectangle.intersectswith?view=net-6.0#system-drawing-rectangle-intersectswith(system-drawing-rectangle)

Edit:

Here a do/while loop instead of the while loop.

Rectangle newPosition;

do
{
    var newPoint = new Point(random.Next(1500), random.Next(-100,0));
    var newSize = new Size(200, 200);

    newPosition = new Rectangle(newPoint, newSize);
} while(_pictureBoxes.Any(x => x.Bounds.IntersectsWith(newPosition))
11 months ago · Santiago Trujillo Denunciar

0

I fixed your code so the picture boxes do not overlap each other:

public void ZombieMaker(int aantal, Form formInstance, string ZombieDik)
{
   for (int i = 0; i < aantal; i++)
   {
        PictureBox picture = new PictureBox();

        picture.Image = Properties.Resources.ZombieDik;
        picture.Size = new Size(200, 200);
        picture.Location = new Point(picture.Width * i, random.Next(-100,0));
        picture.SizeMode = PictureBoxSizeMode.Zoom;
        picture.Click += zombie_Click;
        picture.BackColor = Color.Transparent;
        formInstance.Controls.Add(picture);
        picture.Tag = zombies[i];
  }
}
11 months ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos