0

制作一个面板填充表单,我希望一个角色(由一个图片框表示)继续移动。当我单击角色的图片框时,我希望突出显示一个表示该角色可以移动多远的区域。

下面的图片是我到目前为止所管理的,但这并不是我想要的。红色边框代表图片框,橙色矩形代表高亮区域。每个黑色边框的矩形都是一个面板。

具有 2 个动作的角色的当前突出显示区域

一个角色移动到对角线上的面板应该花费 2 次移动,这样如果一个角色有 2 次移动可用,那么当点击图片框时,下面的区域应该被突出显示:

具有 2 个动作的角色所需的突出显示区域

我理解为什么我的代码会突出显示一个正方形而不是我想要的区域,但我不知道如何修复它。任何帮助,将不胜感激; 下面是我写的代码。

        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;
                }
            }
        }

如果我不够清楚,请随时提出问题

4

1 回答 1

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;
}
于 2013-04-13T05:04:24.540 回答