Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

194
Visualizações
¿Cómo dejar que los cuadros de imágenes no se superpongan entre sí?

Así que hice un juego para un proyecto escolar en Windows Forms. El único problema es que mis imágenes se superponen entre sí. Entonces, mi pregunta, ¿cómo los coloco a todos en un lugar diferente donde no se tocan entre sí o no se superponen?

En este método, creo los zombis y aquí solo elijo ubicaciones aleatorias entre -100 y 0 en el 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]; } }

foto de zombis superpuestos

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Realice un seguimiento de los cuadros de imagen ya colocados y valide si el límite se superpondría.

 //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; ... } }

Para validar la superposición estoy usando el método IntersectWith de la clase Rectangle

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

Editar:

Aquí un bucle do/while while en lugar del bucle while .

 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))
over 4 years ago · Santiago Trujillo Relatório

0

Arreglé su código para que los cuadros de imagen no se superpongan entre sí:

 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]; } }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda