▷ Hangman game in Visual Basic 6.0

In this updated tutorial, you will learn how to create the classic game of hangman using Visual Basic 6.0. We will guide you step by step through the design and implementation process of the game, from the graphical interface to the logic of word control. We will use basic programming concepts such as conditionals, string handling, and events to provide an interactive experience. Ideal for beginners in Visual Basic 6.0.

Form Elements

The form contains the following elements organized in "frames":

  1. Input Frame: Includes a TextBox and a "Play" button. Here you will enter the word that must be between 4 and 13 characters.
  2. Info Frame: Has a Label to display messages such as errors or the game status. There is also a "Restart" button, which appears when the game ends.
  3. Errors Frame: Shows the parts of the hangman's figure when you enter incorrect letters.
  4. Word Frame: Here a matrix of Labels (index from 0 to 13) is displayed where the correct letters are revealed.

Program Operation

When Loading the Form

When the form is loaded, the message "Enter a word and press Play" is displayed in the Label of gameInfo:

Private Sub Form_Load() gameInfo.Caption = "Enter a word and press Play" End Sub

When Pressing the "Play" Button

When you enter a word and press the "Play" button, the labels of the letter container are cleared, the word is validated, and the game starts:

Private Sub play_Click() For i = 0 To 13 letterContainer(i).Text = "" Next i word = wordTextBox.Text wordLength = Len(word) If (word = "") Then gameInfo.Caption = "Error: Please enter a word." ElseIf (wordLength < 4) Then gameInfo.Caption = "Error: Word too short" ElseIf wordLength > 13 Then gameInfo.Caption = "Error: You exceeded the allowed number of letters" wordLength = 0 Else wordInputFrame.Visible = False wordViewFrame.Visible = True gameInfo.Caption = "Start the game!" gameTimer.Enabled = True For i = 0 To wordLength - 1 letterContainer(i).Visible = True Next i End If End Sub

Game Timer

The timer controls the game interactions. Every time you enter a letter, it checks if it is in the word:

Private Sub gameTimer_Timer() letterInput = InputBox("Enter a letter.", "Input", "", 0, 0) If letterInput <> "" Then For i = 1 To wordLength letterInWord = Mid(word, i, 1) If UCase(letterInput) = UCase(letterInWord) Then isLetterInWord = True gameInfo.Caption = "Correct letter: " & letterInput Select Case i Case i letterContainer(i - 1).Text = UCase(letterInput) End Select Else isLetterInWord = False End If Next If isLetterInWord = False Then errors = errors + 1 gameInfo.Caption = "Incorrect letter: " & letterInput Select Case errors Case 1 rope.Visible = True head.Visible = True Case 2 chest.Visible = True Case 3 leftArm.Visible = True Case 4 rightArm.Visible = True Case 5 leftLeg.Visible = True Case 6 rightLeg.Visible = True End Select End If Else gameInfo.Caption = "Error: Enter a letter" End If successes = 0 For i = 0 To wordLength - 1 If letterContainer(i) <> "" Then successes = successes + 1 End If Next If successes = wordLength Then gameTimer.Enabled = False MsgBox "Congratulations, you won!" gameInfo.Caption = "Winner!!" word = "" playAgain.Visible = True End If If errors = 6 Then gameTimer.Enabled = False MsgBox "You lost" gameInfo.Caption = "The word was: " & word word = "" playAgain.Visible = True End If End Sub

Function to Validate Letter Input

The function verifyKey allows only letters to be entered in the TextBox:

Function verifyKey(Tecla_Presionada) Dim allowedKeys As String allowedKeys = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZabcdefghijklmnñopqrstuvwxyz" & Chr(vbKeyBack) If InStr(1, allowedKeys, Chr(Tecla_Presionada)) Then verifyKey = Tecla_Presionada Else verifyKey = 0 End If End Function

Restart the Game

Pressing the "Play Again" button resets the game state:

Private Sub playAgain_Click() wordTextBox.Text = "" wordInputFrame.Visible = True playAgain.Visible = False gameInfo.Caption = "Enter a word and press Play" errors = 0 wordViewFrame.Visible = False rope.Visible = False head.Visible = False chest.Visible = False leftArm.Visible = False rightArm.Visible = False leftLeg.Visible = False rightLeg.Visible = False End Sub

0/Leave a comment/Comments

Hello! We're so glad you've made it this far and are reading this article on Edeptec.

This form is an open space for you: you can leave a comment with your questions, suggestions, experiences, or simply your opinion on the topic discussed.

» Did you find the information helpful?
» Do you have any personal experiences you'd like to share?
» Do you have any topics you'd like to see covered in future articles?

Remember that this space is for learning and sharing, so we encourage you to participate respectfully and constructively. Your comments can help other readers who are on the same path, whether in electronics, programming, sports, or technology.

Thank you for being part of this learning community! Your participation is what makes this project grow.