Hang Man
At some point of your childhood, you must have played this silly game. For those who are unfamiliar, this game involves guessing a random word letter-by-letter. For every wrong guess the player makes, the picture proceeds to make the set to hang the man. For example, on the first wrong guess, the poles appear, on the second, the rope appears, or somewhat to this effect. Upon making enough mistakes, the full picture is complete and it is assumed the Man is HANGED.
I present to you the Hangman Game on Python. I have used Tkinter for the User Interface (I mean, come on, who likes the command prompt? At least not me...). You get to add your desired words in the python list words in the code and you can play this game to your heart's wish, knowing you have done something new.
Some of you may be having projects, So just look at the steps below and skip the code to the end, where you will get the download link to the entire code!
The First Step
Before you jump on the code, you have to check if you have the necessary modules installed. This project uses Tkinter (for the user interface) , Pillow (for uploading the images) and Random (for choosing a random word to guess)
So go ahead and install the modules mentioned above.
The Images
Also, we need to make the images for the Hang Man. For simplicity, you may go to Microsoft Paint, draw each frame seperately and save them in a folder:
Note: You should consider resizing the canvas to a much smaller area than the default one. Usually, the image will appear on the Tkinter window the same size as that you see in MS paint.
The Code:
Importing the necessary Modules:
Defining a function start() which will house the game's input and output:
So what do we have here? There are a lot of variables termed Global because they are going to be used across another defined function guess(). Then you import the images using ImageTk.PhotoImage function of pillow, with the directory address where you have your hangman images.
Note: There is r before the address of each image, because you need to specify that the address is raw since / is a special character in Python. Alternatively, you could also convert single slashes to double slashes with no r.
So we choose a random word from our list of words and get its length. Now we have to conceal the word by using _ _ _ _ _ and make each letter appear like _ _ a_ as the user inputs a correct letter. we also need to update it again. Therefore the concealed text will be stored as a 'list' because it becomes easy with its index slicing. The other stuff is just for the Tkinter Window.
The guess() function is the algorithm or the recipe, you may say, on how to show the concealed word, when to show its letters and when to change the image. I also have a button new which will take us to the function new_game() which destroys the Window to start a new game. I am not going much over the algorithm as that will take ages. But do put print statements here and there to see what the code is doing:
The Working:
This is the opening window, with the title, a short info, the image and the concealed word:
This is how it looks when it shows the correctly guessed letter:
Happy Coding!
Comments
Post a Comment