سؤال

i have made 2 rectangles face to face (up and down) and the distance between them is stored into an integer. How can i wrote a code to check if something (a moveable label) is going through that distance (between them) without touching any rectangle ? I have started to make a label into a rectangle:

Rectangle rec = label2.Bounds;

Update:

 e.Graphics.FillRectangle(Brushes.LightCyan, new Rectangle(Pipe1[0], 0, PipeWidth, Pipe1[1]));
 e.Graphics.FillRectangle(Brushes.LightCyan, new Rectangle(Pipe1[2], Pipe1[3], PipeWidth, this.Height - Pipe1[3]));

 e.Graphics.FillRectangle(Brushes.LightCyan, new Rectangle(Pipe2[0], 0, PipeWidth, Pipe2[1]));
 e.Graphics.FillRectangle(Brushes.LightCyan, new Rectangle(Pipe2[2], Pipe2[3], PipeWidth, this.Height - Pipe2[3]));

 e.Graphics.FillRectangle(Brushes.Green, new Rectangle((Pipe1[0] + Pipe1[1]) / 2, 0, 15, PipeDifferentY));

Here are the two rectangles face to face and the last one im trying to make a little rectangle to insert it between those two, i have the distance between those rectangle stored in that integer, so how can i position the new rectangle between the two face to face one with a height of the distance between them. ?!

Update 2:

I've made it with rectangle intersection, but now i have one more little problem:

if (intersect1 != Rectangle.Empty | intersect2 != Rectangle.Empty)
   {
     points++;
   }

To the points is adding 10, 20, 40, and so on. I tried with:

points += 1;

But it doesn't work either, it's maybe because the label is overlapping the rectangle more then once. How should i resolve this?

هل كانت مفيدة؟

المحلول

I think we need more insight into your code to resolve your points issue. Are you zeroing it out each time you fill it up? I assume that it's an integer? When you step through the code, does it double the value the moment you execute that line, or are you rechecking it periodically?

I suppose a larger question is, do you want "points" to indicate how many times the intersection has happened in total? The number of times over a series of frames? My suspicion is that you're never zeroing it out, but it just keeps accruing, probably at the same speed as the timing of whatever thread you use to check this value.

To answer your first question, assuming you correctly handled building the third rectangle between the first two, is that your check will be thrice. Assuming intersect1 is the intersection of the label's bounds with the top rectangle, intersect2 is the intersection of the label's bounds with the bottom rectangle, and intersect3 is the intersection of the label with the rectangle created by the shared width of the two original rectangles (I don't know, from your example, whether they have a horizontal offset from each other) and the height of the space between them, it's between the two pipes but not touching when (( intersect1 == Rectangle.Empty | intersect2 == Rectangle.Empty) && intersect3 != Rectangle.Empty). Your current check just increments "points" when your label is not intersecting either pipe.

And, just as a wild guess, recreating Flappy Bird?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top