我正在尝试创建一个基于网格的游戏。到目前为止,我有一个基于六边形瓷砖的网格,其坐标方案如下所示:
col 0
| col 1
| | col 2
| | |
__ | __ __ __ __
/00\__/02\__/04\__/06\__/08\__
\__/01\__/03\__/05\__/07\__/09\--- row 0
/10\__/12\__/14\__/16\__/18\__/
\__/11\__/13\__/15\__/17\__/19\--- row 1
/20\__/22\__/24\__/26\__/28\__/
\__/21\__/23\__/25\__/27\__/29\--- row 2
/30\__/32\__/34\__/36\__/38\__/
\__/ \__/ \__/ \__/ \__/ --- row 3
在现实生活中看起来像这样,每个六边形都有随机颜色:
我正在努力弄清楚的是,当用户点击一个六边形时,我如何确定他们点击了哪个六边形?
到目前为止我尝试过的代码如下:
private: System::Void MyForm_MouseDown(System::Object^ sender,
System::Windows::Forms::MouseEventArgs^ e) {
int CloseI=0,CloseJ=0;
CloseJ = FindNearesetX(e->X);
CloseI = FindNearesetY(e->Y);
//Grid[down(y)][along(x)]
P1.X = Grid[CloseI][CloseJ].GetX();
P1.Y = Grid[CloseI][CloseJ].GetY();
} // END MOUSE DOWN EVENT
int FindNearesetX(int ActualX){
int ClosestJPos;
ClosestJPos = ((ActualX-Grid[0][0].GetX())/(1.5*HexSideLength));
return ClosestJPos;
}//END FIND NEAREST X
int FindNearesetY(int ActualY){
int ClosestIPos;
ClosestIPos = ((ActualY-Grid[0][0].getY())/(HexHeight));
return ClosestIPos;
}//END FIND NEAREST Y
private: System::Void MyForm_MouseMove(System::Object^ sender,
System::Windows::Forms::MouseEventArgs^ e) {
this->Invalidate();
P2.X = e->X;
P2.Y = e->Y;
} // END MOUSE MOVE EVENT
然而,这并没有按我的意愿工作,这是因为当用户单击六边形中心点的左侧时,它会捕捉到他们单击的左侧的六边形,并且如果他们单击中心点上方所有奇数列都捕捉到他们点击的那一列上方的六边形。
我已经在这个问题上停留了 2 天,真的很想弄清楚。谢谢