2013-04-07 65 views
0

制作面板填充表单,我希望角色(用pictureBox表示)移动。当我点击角色的pictureBox时,我想要突出显示一个区域,表示该角色可以移动多远。如何突出显示面板网格上的特定区域

下面的图片是我迄今为止管理的,但这不是我想要的。红色边框矩形代表图片框,而橙色矩形代表突出显示的区域。每个黑色边框矩形都是一个面板。

Current Highlighted area for a character with 2 movement

的字符移动到在对角线上面板应该花费2移动,从而如果一个字符具有可用下面的区域应当强调2运动被点击图片框时上:

Desired Highlighted area for a character with 2 movement

我明白为什么我的代码突出显示一个正方形而不是我想要的区域,但我不知道如何解决该问题。任何帮助,将不胜感激;下面是我写的代码。

 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

清楚这是我想出了解决这一问题的代码。如果你能看到更简单/不同的方式来解决这个问题,请随时发表评论。

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; 
} 
相关问题