2014-11-11 10 views
0

저는 포식자와 먹이의 시뮬레이션을 작성 중이며 항상 반복 할 수 있도록 루프를 작성했습니다. 그러나 어떤 이유로 든 내 프로그램이 루프를 전혀 실행하지 않습니다. 어떤 아이디어? 그리고 네, 제 코드가 지저분하다는 것을 압니다. 그러나 나중에 그것을 분류 할 것입니다!Do Do While Loop를 실행하지 않습니다.

Public Class Form1 
Dim intWidth As Integer = Screen.PrimaryScreen.Bounds.Width 
Dim intHeight As Integer = Screen.PrimaryScreen.Bounds.Height 
Dim StartBase As Integer = 10 
Dim Hunter(100, 1) As Integer 
Dim Hunted(100, 1) As Integer 
Dim intCount As Integer = 0 
Dim blnFinished As Integer = False 
Dim blnFirst As Boolean = True 
Private Sub Form1_keydown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown 
    If e.KeyCode = Keys.Escape Then 
     If MsgBox("Do you want to exit?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then 
      Application.Exit() 
     End If 
    End If 
End Sub 

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load 
    Form1_Paint(sender, e) 
    Do While (blnFinished = False) 
     Movement() 
     Form1_Paint(sender, e) 
     Form1_keydown(sender, e) 
    Loop 
End Sub 

Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint 
    Dim X As Integer = 0 
    Dim Y As Integer = 0 
    If blnFirst = True Then 
     RandomPosition(X, Y) 
     For Me.intCount = 1 To StartBase 
      X = CInt(Math.Floor(((intWidth) - 0 + 1) * Rnd())) + 0    'Assigns a random x coordinate 
      Y = CInt(Math.Floor((intHeight - 250) - 0 + 1) * Rnd()) + 0  'Assigns a random y coordinate 
      Hunted(intCount, 0) = X 
      Hunted(intCount, 1) = Y           'Saves the x and y value to Hunted array 
      e.Graphics.FillRectangle(Brushes.LightGreen, X, Y, 15, 15)  'Draws the hunted to the screen with earlier values 
      X = CInt(Math.Floor(((intWidth) - 0 + 1) * Rnd())) + 0    'Assigns a random x coordinate 
      Y = CInt(Math.Floor((intHeight - 250) - 0 + 1) * Rnd()) + 0  'Assigns a random y coordinate 
      Hunter(intCount, 0) = X 
      Hunter(intCount, 1) = Y           'Saves the x and y value to Hunted array 
      e.Graphics.FillRectangle(Brushes.Maroon, X, Y, 15, 15) 'Draws the hunted to the screen with earlier values 
     Next 
     blnFirst = False 
    Else 
     For Me.intCount = 1 To StartBase 
      X = Hunted(intCount, 0) 
      Y = Hunted(intCount, 1) 
      e.Graphics.FillRectangle(Brushes.LightGreen, X, Y, 15, 15) 
      X = Hunter(intCount, 0) 
      Y = Hunter(intCount, 1) 
      e.Graphics.FillRectangle(Brushes.Maroon, X, Y, 15, 15) 
     Next 
    End If 
    e.Graphics.FillRectangle(Brushes.White, 0, intHeight - 235, intWidth, 200) 
End Sub 

Private Sub RandomPosition(X, Y) 
    Randomize() 
    X = CInt(Math.Floor((Width - 0 + 1) * Rnd())) + 0 
    Y = CInt(Math.Floor((Height - 250) - 0 + 1) * Rnd()) + 0 
End Sub 

Private Sub Movement() 
End Sub 
End Class 
+0

blnFinished의 유형을 부울로 변경하십시오. 이제 암시적인 유형 변환이 있습니다. –

+4

스위치 옵션 엄격한 켜기 및 다음 오류를 수정합니다 ... –

+0

이벤트 처리기를 직접 호출하는 (Form_paint, Form_KeyDown) 잘못된 디자인의 징조입니다. Form_Load 내부가 아닌 직접 이러한 메서드를 호출하면 안됩니다. –

답변

0

는 직접 Form_Paint를 호출하고 Form_Load에서 sendere의 값을 전달한다. 그러나 Form_Paint는 e의 형식이 System.Windows.Forms.PaintEventArgs 일 것으로 예상하는 반면 System.EventArgs 형식의 개체는 Form_Load로 전달되기 때문에이 개체를 전달합니다. System.EventArgs에는 Graphics이라는 속성이 없습니다. 그래서 (그와 같은 등) 아래 라인은 가능성이 당신이 그래픽 속성이없는 전달 e의 값 때문에 예외를 던지고있다 :

e.Graphics.FillRectangle(Brushes.LightGreen, X, Y, 15, 15) 

을 그리고 난 당신이 예외 때문에 표시되지 않는 생각 Form_Load에서 발생합니다. 디버그 모드에서 프로그램을 실행하고 출력 창을 봅니다. 아마도 거기에 예외가 표시됩니다. 또는 코드 주위에 Try Catch를 넣으십시오.

타이머를 사용하는 것이 좋을 수도 있고 내부에 틱 이벤트가있는 경우 Form1.Refresh()으로 전화하십시오. 그러면 Paint 이벤트가 제대로 트리거됩니다.