Gökhan Gökce - 4 months ago 37

C# Question

I have problem on C# forms application. I want to make a dart game. The cursor is ok. It will be moving automatically, when you click on form it will "shoot" the form. But I can't calculate the score. How can I find where the background image was shot? By the way, the arrow is a label and I creating that thing when you click on the form. My form size: 500x500 and circles distance 70px Sorry for bad English and thank you ...

There is my target detect codes...

`if (asama == 0)`

{

lblNisan.Top = -30;

asama = 1;

}

else if (asama == 1)

{

asama = 2;

Label ok = new Label();

ok.BackColor = Color.Red;

ok.AutoSize = false;

ok.Width = 5;

ok.Height = 5;

int a = lblNisan.Left + lblNisan.Size.Width / 2;

int b = lblNisan.Top + lblNisan.Size.Height / 2;

if (yon == 0)

{

a -= ruzgar;

}

else if (yon == 1)

{

a += ruzgar;

}

else if (yon == 2)

{

b -= ruzgar;

}

else if (yon == 3)

{

b += ruzgar;

}

Point yer = new Point(a, b);

ok.Location = yer;

this.Controls.Add(ok);

lblNisan.Visible = false;

tmrNisan.Stop();

}

else if (asama == 2)

{

asama = 0;

tmrNisan.Start();

lblNisan.Left = -30;

lblNisan.Top = 185;

lblNisan.Visible = true;

ruzgarHesapla();

}

There is my target movements

`if (asama == 0)`

{

if (lblNisan.Left <= 430 && yonler == 0)

{

if (lblNisan.Left >= 420)

{

yonler = 1;

}

else

lblNisan.Left += 15;

}

if (lblNisan.Left >= -30 && yonler == 1)

{

if (lblNisan.Left <= -20)

{

yonler = 0;

}

else

lblNisan.Left -= 15;

}

}

if (asama == 1)

{

if (lblNisan.Top <= 430 && yonler == 0)

{

if (lblNisan.Top >= 420)

{

yonler = 1;

}

else

lblNisan.Top += 15;

}

if (lblNisan.Top >= -30 && yonler == 1)

{

if (lblNisan.Top <= -20)

{

yonler = 0;

}

else

lblNisan.Top -= 15;

}

}

Answer

So, a label moves horizontally, then when the user clicks its 'X' position should be saved. After that, it moves vertically, same operation to save its 'Y' position.

And when we have both X and Y, we check the score.

My solution would simply to create two variable to save X and Y in your form (let's call them `currentX`

and `currentY`

), set them originally at -1, and then use them directly to check at which step you are. The rest is basically the same.

*If you get both X and Y on a single click, just remove the check for currentX == -1 and the return.*

```
private int currentX = -1;
private int currentY = -1;
//...
void myForm_MouseClick(object sender, MouseEventArgs e)
{
if (currentX == -1) // remove
{ // remove
currentX = myLabel.Location.X;
return; // remove
} // remove
currentY = myLabel.Location.Y;
int centerX = this.X / 2.0;
int centerY = this.Y / 2.0;
// we'll assume the form is a perfect square, else
// it gets a bit too complicated
double bandWidth = /* comcast ? 0 : */ this.Width / (numberOfBands * 2);
double distance = Math.Sqrt(Math.Pow(centerX - currentX, 2) + Math.Pow(centerY - currentY, 2));
if (distance <= bandWidth * 2) // Eagle-eye!
score += 10;
else if (distance <= bandWidth * 3)
score += 9;
//etc
// don't forget to reset
currentX = -1;
currentY = -1;
}
```

*Original answer:*

Without more information, I'll assume you're using an Image for the board. To get the X/Y coordinates of a mouse click on an Image, you can use this solution:

```
this.MouseClick += new MouseEventHandler(myForm_MouseClick);
void myForm_MouseClick(object sender, MouseEventArgs e)
{
int myX = e.X;
int myY = e.Y;
}
```

From there, it's trivial to get the distance from your label to your mouse click, using the distance formula:

```
double distance = Math.Sqrt(Math.Pow(myX - labelX, 2) + Math.Pow(myY - labelY, 2));
```

You should consider using a value relative to the size of your window/form, that you'd divide by the number of bands * 2 (since each band is a circle, it takes 2 different spaces in a given dimension).

```
double bandWidth = this.Width / (numberOfBands * 2);
if (distance <= bandWidth * 2) // Eagle-eye!
score += 10;
else if (distance <= bandWidth * 3)
score += 9;
//etc
```

References:

- Getting the mouse click location

- Distance formula