【问题标题】:VB.net Navigating Text Adventure GameVB.net 导航文字冒险游戏
【发布时间】:2021-06-05 16:44:11
【问题描述】:

我正在开发一款文字冒险游戏。该程序的目标是显示三个选项和一个文本框。用户可以通过在文本框中输入相应的数字来选择其中一个选项,然后该文本框应该将用户导航到下一个区域,向用户显示另外 3 个选项。

我目前遇到的问题是在区域游戏区域中导航。

    Sub gameOver(ByVal DeathMessage)
        lblTitle.Text = "Game Over!"
        lblMain.Text = DeathMessage
    End Sub

    Sub pgMain()
        lblMain.Text = $"Enter 1 to start the game{vbCrLf}Enter 2 to quit the game"
        If aryInput(0) = "1" Then
            pg1()
        ElseIf aryInput(0) = "2" Then
            Me.Close()
        End If
    End Sub

    Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        aryInput(0) = tbxInput.Text
    End Sub

    Sub pg1() ' User picks a starting option
        lblTitle.Text = "You spot a secret magical lair do you"
        lblMain.Text = $"1. Enter the lair through the front door{vbCrLf}2. Enter the lair through the back door{vbCrLf}3. Wait untill midnight to enter the lair."
        If aryInput(0) = "1" Then
            MsgBox("Front door") 'pg2()
        ElseIf aryInput(0) = "2" Then
            MsgBox("backdoor")
            pg3()
        ElseIf aryInput(0) = "3" Then
            gameOver("You were mauled by wolves")
        End If
    End Sub

    'Sub pg2() ' User entered through the front door.
    '    lblMain.Text = $"1. Go to the chest{vbCrLf}2. Go to the bookshelf{vbCr}3. Go to the cauldron"
    '    If tbxInput.Text = "1" Then
    '        pg5() ' They went to the chest
    '    ElseIf tbxInput.Text = "2" Then
    '        pg6() ' User went to the bookshelf
    '    ElseIf tbxInput.Text = "3" Then
    '        pg7() ' User went to the cauldron
    '    End If
    'End Sub

    Sub pg3()
        tbxInput.Text = Nothing
        lblTitle.Text = "You were splashed with a poison spell do you"
        lblMain.Text = $"1. Cut off the infected part{vbCrLf}2. Drink a bucket of milk{vbCrLf}3. Inject yourself with some sort of medical syringe"
        If tbxInput.Text = "1" Then
            MsgBox("Infected Part") 'pg8()
        ElseIf tbxInput.Text = "2" Then
            MsgBox("Milk") 'pg9()
        ElseIf tbxInput.Text = "3" Then
            gameOver("You injected yourself with viper venom.")
        End If
    End Sub

您可能会说我在获取文本框的内容以决定用户下一步将去哪里时遇到问题。我尝试过使用输入框,是的,它可以工作,但是它们有字符限制,我更愿意找到一种方法来使用文本框来做到这一点。我还在考虑使用按键而不是单击按钮的方法。对于初学者的问题,我很抱歉,我仍在学习有关 Visual Basic 的方法。提前谢谢!

【问题讨论】:

  • aryInpur 在哪里声明?
  • 如果您使用单选按钮,用户输入会更可靠。只需更改单选按钮上的文本即可。
  • 您没有让用户有机会在文本框中输入内容。
  • 你被一个 WinForms 应用程序访问,就好像它是一个控制台应用程序一样。 WinForms 是事件驱动的。触发这些潜艇的事件在哪里?
  • 为什么事件使用数组?看来您只使用了第一个元素。

标签: vb.net .net-4.6


【解决方案1】:

我更喜欢单选按钮,因为它更容易控制用户输入。用户可以在文本框中输入任何内容,而您需要处理它不是 1、2 或 3 的可能性。

最初RadioButton3.Visible 在设计时设置为False。如果用户选择开始游戏,它就会变得可见。

我们在Form.Load 开始了整个事情。我声明了一个表单级别的变量来跟踪我们所在的页面,PageNum

我只在 pgX 潜艇中设置了属性。所有操作都在提交按钮中。首先是找到选择了哪个单选按钮。 GetSelectedRadioButton 返回选定的按钮或Nothing。您必须将Me(指的是Form,代码运行所在的类)作为容器传递。单选按钮通常位于 GroupBox 或其他容器控件中,因此允许这样做。

您需要为pg5pg6pg7pg8pg9 编写代码。还要在提交按钮中添加Case 5Case 6Case 7Case 8Case 9

Private PageNum As Integer

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    pgMain()
End Sub

Private Sub pgMain()
    lblMain.Text = "Make a selection and click Submit"
    RadioButton1.Text = "start the game"
    RadioButton2.Text = "quit the game"
End Sub

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    Dim rb As RadioButton = GetSelectedRadioButton(Me)
    If rb IsNot Nothing Then
        Dim Name = rb.Name
        Select Case PageNum
            Case 0
                If Name = "RadioButton1" Then
                    pg1()
                ElseIf Name = "RadioButton2" Then
                    Close()
                End If
            Case 1
                If Name = "RadioButton1" Then
                    MsgBox("Front door") '
                    pg2()
                ElseIf Name = "RadioButton2" Then
                    MsgBox("backdoor")
                    pg3()
                ElseIf Name = "RadioButton3" Then
                    gameOver("You were mauled by wolves")
                End If
            Case 2
                If Name = "RadioButton1" Then
                    pg5() ' They went to the chest
                ElseIf Name = "RadioButton2" Then
                    pg6() ' User went to the bookshelf
                ElseIf Name = "RadioButton3" Then
                    pg7() ' User went to the cauldron
                End If
            Case 3
                If Name = "RadioButton1" Then
                    MsgBox("Infected Part")
                    pg8()
                ElseIf Name = "RadioButton2" Then
                    MsgBox("Milk")
                    pg9()
                ElseIf Name = "RadioButton3" Then
                    gameOver("You injected yourself with viper venom.")
                End If
        End Select
    Else
        MessageBox.Show("Please make a selection.")
    End If
End Sub

Public Function GetSelectedRadioButton(Container As Control) As RadioButton
    Dim rb = Container.Controls.OfType(Of RadioButton)().FirstOrDefault(Function(r) r.Checked = True)
    Return rb
End Function

Private Sub pg1() ' User picks a starting option
    PageNum = 1
    lblTitle.Text = "You spot a secret magical lair do you"
    RadioButton1.Text = "Enter the lair through the front door"
    RadioButton2.Text = "Enter the lair through the back door"
    RadioButton3.Visible = True 'Set to False at design time
    RadioButton3.Text = "Wait untill midnight to enter the lair."
End Sub

Private Sub pg2() ' User entered through the front door.
    PageNum = 2
    lblTitle.Text = "You see a chest, a bookself, and a cauldron"
    RadioButton1.Text = "Go to the chest"
    RadioButton2.Text = "Go to the bookshelf"
    RadioButton3.Text = "Go to the cauldron"
End Sub

Private Sub pg3()
    PageNum = 3
    lblTitle.Text = "You were splashed with a poison spell do you"
    RadioButton1.Text = "Cut off the infected part"
    RadioButton2.Text = "Drink a bucket of milk"
    RadioButton3.Text = "Inject yourself with some sort of medical syringe"
End Sub

Private Sub gameOver(DeathMessage As String)
    lblTitle.Text = "Game Over!"
    lblMain.Text = DeathMessage
    RadioButton1.Visible = False
    RadioButton2.Visible = False
    RadioButton3.Visible = False
    btnSubmit.Visible = False
End Sub

【讨论】:

    猜你喜欢
    • 2016-08-25
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多