4 Replies - 1811 Views - Last Post: 04 June 2014 - 11:30 AM Rate Topic: -----

#1 codegirl17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-June 14

Hangman game in VB 2010 help!

Posted 02 June 2014 - 09:26 AM

So in college I am making a hangman game in VB 2010. I will post my code as I think it will be easier than me explaining it. Basically I can't seem to get the 'Start drawing when losing' to work where the hangman should be drawn each time the player guesses a wrong letter. I was wondering if anyone would be able to help me with this as I really have no idea how to get this to work? (I'm new to all this so sorry if it's hard to understand what I mean but I've tried my best).
Public Class frmhangman
    Dim Word As String                      ' will hold the word to be guessed
    Dim WordLength As Integer               ' will hold the length of the word
    Dim Char1 As String                     ' will hold the first letter of the word
    Dim Char2 As String                     ' will hold the second letter of the word
    Dim Char3 As String                     ' will hold the third letter of the word
    Dim Char4 As String                     ' will hold the fourth letter of the word
    Dim Char5 As String                     ' will hold the fifth letter of the word
    Dim Char6 As String                     ' will hold the sixth letter of the word
    Dim GameStarted As Boolean              ' will switch to True when the user click on Play Button
    Dim RightTrys As Integer                ' will hold the number of the right guessed letters
    Dim WrongTrys As Integer                ' will hold the number of the wrong guessed letters


    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        End
        'exits game'.
    End Sub

    Public Sub ShowLength(ByVal VisibleLines As Integer)
        hideCharachterLength()
        If VisibleLines = 1 Then
            cmdletterline1.Visible = True
        ElseIf VisibleLines = 2 Then
            cmdletterline1.Visible = True
            cmdLetterLine2.Visible = True
        ElseIf VisibleLines = 3 Then
            cmdletterline1.Visible = True
            cmdLetterLine2.Visible = True
            cmdLetterLine3.Visible = True
        ElseIf VisibleLines = 4 Then
            cmdletterline1.Visible = True
            cmdLetterLine2.Visible = True
            cmdLetterLine3.Visible = True
            cmdLetterLine4.Visible = True
        ElseIf VisibleLines = 5 Then
            cmdletterline1.Visible = True
            cmdLetterLine2.Visible = True
            cmdLetterLine3.Visible = True
            cmdLetterLine4.Visible = True
            cmdLetterLine5.Visible = True
        ElseIf VisibleLines = 6 Then
            cmdletterline1.Visible = True
            cmdLetterLine2.Visible = True
            cmdLetterLine3.Visible = True
            cmdLetterLine4.Visible = True
            cmdLetterLine5.Visible = True
            cmdLetterLine6.Visible = True
        End If
    End Sub

    Public Sub AssignLetters()
        If WordLength = 2 Then
            Char1 = Word.Chars(0).ToString.ToUpper
            Char2 = Word.Chars(1).ToString.ToUpper
        ElseIf WordLength = 3 Then
            Char1 = Word.Chars(0).ToString.ToUpper
            Char2 = Word.Chars(1).ToString.ToUpper
            Char3 = Word.Chars(2).ToString.ToUpper
        ElseIf WordLength = 4 Then
            Char1 = Word.Chars(0).ToString.ToUpper
            Char2 = Word.Chars(1).ToString.ToUpper
            Char3 = Word.Chars(2).ToString.ToUpper
            Char4 = Word.Chars(3).ToString.ToUpper
        ElseIf WordLength = 5 Then
            Char1 = Word.Chars(0).ToString.ToUpper
            Char2 = Word.Chars(1).ToString.ToUpper
            Char3 = Word.Chars(2).ToString.ToUpper
            Char4 = Word.Chars(3).ToString.ToUpper
            Char5 = Word.Chars(4).ToString.ToUpper
        ElseIf WordLength = 6 Then
            Char1 = Word.Chars(0).ToString.ToUpper
            Char2 = Word.Chars(1).ToString.ToUpper
            Char3 = Word.Chars(2).ToString.ToUpper
            Char4 = Word.Chars(3).ToString.ToUpper
            Char5 = Word.Chars(4).ToString.ToUpper
            Char6 = Word.Chars(5).ToString.ToUpper
        End If
    End Sub
    Public Sub hideCharachterLength()
        cmdletterline1.Visible = False
        cmdLetterLine2.Visible = False
        cmdLetterLine3.Visible = False
        cmdLetterLine4.Visible = False
        cmdLetterLine5.Visible = False
        cmdLetterLine6.Visible = False
        'hides the letter lines'.
    End Sub
    Public Sub StartDrawingWhenLosing(ByVal trys As Integer)
        Dim pnt As Graphics
        pnt = Me.CreateGraphics
        Dim pencil As New Pen(Color.Black, 2)
        If wrongtrys = 1 Then
            pnt.DrawLine(pencil, 40, 350, 40, 40) 'gallows up line'
            pnt.DrawLine(pencil, 40, 40, 200, 40) 'gallows top line' 
            pnt.DrawLine(pencil, 10, 350, 248, 350) 'gallows bottom line'
        ElseIf wrongtrys = 2 Then
            pnt.DrawLine(pencil, 200, 140, 200, 40) 'rope'  
            pnt.DrawEllipse(pencil, 180, 140, 40, 40) 'Head' 
            pnt.DrawLine(pencil, 200, 179, 200, 240) 'body'
        ElseIf wrongtrys = 3 Then
            pnt.DrawLine(pencil, 200, 184, 180, 210) 'left arm' 
            pnt.DrawLine(pencil, 220, 212, 200, 186) 'right arm' 
        ElseIf wrongtrys = 4 Then
            pnt.DrawLine(pencil, 200, 238, 180, 264) 'left leg'
            pnt.DrawLine(pencil, 220, 264, 200, 238) 'right leg'
        ElseIf wrongtrys = 5 Then
            pnt.DrawLine(pencil, 206, 154, 206, 157) 'right eye' 
            pnt.DrawLine(pencil, 194, 154, 194, 157) 'left eye'
        ElseIf wrongtrys = 6 Then
            pnt.DrawLine(pencil, 200, 156, 200, 164) 'nose'
            pnt.DrawLine(pencil, 194, 168, 206, 168) 'Mouth'
            TextBox1.Clear()
            Try
                lblletter1.Text = Char1
                lblletter2.Text = Char2
                lblletter3.Text = Char3
                lblletter4.Text = Char4
                lblletter5.Text = Char5
                lblletter6.Text = Char6
            Catch ex As Exception
            End Try
            MsgBox("You Lose!!")
        End If
    End Sub
    Public Sub clearLetters()
        lblletter1.Text = ""
        lblletter2.Text = ""
        lblletter3.Text = ""
        lblletter4.Text = ""
        lblletter5.Text = ""
        lblletter6.Text = ""
        'Clears all the letters'.
    End Sub
    Public Sub CheckForWinning()
        If RightTrys = WordLength Then
            MsgBox("You Won")
            'checks word length to see if you win. If win then a message box will display saying "you won"'. 
        End If
    End Sub
    Private Sub frmhangman_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        listbox.Items.Insert(0, "Colors")
        listbox.Items.Insert(1, "Errors")
        listbox.Items.Insert(2, "Design")
        listbox.Items.Insert(3, "String")
        listbox.Items.Insert(4, "Coding")
        listbox.Items.Insert(5, "Scopes")
        listbox.Items.Insert(6, "Debugs")
        listbox.Items.Insert(7, "Output")
        listbox.Items.Insert(8, "Inputs")
        listbox.Items.Insert(9, "Events")
        'puts words into the listbox'
        Char1 = ""
        Char2 = ""
        Char3 = ""
        Char4 = ""
        Char5 = ""
        Char6 = ""
        Dim number As Integer = CInt(Int((9 * Rnd())))
        Randomize()
        number = Int(Rnd() * listbox.Items.Count - 1) + 1
        Word = listbox.Items(number)
        Label1.Text = Word
        WordLength = Word.Length
        ShowLength(Word.Length)
        clearLetters()
        AssignLetters()
        TextBox1.Focus()
        RightTrys = 0
        WrongTrys = 0
        lblLetterUsed.Text = "Wrong Guesses: "
        GameStarted = True
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If TextBox1.Text.ToUpper.Contains(Char1) And lblletter1.Text = "" And Char1 <> "" Then
            lblletter1.Text = Char1
            RightTrys += 1
            CheckForWinning()
        ElseIf TextBox1.Text.ToUpper.Contains(Char2) And lblletter2.Text = "" And Char2 <> "" Then
            lblletter2.Text = Char2
            RightTrys += 1
            CheckForWinning()
        ElseIf TextBox1.Text.ToUpper.Contains(Char3) And lblletter3.Text = "" And Char3 <> "" Then
            lblletter3.Text = Char3
            RightTrys += 1
            CheckForWinning()
        ElseIf TextBox1.Text.ToUpper.Contains(Char4) And lblletter4.Text = "" And Char4 <> "" Then
            lblletter4.Text = Char4
            RightTrys += 1
            CheckForWinning()
        ElseIf TextBox1.Text.ToUpper.Contains(Char5) And lblletter5.Text = "" And Char5 <> "" Then
            lblletter5.Text = Char5
            RightTrys += 1
            CheckForWinning()
        ElseIf TextBox1.Text.ToUpper.Contains(Char6) And lblletter6.Text = "" And Char6 <> "" Then
            lblletter6.Text = Char6
            RightTrys += 1
            CheckForWinning()
        End If
        TextBox1.Text = ""
    End Sub
End Class


Thanks for any help that you can offer me:)

This post has been edited by modi123_1: 02 June 2014 - 09:29 AM
Reason for edit:: in the future use the 'code' tag button in the editor


Is This A Good Question/Topic? 0
  • +

Replies To: Hangman game in VB 2010 help!

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13495
  • View blog
  • Posts: 53,911
  • Joined: 12-June 08

Re: Hangman game in VB 2010 help!

Posted 02 June 2014 - 09:30 AM

Quote

Basically I can't seem to get the 'Start drawing when losing' to work where the hangman should be drawn each time the player guesses a wrong letter.


Where are you calling this? Where *SHOULD* you be calling it? Perhaps after a user input event?
089	    Public Sub StartDrawingWhenLosing(ByVal trys As Integer)

Was This Post Helpful? 0
  • +
  • -

#3 codegirl17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-June 14

Re: Hangman game in VB 2010 help!

Posted 02 June 2014 - 09:44 AM

View Postmodi123_1, on 02 June 2014 - 09:30 AM, said:

Quote

Basically I can't seem to get the 'Start drawing when losing' to work where the hangman should be drawn each time the player guesses a wrong letter.


Where are you calling this? Where *SHOULD* you be calling it? Perhaps after a user input event?
089	    Public Sub StartDrawingWhenLosing(ByVal trys As Integer)


the only user input is the user inputting a letter into the textbox. would you suggest that I put the 'Public Sub StartDrawingWhenLosing(ByVal trys As Integer) code after the user input?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13495
  • View blog
  • Posts: 53,911
  • Joined: 12-June 08

Re: Hangman game in VB 2010 help!

Posted 02 June 2014 - 09:47 AM

I am not sure what you mean by 'after the user input', but I would suggest making a call - to the StartDrawingWhenLosing method - in that event (assuming that is the only point where the user triggers an action from them entering a letter).
Was This Post Helpful? 0
  • +
  • -

#5 codegirl17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-June 14

Re: Hangman game in VB 2010 help!

Posted 04 June 2014 - 11:30 AM

View Postmodi123_1, on 02 June 2014 - 09:47 AM, said:

I am not sure what you mean by 'after the user input', but I would suggest making a call - to the StartDrawingWhenLosing method - in that event (assuming that is the only point where the user triggers an action from them entering a letter).


Thank you for your help. I now have the hangman game functioning:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1