简体   繁体   中英

VB.Net Hangman Game Multiple Rounds

I need some help.

I'm currently creating a Hangman Game in VB.Net.

I'm at the stage where a random word is loaded in from a text file of 6 words, and you can click buttons to guess it. If you guess letters wrong, the frame is shown etc, if they're right, the letter shows in the word through labels.

The next bit, that I'm stuck on, is that multiple rounds are needed. I need there to be 3 turns of this hangman game, so, if you guess the word right you get 10 points, and if you fail, you get 0, And then the game resets with your points and you can play again in Turn 2. Then again in Turn 3, and after Turn 3 finishes, the High Score Form is loaded.

You could create a variable to hold the current round in your Module, at the beginning of each round increase it, and at the end of the round check the current round and make a if then logic.

Dim myRound as Integer = 0

And in the PlayForm in the constructor.

myRound += 1

Once the round is complete.

if myRound >= 3 Then
    'open the score page
Else
    'start the next round
End if
'In General Declarations:
Dim ButtonList As New List(Of Button)  'or Of Control if you have other types of controls
Dim HgmList As New List(Of PowerPacks.Shape)
Dim AnswerList As New List(Of Label)    

'In PlayForm_Load:
With ButtonList
    .Clear()
    .Add(Me.BtnA)
    .Add(Me.BtnB)
    .Add(Me.BtnC)
    'You get the idea
    'Add all your buttons you want to re-enable to the list
End With
With HgmList
    .Clear()
    .Add(Me.SideFrameLine)
    .Add(Me.TopFrameLine)
    .Add(Me.CornerFrameLine)
    'etc.
End With
With AnswerList
    .Add(Me.FirstLetterLbl)
    'etc. Just like the other two.
End With

'At the end of your `If Correct = False Then` Block:
Else  'Check for win after each correct guess
    Dim Winner As Boolean = True
    Dim CheckLetter As Label
    For Each CheckLetter in AnswerList
        If Not CheckLetter.Visible Then
            Winner = False
            Exit For
        End If
    Next
    If Winner Then
        NextRound(10)
    End If
End If

'Somewhere inside your form code:
Private Sub NextRound(RoundScore As Integer)
    UserScore += RoundScore
    If TurnNumber = 3 Then 'Game Over
        MsgBox("Game Over" & vbNewLine & "Score: " & UserScore)
    Else 'This is the part you asked about: resetting the form
        TurnNumber += 1
        PlayForm_Load(Nothing, Nothing)
        Dim ResetControl As Control
        Dim ResetShape As PowerPacks.Shape
        For Each ResetControl In ButtonList
            ResetControl.Enabled = True
        Next
        For Each ResetShape In HgmList
            ResetControl.Visible = False
        Next
    End If
End Sub

I only added the .Clear() to the list builders because you already have your code to get a new word in PlayForm_Load. If you move it (say to a new Sub called NewWord), you don't need .Clear, and you would call your new sub instead of PlayForm_Load.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM