2014-11-11 2 views
0

Я пишу симуляцию хищника и жертвы и построил цикл, чтобы он всегда продолжался, но по какой-то причине моя программа вообще не запускает цикл. Есть идеи? И да, я знаю, что мой код грязный, но я разобраю его позже!Не работает мой 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 на boolean. Теперь у вас есть неявные преобразования типов. –

+4

Опция переключателя Strict On и исправляет ошибки, которые он затем выкидывает ... –

+0

Вызов обработчиков событий напрямую (Form_paint, Form_KeyDown) является признаком плохого дизайна. Вы не должны называть эти методы напрямую, а не внутри Form_Load. –

ответ

0

Вы звоните Form_Paint непосредственно и передавая значения для sender и e от . Однако Form_Paint ожидает, что e будет иметь тип System.Windows.Forms.PaintEventArgs, тогда как вы передаете объект типа System.EventArgs, поскольку это то, что передается в Form_Load. System.EventArgs не имеет объекта под названием Graphics. Таким образом, линия ниже (и другие подобные ему), скорее всего, бросает исключение, поскольку значение e, что вы прошли в не имеют свойство Graphics:

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

И я думаю, что вы не видите исключение, потому что это происходит в Form_Load. Запустите программу в режиме отладки и посмотрите в окне вывода. Вероятно, это исключение. Или поставьте Try Catch вокруг вашего кода.

Возможно, вам будет лучше использовать таймер, а внутри его отметьте событие, позвоните Form1.Refresh(). Это вызовет событие Paint правильно.