您如何在do循环中等待用户输入?

时间:2019-06-09 01:52:22

标签: vb.net

https://imgur.com/MpgjG3R

我要制作的程序是一个基于文本的游戏。

播放器输入命令,输入与不同的“房间”一起显示在列表框中

有一个设置的生成点,它是一个布尔语句,直到输入命令后该布尔语句才为真。

例如,如果输入“ n”,则第二个布尔值“ spawnpointN”为true,而原始的生成点为false。

在循环迭代一次之前,我希望它等待用户输入。

如何使循环暂停?

Public Class frmMain

    Const north As String = "n"
    Const east As String = "e"
    Const west As String = "w"
    Const south As String = "s"
    Dim input As String

    Dim finished As Boolean = False
    Dim spawnpoint As Boolean = True
    Dim spawnpointN As Boolean = False
    Dim spawnpointE As Boolean = False
    Dim spawnpointW As Boolean = False
    Dim spawnpointS As Boolean = False

    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AcceptButton = btnEnter
        Do
            If spawnpoint = True Then
                Me.lstDisplay.Items.Add("You awake in a room of darkness.")

                If Me.input = north Then
                    spawnpointN = True
                    spawnpoint = False
                End If

                If Me.input = east Then
                    spawnpointE = True
                    spawnpoint = False
                End If

                If Me.input = south Then
                    spawnpointS = True
                    spawnpoint = False
                End If

                If Me.input = west Then
                    spawnpointW = True
                    spawnpoint = False
                End If

            End If

            If spawnpointN = True Then
                lstDisplay.Items.Add("Test")
                If Me.input = south Then
                    Me.spawnpoint = True
                    Me.spawnpointN = False
                End If
            End If
            If input = "end" Then
                finished = True
            End If

        Loop Until finished = True

        If finished = True Then
            lstDisplay.Items.Add("You have finished my game!")

        End If


    End Sub

    Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click
        Dim input As String = Me.txtInput.Text
        Me.lstDisplay.Items.Add(input)
        Me.txtInput.Clear()
        lstDisplay.TopIndex = lstDisplay.Items.Count - 1
    End Sub

End Class

现在,我的代码甚至无法运行。

1 个答案:

答案 0 :(得分:0)

我同意@Idle_Mind。您采用了主机游戏的想法,并将其移植到winforms。很好,但是您必须更改应用程序的行为。

您不会向用户询问北,东,南,西,而是在文本字段的顶部,左侧,底部,右侧放置按钮,然后这些按钮的事件处理程序将在对象上调用逻辑,例如具有CurrentRoom属性,该属性返回具有功能Go(direction As Direction)的房间对象,然后该对象将返回下一个房间对象等。