制作一个Panel填充表单,我想要一个字符(由pictureBox表示)来移动。当我点击角色的pictureBox时,我希望突出显示一个区域,表示该角色可以移动多远。
下面的图片是我目前管理的图片,但这不是我想要的。红色边框矩形表示pictureBox,而橙色矩形表示突出显示的区域。每个黑色边框矩形都是一个面板。
移动到对角线上的面板的角色应该消耗2个移动,这样如果角色有2个移动可用,则在点击pictureBox时,应突出显示下面的区域:
我理解为什么我的代码突出显示正方形而不是我想要的区域,但我不确定如何解决它。任何帮助,将不胜感激;下面是我写的代码。
foreach (Panel pan in grid)
{
if (pan.Left <= (selectedCharacter.PictureBox.Left + (selectedCharacter.Movement * 80))
&& pan.Left >= (selectedCharacter.PictureBox.Left - (selectedCharacter.Movement * 80)))
{
if (pan.Top <= (selectedCharacter.PictureBox.Top + (selectedCharacter.Movement * 100))
&& pan.Top >= (selectedCharacter.PictureBox.Top - (selectedCharacter.Movement * 100)))
{
pan.BackColor = selectedCharacter.PlayerHighlight;
}
}
}
如果我不够清楚,请随意提问
答案 0 :(得分:0)
以下是我提出的解决此问题的代码。如果您能看到更容易/不同的解决方法,请随意发表评论。
Panel[] highlightedMovement = new Panel[1]; //creates an array of panels to be highlighted
highlightedMovement[0] = characterBeingHighlighted.CurrentPanel; //adds the panel the character is currently on to the array
int z = 1;
//highlights panels adjecent to those already highlighted for as many iterations as the character's movement stat
for (int i = 0; i < characterBeingHighlighted.Movement; i++)
{
foreach (Panel highlightPanel in highlightedMovement)
{
//goes through all panels on the grid and adds them to the array if they are adjacent to any already in the array.
foreach (Panel gridPanel in grid)
{
//checks if the panel is adjacent to any already in the array
if ((gridPanel.Top == (highlightPanel.Top + 100) && gridPanel.Left == highlightPanel.Left) ||
(gridPanel.Top == (highlightPanel.Top - 100) && gridPanel.Left == highlightPanel.Left) ||
(gridPanel.Left == (highlightPanel.Left + 80) && gridPanel.Top == highlightPanel.Top) ||
(gridPanel.Left == (highlightPanel.Left - 80) && gridPanel.Top == highlightPanel.Top))
{
//adds it to the array but only if it isn't already in the array
if (!highlightedMovement.Contains(gridPanel))
{
Array.Resize(ref highlightedMovement, highlightedMovement.Length + 1);
highlightedMovement[z] = gridPanel;
z++;
}
}
}
}
}
//highlights all the panels in the array
foreach (Panel panel in highlightedMovement)
{
panel.BackColor = characterBeingHighlighted.PlayerHighlight;
}