I'm writing a program, I need that when PictureBox1, called "Player", hits certain pictures of PictureBox2-15, referred to as eat1-14, they disappear. Here is how I tried it:

if ((Player.Location.X < eat1.Location.X)) { if ((Player.Location.Y < eat1.Location.Y)) { eat1.Visible = false; } } 

It works very strange, i.e. sometimes food is not eaten, or only one of 14 is eaten.

  • WinForms, WPF or something else? - Alexander Petrov
  • BringToFront () - Alexander Petrov

1 answer 1

You have 2 objects described by Rectangle (rectangle). This means that you need to do a simple check.

The method will be approximately as follows:

 bool RectContains(Rectangle R1, Rectangle R2) { if ( (R2.x+R2.width) < (R1.x+R1.width) && (R2.x) > (R1.x) && (R2.y) > (R1.y) && (R2.y+R2.height) < (R1.y+R1.height) ) { return true; } else { return false; } } 

(code did not check)

Well, any picture can be described as Rectangle. That is, the point of the upper left corner + the width and height of the rectangle.

Well, then do the check every 50 ms (for example) if

 if( RectContains(R1, R2) ) { PictureBox2.Destroy(); //ну или визибл = фалс }