Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 不运行Do While循环_Vb.net_Loops - Fatal编程技术网

Vb.net 不运行Do While循环

Vb.net 不运行Do While循环,vb.net,loops,Vb.net,Loops,我正在编写一个捕食者和猎物的模拟,并建立了一个循环,因此它将始终继续,但由于某种原因,我的程序根本不运行循环。有什么想法吗?是的,我知道我的代码很乱,但我会在以后整理 Public Class Form1 Dim intWidth As Integer = Screen.PrimaryScreen.Bounds.Width Dim intHeight As Integer = Screen.PrimaryScreen.Bounds.Height Dim StartBase As Integer

我正在编写一个捕食者和猎物的模拟,并建立了一个循环,因此它将始终继续,但由于某种原因,我的程序根本不运行循环。有什么想法吗?是的,我知道我的代码很乱,但我会在以后整理

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

您直接调用Form_Paint,并从Form_Load传入sender和e的值。但是,Form_Paint希望e的类型为System.Windows.Forms.PaintEventArgs,而您正在传入System.EventArgs类型的对象,因为这是传递到Form_Load中的对象。System.EventArgs没有名为Graphics的属性。因此,下面这行以及其他类似的行可能会引发异常,因为传入的e值没有图形属性:

e.Graphics.FillRectangle(Brushes.LightGreen, X, Y, 15, 15)
我认为您没有看到异常,因为它发生在表单加载中。在调试模式下运行程序并查看输出窗口。这里可能显示了例外情况。或者在代码周围放一个Try-Catch


您最好使用计时器,并在它的tick事件中调用Form1.Refresh。这将正确触发绘制事件

将blnFinished的类型更改为boolean。现在有了隐式类型转换。打开选项Strict并修复它将抛出的错误…直接调用事件处理程序Form_paint,Form_KeyDown是设计不佳的标志。您不应该直接调用这些方法,也不应该在Form_Load中调用。