Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.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 如何指定要从哪个函数获取参数?_Vb.net - Fatal编程技术网

Vb.net 如何指定要从哪个函数获取参数?

Vb.net 如何指定要从哪个函数获取参数?,vb.net,Vb.net,我必须为我的工作学习VisualBasic,我一直在用自己的时间编写代码来帮助加快这个过程。我这里有一个基本的控制台应用程序,我想我传递的参数不正确 这个程序要做的是在一个矩形中绘制一组ASCII字母。其中一个字母代表玩家。当玩家按下一个键时,它应该清除屏幕,并根据按下的箭头键在玩家的新位置重新绘制整个画面 问题是,我将所有代码拆分为子代码和函数,以便于维护和理解。现在它被打破了,当按下一个键时,玩家的位置不再改变 Sub Game() Dim width As Integ

我必须为我的工作学习VisualBasic,我一直在用自己的时间编写代码来帮助加快这个过程。我这里有一个基本的控制台应用程序,我想我传递的参数不正确

这个程序要做的是在一个矩形中绘制一组ASCII字母。其中一个字母代表玩家。当玩家按下一个键时,它应该清除屏幕,并根据按下的箭头键在玩家的新位置重新绘制整个画面

问题是,我将所有代码拆分为子代码和函数,以便于维护和理解。现在它被打破了,当按下一个键时,玩家的位置不再改变

   Sub Game()
        Dim width As Integer = 60
        Dim height As Integer = 10
        Dim playerX As Integer = 5
        Dim playerY As Integer = 5
        Dim cursorX As Integer = 0
        Dim cursorY As Integer = 0
        Dim numofTiles As Integer = 0
        Dim keyPressed As ConsoleKeyInfo = Nothing

    While True
        numofTiles = 0
        cursorY = 0
        'Each time the screen is updated these variables need to go back to default
        For i = 0 To height - 1
            cursorY += 1
            cursorX = 0
            'we are starting on a new line so the X position of the cursor will go back to 0
            For j = 0 To width - 1
                DrawTiles(cursorX, playerX, cursorY, playerY)
                cursorX += 1
                numofTiles += 1
                'update information since we just drew a char to the screen
            Next
            Console.WriteLine()
        Next
        keyPressed = Console.ReadKey()
        If PlayerPressedLeftKey(keyPressed) Then
            If LeftIsClear(playerX) Then
                MovePlayerLeft(playerX)
            End If
        ElseIf PlayerPressedRightKey(keyPressed) Then
            If RightIsClear(playerX, width) Then
                MovePlayerRight(playerX)
            End If
        ElseIf PlayerPressedUpKey(keyPressed) Then
            If TopIsClear(playerY) Then
                MovePlayerUp(playerY)
            End If
        ElseIf PlayerPressedDownKey(keyPressed) Then
            If BottomIsClear(playerY, height) Then
                MovePlayerDown(playerY)
            End If
        End If
        Console.Clear()
    End While
End Sub
Sub DrawTiles(cursorX, playerX, cursorY, playerY)
    If Not CursorIsOnPlayersPosition(cursorX, playerX, cursorY, playerY) Then
        DrawBackgroundTile()
    ElseIf CursorIsOnPlayersPosition(cursorX, playerX, cursorY, playerY) Then
        DrawPlayer()
    End If
End Sub
Function RightIsClear(playerX, width)
    If playerX + 1 < width Then : Return True
    Else : Return False
    End If
End Function
Function LeftIsClear(playerX)
    If playerX > 0 Then : Return True
    Else : Return False
    End If

End Function
Function BottomIsClear(playerY, height)
    If playerY < height Then : Return True
    Else : Return False
    End If
End Function
Function TopIsClear(playerY)
    If playerY - 1 > 0 Then : Return True
        'top edge of screen is (X, 0)
    Else : Return False
    End If

End Function
Function CursorIsOnPlayersPosition(cursorX, playerX, cursorY, playerY)
    If cursorX = playerX AndAlso cursorY = playerY Then
        Return True
    Else : Return False
    End If
End Function
Sub DrawPlayer()
    Console.ForegroundColor = ConsoleColor.Green
    Console.Write("X")
    Console.ResetColor()
End Sub
Sub DrawBackgroundTile()
    Console.Write("-")
End Sub
Function PlayerPressedLeftKey(keyPressed)
    If keyPressed.Key = ConsoleKey.LeftArrow Then
        Return True
    Else : Return False
    End If
End Function
Function PlayerPressedRightKey(keyPressed)
    If keyPressed.Key = ConsoleKey.RightArrow Then
        Return True
    Else : Return False
    End If
End Function
Function PlayerPressedUpKey(keyPressed)
    If keyPressed.Key = ConsoleKey.UpArrow Then
        Return True
    Else : Return False
    End If
End Function
Function PlayerPressedDownKey(keyPressed)
    If keyPressed.Key = ConsoleKey.DownArrow Then
        Return True
    Else : Return False
    End If
End Function
Sub MovePlayerLeft(playerX)
    playerX -= 1
End Sub
Sub MovePlayerRight(playerX)
    playerX += 1
End Sub
Sub MovePlayerUp(playerY)
    playerY -= 1
End Sub
Sub MovePlayerDown(playerY)
    playerY += 1
End Sub
LeftIsClear函数和MovePlayerLeft子函数都将playerX作为参数,MovePlayerLeft位于LeftIsClear内部。我确信这是在破坏规则,这样我就不能再改变球员的位置了


我知道这是一个很大的代码块,但我想尽量清楚我的问题。重要的部分是while-True循环和函数所采用的参数。

在执行其他操作之前,请将“关闭”更改为“打开”。修复后,如果问题仍然存在,请编辑您的问题。用新代码替换旧代码。我将其更改为strict,但我得到了约70个错误,我必须逐一修复。一些旧的代码行可以由IDE修复,但其他代码行导致的错误我不知道如何修复,比如Option Strict On Disallows Late Binding。这很容易修复。您需要告诉编译器您正在使用什么类型的数据。以这个函数为例:函数playerPressedLeftKeyPressed。按键的类型是什么?它的对象是As,对象类型没有名为Key的属性。我想正确的类型是ConsoleKeyInfo。函数返回什么类型的数据?我猜是布尔型的。正确的签名应该是这样的:函数playerPresseDupKeyPressed作为ConsoleKeyInfo作为booleanPorting作为Boolean在函数声明的末尾作为Boolean似乎解决了这个问题。谢谢
If LeftIsClear(playerX) Then
    MovePlayerLeft(playerX)
        End If