【问题标题】:How do you wait for user input in a do loop?如何在 do 循环中等待用户输入?
【发布时间】:2019-06-09 19:07:01
【问题描述】:

!https://imgur.com/MpgjG3R

我正在尝试制作的程序是基于文本的游戏。

玩家输入命令,输入显示在列表框中,以及不同的“房间”

有一个设置的生成点,它是一个布尔语句,在输入命令之前为真。

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

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

如何让循环暂停?

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

现在,我的代码甚至没有运行。

【问题讨论】:

  • 请始终提供错误信息。否则将很难或不可能帮助您。
  • 当我按下运行时,什么也没有发生。它只是停留在这个imgur.com/yYht8GQ
  • 卡在循环中; GUI 在那里时无法响应。您正在尝试将控制台样式的线性代码塞进非线性的 winforms 应用程序中;一个方形钉子插入一个圆孔。相反,将您的逻辑放入一个不同的子程序中,没有循环,从按钮处理程序调用。

标签: vb.net


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2016-07-19
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    • 2016-05-03
    相关资源
    最近更新 更多