2017-01-07 1 views
0

으로 삭제합니다. 레벨이 올라감에 따라 원래 크기에서 작은 크기로 보드를 축소해야하는 고전적인 뱀 게임을 만듭니다. 현재 보드 크기를 조정했지만 사용하지 않는 PictureBox 아직 삭제되어 삭제할 수 없습니다. 새로운 변수로 PictureBox를 다시 만드는 방법을 삭제하고 호출하는 방법에 문제가 있습니다.모든 PictureBox를 C#

C#의 PictureBox 삭제/제거와 관련하여 내 문제를 해결할 수있는 사람이 있으면 감사하게 생각합니다. 다음은 내 프로젝트의 소스 코드 일부입니다. 전체 소스 코드보기, 나를 직접 요청할 수 있습니다.

여기서 보드를 다시 만들거나 새로 고치는 코드는 어디에 두어야합니까?

private void gotoNextLevel(int nextLevel) 
    { 
     mode = "REST"; 
     mySnake = new Snake(mainBoard); //Brand new snake with length 1 
     apples = new Rewards(nextLevel, mainBoard); //<--- Generate 5 apples 
    } 

다음은 게임용 보드를 만드는 방법입니다.

int maxRow = 10, maxCol = 20;  //Max 10 rows, and 20 columns in the board 
    int squareSize = 30;    //Each square is 30px by 30px 

    PictureBox[,] squares; 

    public Board(Form mainForm) 
    { 
     squares = new PictureBox[maxRow, maxCol]; 
     for (int row = 0; row < maxRow; row++) 
     { 
      for (int col = 0; col < maxCol; col++) 
      { 
       squares[row, col] = new PictureBox(); 
       squares[row, col].Location = new Point(col * squareSize, row * squareSize); 
       squares[row, col].Height = squareSize; 
       squares[row, col].Width = squareSize; 
       squares[row, col].SizeMode = PictureBoxSizeMode.StretchImage; 
       squares[row, col].BackColor = Color.DarkGray; 
       squares[row, col].BorderStyle = BorderStyle.FixedSingle; 

       mainForm.Controls["boardPanel"].Controls.Add(squares[row, col]); 
      } 
     } 
     mainForm.Controls["controlPanel"].Location = new Point(mainForm.Controls["boardPanel"].Location.X, mainForm.Controls["boardPanel"].Location.Y + mainForm.Controls["boardPanel"].Height + 20); 
    } 

다음은 새로 고침 방법입니다.

private void refresh(Object myObject, EventArgs myEventArgs) 
    { 
     mySnake.move(mode); //Move the snake based on mode 
     modeLBL.Text = mode; 

     mainBoard.draw(); 
     apples.draw(); //<----- draw apples 
     mySnake.draw(); 

     //increment the duration by amount of time that has passed 
     //this method is called every speed millisecond 
     duration += speed; 
     timerLBL.Text = Convert.ToString(duration/1000); //Show time passed 


     //Check if snke is biting itself. If so, call GameOver. 
     if (mySnake.checkEatItself() == true) 
     { 
      GameOver(); 
     } 
     else if (apples.checkIFSnakeHeadEatApple(mySnake.getHeadPosition()) == true) 
     { 
      score += apples.eatAppleAtPostion(mySnake.getHeadPosition()); 

      scoreLBL.Text = Convert.ToString(score); 


      if (apples.noMoreApples() == true) 
      { 
       clock.Stop(); 
       level++; 
       levelLBL.Text = Convert.ToString(level); 
       gotoNextLevel(level); 
       MessageBox.Show("Press the start button to go to Level " + level, "Congrats"); 
      } 
      else 
      { 
       //Length the snake and continue with the Game 
       mySnake.extendBody(); 
      } 
     } 
    } 
+0

boardPanel의'Controls' 컬렉션 :'boardPanel.Controls합니다. Clear();는 모든 그림 상자를 제거합니다. – dlatikay

+0

당신은 일반적인 실수를하고 있습니다. 새로 고침 이벤트에서 인수를 사용하는 대신보기의 개체에 직접 액세스합니다. MyObject를 사용해야합니다. 새로운 pictureboxes를 만드는 것보다 현재 pictureboxes의 크기를 축소하는 것이 더 낫지 않습니까? – jdweng

+0

후 "boardPanel.Controls.Clear();" dlatikay에 의해 suggeested, 보드 올바르게 모든 것을 지 웠지만 슬프게도 새로운 가치로 내 보드를 다시 그리지 않았다. – Jatiz

답변

0

우선 : Visual Studio 자체에서 생성 한 refresh()의 서명은 무엇입니까? 그렇다면, body를 복사하고 매개 변수없이 새로운 메소드에 넣는 것은 어떨까요? 왜냐하면 현재 refresh() 메소드에서 사용하지 않기 때문입니다.

둘째 : 새 메서드를 만드는 경우 컨트롤을 먼저 지운 다음 다시 그리기를 호출하는 메서드를 만들 수 있습니다. 나는 모든 컨트롤을 지우므로 컨트롤이 다시 호출되지 않는 이벤트이기 때문에 다시 그려지지 않는다고 생각합니다.

그리고 마지막으로 : 당신의 PictureBox 이외의 컨트롤을 가지고 있지만 만에 PictureBox를 제거 할 경우이를 사용할 수 있습니다 : 투명

foreach (Control control in this.Controls) 
{ 
    if (control is PictureBox) 
    { 
     this.Controls.Remove(control); 
    } 
} 
관련 문제