|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hangman Game
I created a Form for a Hangman Game, it's very basic, there's no actual hangman image. I wrote the code and it worked but there was no "3 strikes you're out" type of thing, you could just keep clicking letters until you solved the puzzle. I tried to make it so when you click a wrong letter, an X appears on the screen. I want a box to pop up and say Game Over when there are 6 Xs visible at the top of the screen, but I can't figure it out. The file is on my website: http://home.mchsi.com/~sissipoo246/Hangman.exe )
|
|
#2
|
|||
|
|||
|
Use a counter which is incremented by 1 whenever X increases. Whenever X increases, an If clause should be used which checks if the value of X = 6 which has the following codes (which can be modified to suit your program) as shown below within the tags [Code][\Code].
[Code] If Xcounter = 6 Then MsgBox "GAME OVER", vbOKOnly, "HANGMAN" Exit Sub End If [\Code] |
|
#3
|
|||
|
|||
|
Erm, I'm extremely new to Visual Basic, I have no clue how to create a counter (Let alone make one that goes up every time another X shows up.) What you said makes sense, but I just haven't learned all that much about programming.
|
|
#4
|
|||
|
|||
|
Quote:
You will need to make a global variable ie. Xcounter , at the beginning of the form ... not inside a subroutine (Dim Xcounter) and add 1 to the variable each time that the user fail (Xcounter =Xcounter +1) |
|
#5
|
||||
|
||||
|
If you are still confused just post your code and I will illustrate how to implement the suggestions you have been given.
|
|
#6
|
|||
|
|||
|
Dim Xcounter
Private Sub A1_Click() Text1.Text = "Correct!" LabelA.Caption = "A" A1.Enabled = False End Sub Private Sub B1_Click() Text1.Text = "Correct!" LabelB.Caption = "B" B1.Enabled = False End Sub Private Sub C1_Click() Text1.Text = "Incorrect." CLabel.Visible = True C1.Enabled = False Xcounter = Xcounter + 1 If Xcounter = 6 Then MsgBox "GAME OVER", vbOKOnly, "HANGMAN" Exit Sub End If End Sub That's the first bit. The rest looks the same, but different letters and whatnot. The whole counter thing works, really well. (Thank you!) Now my problem is that I want to make it so, when it comes up GAME OVER the rest of the letters become visible, so you can see the answer. If seeing how it works now would help you help me... http://home.mchsi.com/~sissipoo246/Hangman.exe |
|
#7
|
||||
|
||||
|
Hi,
All you have to do is to make the labels visible, I dont know the names of your labels so you will have to change the names accordingly. I believe most of them are correct but because there are several E's they may be LabelE1, LabelE2 etc.: Code:
Private Sub C1_Click() Text1.Text = "Incorrect." CLabel.Visible = True C1.Enabled = False Xcounter = Xcounter + 1 If Xcounter = 6 Then MsgBox "GAME OVER", vbOKOnly, "HANGMAN" LabelB.Caption = "B" LabelE.Caption = "E" LabelS.Caption = "S" LabelT.Caption = "T" LabelD.Caption = "D" LabelA.Caption = "A" LabelD.Caption = "D" LabelE.Caption = "E" LabelV.Caption = "V" LabelE.Caption = "E" LabelR.Caption = "R" Exit Sub End If End Sub |
|
#8
|
||||
|
||||
|
Hi,
Just thought I'd throw something together to demonstrate how to use arrays to allow multiple questions instead of just one possibility. The basis of the project is two control arrays, one for the labels on the form which will display the blanks/letters and one for the command buttons which allow the user to guess. To add a control array you just put a control (eg. a button) on the form and right click on it and select "Copy", then "paste" it anywhere on the form. You will see a message saying "do you want to create a control array?", click OK. You can now paste as many command buttons as you want (in this case 26, 1 for each letter of the alphabet!!), each one will have the same name but with a subscript which identifies it. Now, instead of a seperate subroutine for each of your 26 command buttons you just need one and you can identify the letter selected by the index of the button. The simplified code now looks like this: Code:
Option Explicit
Dim arrayofanswers(10) As String
Dim givenanswer() As String
Dim wronganswers As Integer
Dim ok As Boolean
Dim ano As Double
Dim ano2 As Integer
Dim a As Integer
Public Sub dosearch(x As String)
Dim b As Integer
ok = False
Dim g As Integer
g = 0
For b = 0 To UBound(givenanswer) - 1
If UCase(givenanswer(b)) = x Then
g = g + 1
Label3(b) = x
ok = True
End If
Next
If g >= 1 Then
If g = 1 Then
Label2.Caption = "Well Done, there is 1 " & x
Else
Label2.Caption = "Well Done, there are " & g & " " & x & "s"
End If
Else
Label2.Caption = "Sorry, there are no " & x & "s"
End If
If ok = False Then
Label1.Caption = Label1.Caption & " X "
wronganswers = wronganswers + 1
Dim tempstring As String
Dim k As Integer
For k = 0 To UBound(givenanswer) - 1
tempstring = tempstring & givenanswer(k)
Next
Dim vbQuote As String
vbQuote = Chr(34)
Dim prompt$
prompt$ = "Game Over - The Correct Answer Was " & vbQuote & tempstring & vbQuote
If wronganswers = 6 Then MsgBox (prompt$)
End If
End Sub
Private Sub Command1_Click(Index As Integer)
Command1(Index).Enabled = False
Select Case Index
Case Is = 0
Call dosearch("A")
Case Is = 1
Call dosearch("B")
Case Is = 2
Call dosearch("C")
Case Is = 3
Call dosearch("D")
Case Is = 4
Call dosearch("E")
Case Is = 5
Call dosearch("F")
Case Is = 6
Call dosearch("G")
Case Is = 7
Call dosearch("H")
Case Is = 8
Call dosearch("I")
Case Is = 9
Call dosearch("J")
Case Is = 10
Call dosearch("K")
Case Is = 11
Call dosearch("L")
Case Is = 12
Call dosearch("M")
Case Is = 13
Call dosearch("N")
Case Is = 14
Call dosearch("O")
Case Is = 15
Call dosearch("P")
Case Is = 16
Call dosearch("Q")
Case Is = 17
Call dosearch("R")
Case Is = 18
Call dosearch("S")
Case Is = 19
Call dosearch("T")
Case Is = 20
Call dosearch("U")
Case Is = 21
Call dosearch("V")
Case Is = 22
Call dosearch("W")
Case Is = 23
Call dosearch("X")
Case Is = 24
Call dosearch("Y")
Case Is = 25
Call dosearch("Z")
End Select
End Sub
Private Sub Command2_Click()
Call getrandomquestion
wronganswers = 0
For a = 0 To 25
Command1(a).Enabled = True
If a < 13 Then Label3(a).Caption = " "
Label1.Caption = ""
Label2.Caption = ""
Next
End Sub
Private Sub Form_Load()
wronganswers = 0
arrayofanswers(0) = "aaaaaaaaaaaa"
arrayofanswers(1) = "bbbbbbbbbbbb"
arrayofanswers(2) = "cccccccccccc"
arrayofanswers(3) = "ddd d d d dd"
arrayofanswers(4) = "eeeeeeeeeeee"
arrayofanswers(5) = "etc"
arrayofanswers(6) = "best dad ever"
arrayofanswers(7) = ""
arrayofanswers(8) = ""
arrayofanswers(9) = ""
Randomize
Call getrandomquestion
End Sub
Private Sub getrandomquestion()
ano = Rnd
ano2 = ano * 9
For a = 1 To 12
Label3(a).Visible = False
Next
ReDim givenanswer(Len(arrayofanswers(ano2)))
For a = 1 To Len(arrayofanswers(ano2))
givenanswer(a - 1) = Left(arrayofanswers(ano2), a)
givenanswer(a - 1) = Right$(givenanswer(a - 1), 1)
If givenanswer(a - 1) = " " Then
Label3(a - 1).Visible = False
Else
Label3(a - 1).Visible = True
End If
Next
End Sub
Last edited by sync_or_swim : June 20th, 2006 at 10:38 AM. Reason: Removed Attachment due to Problems - Samantha, if you haven't downloaded the sample project let me know and I will attach it again for you |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > Hangman Game |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|