Tuesday, July 12, 2011

Visual Studio Hangman Game Tutorial

Hello and welcome to my blog. Right now I'm writing a tutorial on how to make a simple Hangman game in Visual Studio 2010 using Visual Basic.

You can view what I have so far here:

Hangman Game VB.NET

26 comments:

  1. Hey, first off, great tutorial! Well written and explained thoroughly! One question though. If I wanted to implement underscores as placeholders for the word, how/where would I do so?

    For example, the word is "Words" I would like it to show _ _ _ _ _ and then replace the blanks with the right letters as they are guessed. It helps the player to make better guesses based on the length of the word. Thanks again!

    ReplyDelete
  2. Hey, Bob thanks for commenting.

    In the 'New game' sub we had an if statement which looked like this:

    For Each labelitm As Label In GroupBox2.Controls

    If labelitm.Text = "" Then

    Else

    labelitm.Visible = False

    End If

    Next

    Instead of hiding the labels with labelitm.visible = false we can use: labelitm.text = "_" to show an underscore instead.

    ReplyDelete
  3. Works a treat! Thanks for the quick reply!

    Any more tutorials coming up soon?

    ReplyDelete
  4. I was thinking of maybe adding an additional "extras" part where I add things like showing which word it was in a messagebox in case the player looses and adding the ability to include the dictionary in the .exe file among other things.

    But after that I'll add more tutorials. If you have any requests then please post them.

    ReplyDelete
  5. So back to the underscore thing. I have found a couple of problems with just replacing visible = false with .text = "_" First, if statement in labelCheck() will pass on the first try and announce a win, because the condition is met immediately. I overcame this by changing that If statement to

    If lblWordL1.Text <> "_" And lblWordL2.Text <> "_" And lblWordL3.Text <> "_" And lblWordL4.Text <> "_" And lblWordL5.Text <> "_" And
    lblWordL6.Text <> "_" And lblWordL7.Text <> "_" And lblWordL8.Text <> "_" Then

    This works well. Then you run into the problem of matching each letter. As it stands, on button click it assigns the corresponding letter to 'a' and then checks to see if a = labelitm.text This condition will never be met, now that we have replaced the actual letter with an "_". Maybe we could check the actual array and position rather than the text property of the labels... Thoughts?

    ReplyDelete
  6. Hmm you're right that kind if messes up the code.

    You an use "tags" to associate data with an object such as a letterbox.

    So instead of saying "labelitm.text = letters(idxnum)" where we insert the letters into the label we'll say "labelitm.tag = letters(idxnum)"

    This won't display the letter in the label but will associate the letter with the label.

    Remember to change the condition to fit this new method as well.

    ReplyDelete
  7. Hey Mike, just wanted you to know that I was able to get all the bugs worked out, and added a ton of features to the Hangman game. If you want to check it out, visit www.action-bob.com and click VB Hangman. Thanks for the great Tut!

    ReplyDelete
  8. Looks good, I like the synonym feature, how did you do that?

    I'm writing a tut on making Pong. So, check back in the next couple of days.

    ReplyDelete
  9. I did the synonym thing by first adding 13 additional word lists to the program. The "hint" you see at the top is based on which word list the word was chosen from. If it was chosen from the Animals list, the hint will read "It's an Animal". I also implemented an array that every word chosen is added to it so you never get the same word twice during a game. Is there a way I can contribute tutorials to this site? Would love to redo your hangman game as a c# tutorial.

    ReplyDelete
  10. Yes sure if you want. You could send it to me and I would put int in this blog.

    You can also provide a link to your site from it if you want.

    ReplyDelete
  11. The hangman.zip i can`t download , i want it

    ReplyDelete
  12. my mail is SymphonyDay@gmail.com

    ReplyDelete
  13. Hello Mike,
    Thanks for this great tut, but i`m a beginner and i have some problems.

    When i play the game and i have take a wrong key the pictures doesn`t change.

    And when i click on "new game" then de buttons are not coming back. I have to try to put your code the next code: For Each btn As Button In GroupBox1.Controlsbtn.Enable = True Next
    Under the private sub from the "New Game"

    But then visual basic 2010 give message:Controlsbtn' is not a member of 'System.Windows.Forms.GroupBox

    can you help me to solve this problems?

    ReplyDelete
  14. hiya, this program does not work for me fully.. When i select the letters to guess the word only like one letter will actually show up on screen.. and the buttons dont reset when they are suppose to... i just need some help.. would really appreciate it if you could help me out with this..

    and ive checked through the whole code to see that im not doing anything wrong and i cant find anything that is wrong..

    p.s. Really good tutorial :)

    ReplyDelete
    Replies
    1. i have the same problem, and i have to place this code under
      if file.Exits

      For Each button As Button In GroupBox1.Controls
      button.Enabled = True
      and it works.

      But my pictures doesn`t change if i have push a wrong key. Do you have also that problem?

      Delete
    2. To get your picture to change when a wrong key is pressed, first check the condition of the keypress, and fire the method that controls the picture of the gallows. Increment or decrement the gallows photos based on the photo that was previously loaded. Mike's tutorial outlines this in detail, just make sure you follow it exactly. I followed the tutorial, and was able to get this up and running with minor issues, and added tons of functionality to the game afterwards. Please feel free to visit my website at www.action-bob.com and click VB Hangman Game to see my changes. Good luck guys, and keep posting if you have questions!!

      Delete
  15. hey, I'm stuck on this one little problem..

    so far i have completed everything correct until this:

    For Each btn As Button In GroupBox1.Controlsbtn.Enabled = TrueNext

    I wrote it where i have to, but errors are starting to pop out. :(

    It is last task before putting images in, help, please.. something is not working well with that :(

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. oh, i just solved it by myself, you just need to write that like this:

    For Each btn As Button In GroupBox1.Controls
    btn.Enabled = True
    Next

    :)

    ReplyDelete
  18. Another question.

    How can i get that after i lose this game (when i have made all the mistakes) a message box pops out with a text - You lose, the correct word was ....! My question was, how can I get that exact correct word to pop out???

    Beginning may be:
    MessageBox.Show("You lose, the correct answer was " & ....)

    ReplyDelete
  19. It's been a while but I'm pretty sure that the word is in the string words(n), so it would be MessageBox.Show("You lose, the correct answer was " & words(n))

    ReplyDelete
  20. i tried it but it tells me that i have to declare words(n), also i tried to declare it as in new game button sub but it just shows me another random word :(

    help :(

    ReplyDelete
  21. I'm sorry it's been such a long time since I made this tut.

    Try declaring words(n) as a global variable after it's declared and then use that global variable to to display to word.

    ReplyDelete
  22. After you've declared words(n)

    Public nameofvariable as String = words(n)

    Then MessageBox.Show("You lose, the correct answer was " & nameofvariable)

    ReplyDelete
  23. Hello, thank´s for this tut. very well done.
    in the 1st comment you are talking with action-bob about having underscores "_" instead empty spaces... but i don't understand totally all those steps "i'm very newbie with vb"...can you explain it again?
    thank you... sorry my bad english...

    ReplyDelete
  24. i was wondering if you could give me the whole working code so i can compare it to mine, mine doesn't seem to work at all

    ReplyDelete