VB.net 导航文字冒险游戏

如何解决VB.net 导航文字冒险游戏

我正在开发一款文字冒险游戏。该程序的目标是显示三个选项和一个文本框。用户可以通过在文本框中输入相应的数字来选择其中一个选项,然后应该将用户导航到下一个区域,在那里用户会看到另外 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 的方法。提前致谢!

解决方法

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

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

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

我只在 pgX subs 中设置了属性。所有操作都在提交按钮中。第一件事是找到选择了哪个单选按钮。 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?