我在C#表单应用程序上遇到问题。我想做一个飞镖游戏。光标没问题。它将自动移动,当您点击表格时,它将“拍摄”表格。但我无法计算得分。如何找到拍摄背景图像的位置?顺便说一下,箭头是一个标签,当你点击表格时我创造了那个东西。我的表单大小:500x500,圆圈距离70px。
我的目标检测代码......
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();
}
有我的目标动作
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;
}
}
答案 0 :(得分:2)
因此,标签会水平移动,然后当用户点击它的“X&#39;位置应该保存。之后,它垂直移动,同样的操作以保存其“Y&#39;位置。
当我们同时拥有X和Y时,我们会检查分数。
我的解决方案只是创建两个变量来保存表单中的X和Y(让我们称之为currentX
和currentY
),将它们原来设置为-1,然后使用他们直接检查你是哪一步。其余基本相同。
如果您只需点击一下就可以获得X和Y,只需删除对currentX == -1和返回的检查。
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;
}
原始答案:
如果没有更多信息,我会假设您正在使用图像作为电路板。要获得鼠标的X / Y坐标,请单击图像,您可以使用此解决方案:
this.MouseClick += new MouseEventHandler(myForm_MouseClick);
void myForm_MouseClick(object sender, MouseEventArgs e)
{
int myX = e.X;
int myY = e.Y;
}
从那里,使用距离公式,从标签到鼠标点击的距离是微不足道的:
double distance = Math.Sqrt(Math.Pow(myX - labelX, 2) + Math.Pow(myY - labelY, 2));
您应该考虑使用相对于窗口/窗体大小的值,除以条带数* 2(因为每个条带是圆形,在给定维度上需要2个不同的空格)。
double bandWidth = this.Width / (numberOfBands * 2);
if (distance <= bandWidth * 2) // Eagle-eye!
score += 10;
else if (distance <= bandWidth * 3)
score += 9;
//etc
参考文献:
- Getting the mouse click location
- Distance formula
编辑:我完全没有回答只是为了让带宽开玩笑。