问题描述
我正在开发一款文字冒险游戏。该程序的目标是显示三个选项和一个文本框。用户可以通过在文本框中输入相应的数字来选择其中一个选项,然后应该将用户导航到下一个区域,在那里用户会看到另外 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
或其他容器控件中找到单选按钮,因此这允许这样做。
您需要为 pg5
、pg6
、pg7
、pg8
和 pg9
编写代码。还将 Case 5
、Case 6
、Case 7
、Case 8
和 Case 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