Technology Programming

How to Build a Hangman Game in Visual Basic

    • 1). Start a new project in Microsoft Visual Basic and create a new Windows Form. This will be the main user interface and the only form that needs to be created. On this form place 26 labels or buttons, one for each letter, in a control array arranged in a nice grid on the right side of the form. Place a single picture box control on the left side of the form. Along the bottom, place label controls, one for each blank space, with enough for the letters in the longest word. The text of each control will be an underscore character. Also at the bottom, place a "Quit" button and a "New Game" button. Create an imagelist control on the form and load it with images of the different stages of the hanged man, from empty gallows at the start to a fully drawn man and "Game over" at the end. Finally, create a global string variable that will store the word to use for the current game.

    • 2). Create a list of words that will be used and save them in a text file in the same folder as the project. In the "Form_Load" event of the main form, add code that will load the text file into an array of strings. If using Visual Basic 6.0, the code to read the text file would be something like this:

      Open <word file name and path> for Input as #1
      While not EOF(1)
      sWord = line input #1
      Wend
      Close(1)

    • 3). Set up the user interface for the first game. In a loop, set all of the letter buttons to be enabled. Set all of the blank letter labels to be hidden and set the picture control to be the empty gallows image from the imagelist.

    • 4). Add an event handler for the "New Game" button. In it, pick a random number between 1 and the number of elements in the word array, then get the word at that array position. Find the length of the word and make that many of the blank letter labels visible. Set all of the blank letter labels to be hidden and set the picture control to be the empty gallows image from the imagelist. This was done at the load of the form as well. To save coding, the "Form_Load" event can be made to simply call the new game "Click Event" so that loading the form automatically starts a new game.

    • 5). Add a click event to the control array for the letter buttons. In the event handler for the button, check if the current game word contains that letter. If it does, change the label for the letter position from an underscore to the selected letter. Then set the enabled property of the clicked button to "False" so it cannot be clicked again. This will also be a visual indication of the letters that have been tried. If the selected letter is not in the game word, increment the image in the picture box to the next image in the image list. If the next image is the "Game over" image, then the game has ended and all of the letter buttons should be set to disabled.

    • 6). Add a click event to the "Quit" button that just ends the program (using Visual Basic's "End" statement).

Related posts "Technology : Programming"

How LiteData Creates Better Sites For Their Clients

Programming

Contrastive analysis of methodologies of test design for functional testing

Programming

A Proper Diet That Is Filled With Healthy Fats Can Help You Lose Weight

Programming

How to Create an Array of Objects in PHP

Programming

How to read character information from the World of Warcraft servers.

Programming

7 Ways To Master Ayurvedic Medicine For Constipation Without Breaking A Sweat

Programming

Web Design Company Kolkata for Seamless Ecommerce Site

Programming

What Happened to "Borland Delphi"? What is CodeGear? What is Embarcadero?

Programming

How to Send Pages to iFrame

Programming

Leave a Comment