# Visual Basic > Games and Graphics Programming > Game Demos >  [VB.Net]Hangman

## dday9

This is the source code and the .exe

*Features:*
Plays hangman

*Drawbacks:*
Not enough words in my word list :P

*Notes:*
It's a fun game.

*Full Project:*
hangman.zip

*Source Code:*


```
Option Strict On
Option Explicit On
Public Class Form1
    Private word_list() As String
    Private word As String
    Private r As New Random
    Private damage As Single

#Region "Button Clicks"

    Private Sub btn_click(ByVal btn As Button)
        btn.Enabled = False

        Call check(btn.Text)

    End Sub

    Private Sub Buttons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click, Button8.Click, Button7.Click, Button6.Click, Button5.Click, Button4.Click, Button3.Click, Button26.Click, Button25.Click, Button24.Click, Button23.Click, Button22.Click, Button21.Click, Button20.Click, Button2.Click, Button19.Click, Button18.Click, Button17.Click, Button16.Click, Button15.Click, Button14.Click, Button13.Click, Button12.Click, Button11.Click, Button10.Click, Button1.Click
        Dim btn As Button = DirectCast(sender, Button)

        Call btn_click(btn)
    End Sub

    Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        For Each btn As Button In Panel1.Controls.OfType(Of Button)()
            If e.KeyCode.ToString.ToLower = btn.Text.ToLower Then
                Call btn_click(btn)
            End If
        Next
    End Sub

#End Region

#Region "Check Guess and New Game"

    Private Sub check(ByVal letter As String)
        If word.Contains(letter.ToLower) Then
            'Match
            Dim indexes As New List(Of Integer)
            For i As Integer = 0 To word.Length - 1
                If word.Substring(i, 1).ToLower = letter.ToLower Then
                    indexes.Add(i)
                End If
            Next

            For Each Int As Integer In indexes
                TextBox1.Text = TextBox1.Text.Remove(Int * 4, 4).Insert(Int * 4, letter.ToUpper & "   ")
            Next

            If TextBox1.Text.Contains("_") = False Then
                MessageBox.Show("Congratulations, you've won!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Call newgame()
            End If

        Else
            'Not-a-Match
            damage += 1

            Select Case damage
                Case 1
                    Head.Visible = True
                Case 2
                    body.Visible = True
                Case 3
                    left_arm.Visible = True
                Case 4
                    right_arm.Visible = True
                Case 5
                    left_leg.Visible = True
                Case 6
                    right_leg.Visible = True
                    MessageBox.Show("Sorry, but you lost. The word was: " & word, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Call newgame()
            End Select
        End If
    End Sub

    Private Sub newgame()
        'Reset Body
        Head.Visible = False
        body.Visible = False
        left_arm.Visible = False
        right_arm.Visible = False
        left_leg.Visible = False
        right_leg.Visible = False

        'No more damage
        damage = 0

        'Choose random word
        Dim i As Integer = r.Next(0, word_list.Count)
        word = word_list(i)

        'Decrypt word
        TextBox1.Clear()
        For int As Integer = 0 To word.Length - 1
            TextBox1.Text &= "__  "
        Next

        'Reset button
        For Each btn As Button In Panel1.Controls.OfType(Of Button)()
            btn.Enabled = True
        Next

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim del() As String = {Environment.NewLine}
        word_list = My.Resources.word_file.Split(del, StringSplitOptions.RemoveEmptyEntries)

        Call newgame()
    End Sub

End Class
```

To compile the source you must have:
One(1) oval shape named headFive(5) line shapes named: body, left_arm, right_arm, left_leg, and right_legOne(1) textbox, default nameOne(1) panel, default nameTwenty-Six(26) buttons, default name, inside panel1One(1) text file in My.Resources named: word_file

----------


## NancyMerrison

Hy,

Hangman, the game of our childhood  :Big Grin:

----------


## dday9

Yep, it was just something simple that was whipped together. Eventually I'd like to spice it up with some fancy graphics, but for now it's OK ;]

----------

