当前位置 : 主页 > 编程语言 > c语言 >

vb.net – 在结束sub之前让子程序等待按钮输入

来源:互联网 收集:自由互联 发布时间:2021-06-24
我正在使用do while循环,在每次循环之后,需要询问用户他/她下一步要做什么.该节目是一个口袋妖怪风格的战斗RPG,除了让口袋妖怪打架,你自己打怪物. 我正在寻找的是口袋妖怪/最终幻想
我正在使用do while循环,在每次循环之后,需要询问用户他/她下一步要做什么.该节目是一个口袋妖怪风格的战斗RPG,除了让口袋妖怪打架,你自己打怪物.

我正在寻找的是口袋妖怪/最终幻想的战斗风格.

这是我处理战斗的子:

Private Sub Battle(ByVal Name As String, ByVal life As Integer, ByVal damage As Integer)

    lbl_MonsterName.Text = Name
    mobCurrHealth = mobMaxHealth
    lbl_MonsterHP.Text = mobCurrHealth & " / " & mobMaxHealth
    bar_monsterHP.Value = mobCurrHealth
    bar_monsterHP.Maximum = mobMaxHealth


    txtbx_Action.AppendText(Environment.NewLine & "You find a " & Name)
    lbl_MonsterName.Visible = True
    lbl_MonsterHP.Visible = True
    bar_monsterHP.Visible = True

    wait(2000)

    txtbx_Action.AppendText(Environment.NewLine & "What would you like to do?")
    btn_Attack.Visible = True
    btn_Run.Visible = True

'Here I want the program to wait for button input, but i still have to find a way to do that        


End Sub

在“您想做什么”之后,将显示2(3)个按钮:

>攻击
>跑
>(库存)

但是,正如你所看到的那样(而且我确实理解了什么是错的,我只是不知道如何解决它)我的战斗子按钮在按钮可见后结束.

我希望子等待,直到我按下这些按钮中的任何一个按下,然后相应地采取行动.

我怎样才能做到这一点?

编辑:

这是我使用的代码,其中称为“战斗”子:

Private Sub btn_Adventure_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Adventure.Click

    btn_Adventure.Visible = False

    txtbx_Action.Visible = True

    Dim rng As New Random
    Dim MobGen As Integer = rng.Next(1, 4)

    Select Case MobGen

        Case 1
            mobName = "Rat"
            mobMaxHealth = 5
            mobDamage = 2
        Case 2
            mobName = "Bat"
            mobMaxHealth = 7
            mobDamage = 3
        Case 3
            mobName = "Snake"
            mobMaxHealth = 9
            mobDamage = 4
        Case 4
            mobName = "Wolf"
            mobMaxHealth = 11
            mobDamage = 5

    End Select

    txtbx_Action.AppendText(Environment.NewLine & "You run around the forest trying to find something to kill")
    wait(1500)
    Battle(mobName, mobMaxHealth, mobDamage)


    btn_Adventure.Visible = True

End Sub

另外,如果您认为我现在处理程序的方式非常错误,请告诉我如何改进它!这是一个尝试更好地学习vb.net语言的爱好项目,因此任何建议或建设性批评都非常赞赏!

我在这个完整的例子中避免了绑定和其他事情,试图让它更容易理解并尽可能地保持它.

Public AllMobs As New List(Of Mob)
Public Player As Mob
Public CurrentMob As Mob
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    AllMobs.Add(New Mob() With {.Name = "rat", .Damage = 2, .MaxHealth = 5, .CurrentHealth = 5})
    AllMobs.Add(New Mob() With {.Name = "bat", .Damage = 3, .MaxHealth = 7, .CurrentHealth = 7})
    AllMobs.Add(New Mob() With {.Name = "wolf", .Damage = 5, .MaxHealth = 11, .CurrentHealth = 11})
    Player = New Mob() With {.Name = "Gutanoth", .Damage = 1, .MaxHealth = 100, .CurrentHealth = 100}
    GetNextFight()
End Sub
Private Sub GetNextFight()
    If CurrentMob Is Nothing Then
        CurrentMob = SpawnMob()
        txtbx_Action.AppendText("You continue into the forest" & vbNewLine)
        txtbx_Action.AppendText(CurrentMob.Name + " is here" & vbNewLine)
    End If
End Sub
Private Sub NextBattleAction()
   If Player.IsDead Then
        MessageBox.Show("You died. Lose!")
        Close()
    ElseIf CurrentMob.IsDead Then
        txtbx_Action.AppendText("---Victorious!" & vbNewLine)
        AllMobs.Remove(CurrentMob)
        CurrentMob = Nothing ' destroy mob object
    End If
    If AllMobs.Count = 0 Then
        MessageBox.Show("You've killed them all. Win!")
        Close()
    Else
        GetNextFight()
    End If
    If CurrentMob IsNot Nothing Then
        lbl_MonsterHP.Text = CurrentMob.CurrentHealth & " / " & CurrentMob.MaxHealth
    End If
End Sub
Private Function SpawnMob() As Mob
    If AllMobs.Count = 0 Then Return Nothing
    Dim rng As New Random
    Dim selectedIndex As Integer = rng.Next(AllMobs.Count)
    Return AllMobs(selectedIndex)
End Function
Private Sub btn_Attack_Click(sender As Object, e As EventArgs) Handles btn_Attack.Click
    CurrentMob.Attack(Player)
    txtbx_Action.AppendText(CurrentMob.Name & " attacks" & vbNewLine)
    Player.Attack(CurrentMob)
    txtbx_Action.AppendText("you attack " & vbNewLine)
    NextBattleAction()
End Sub
Private Sub btn_Run_Click(sender As Object, e As EventArgs) Handles btn_Run.Click
    txtbx_Action.AppendText("you run away" & vbNewLine)
    CurrentMob = Nothing ' make no current mob 
    GetNextFight()
End Sub

这应该放在一个单独的类文件Mob.vb中

Public Class Mob
    Property Name() As String
    Property MaxHealth() As Integer
    Property CurrentHealth() As Integer
    Property Damage As Integer
    Public Sub Attack(targetMob As Mob)
        targetMob.CurrentHealth = targetMob.CurrentHealth - Damage
    End Sub
    Public Function IsDead() As Boolean
        If CurrentHealth <= 0 Then Return True Else Return False
    End Function
End Class
网友评论