Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

having a subroutine wait for button input before ending the sub

I am using a do while loop that after every loop, needs to ask the user what he/she wants to do next. The program is a pokemon style battle rpg, except for letting pokemon fight, you fight the monsters yourself.

What I am looking for is a pokemon/final fantasy style of fighting.

this is my sub that handles the battles:

  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

After the line " What would you like to do", 2 (3) buttons will be visible:

  • Attack
  • Run
  • (Inventory)

However, as you can see (and I do understand what is wrong, I just don't know how to fix it) my battle sub ends after the buttons are made visible.

I want the sub to wait until I have pressed either of these buttons are pressed and then act accordingly.

How can I do this?


Edit:

This is the code I use, where the sub "Battle" is called:

   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

Also, if you think the way I am handling the program now is very wrong, please let me know how to improve it! This is a hobby project to try to learn the vb.net language a bit better, so any advice or constructive critisism is verry appreciated!

like image 499
Gutanoth Avatar asked May 05 '26 05:05

Gutanoth


1 Answers

Buttons events occur when you click the a button which is what it seems you're looking for. I've actually started rewriting pokemon for my own growth and I use the button_click events.

Button_Click subs are subs that run when a button is clicked. When a program is hovering, waiting for an input and a button is clicked, the program will jump into the sub associated with the button that was clicked.

In order to generate the "button_click" sub, you can double click a button when you're viewing the form to automatically generate the "Button_Click" sub. There are other ways, but I personally find them trickier to use and more prone to mistakes.

What will happen here is your "Battle" sub will end and the battle will be set up. The program will be hovering, waiting for something to happen. When you click one of the buttons your program will jump into the sub for whatever button was clicked. It does NOT require the program to be holding in a loop until a button is pressed, and you DON'T have to add any code to your existing battle sub (you can just let it end).

Here's a little example to help get you started. The code that fills the button_click subs is something you'll tweak to your own purposes, but your setup should look very similar to this. It's written is Visual Basic 2010 Express.

Private Sub btn_Attack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Attack.Click
    If mobCurrHealth > 0 And youCurrHealth > 0 Then
        'Both the monster and you are alive
        'Execute attack code
    End If
End Sub

Private Sub btn_Run_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Run.Click
    If mobCurrHealth > 0 And youCurrHealth > 0 Then
        'Both the monster and you are alive
        'Execute run code
    End If
End Sub

The important lines are the sub declaration lines which you'll probably automatically generate. If you don't automatically generate your sub by double clicking the button, make sure that the "Handles" is set to the button you want to activate the sub followed by ".Click". Whatever you want to happen once the button is pressed is up to you. The above example checks to make sure both you and the monster are still alive, but it can be anything.

Good luck with your game development!

like image 142
Paul Avatar answered May 07 '26 03:05

Paul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!