Skip to main content

Hang Man Game using Python with Tkinter

 


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:


                              

 

I will leave the rest of the features up to you to see. Download the file below to get your hands over the entire code. Remember to follow the steps and make the pictures on your system, or else Python will not be able to find the pictures and show you error:



Happy Coding!


Comments

Popular posts from this blog

Simple Omegle Bot Using Selenium With Python

Omegle is an online text-based and video-based chatting platform, which allows users from around the world to talk to complete strangers anonymously, for free! The text-based Omegle has a simple concept: Complete a Captcha  verification, connect automatically to a stranger, and after chatting get on to the next stranger. Apart from having a chit-chat with a stranger from the far side of the globe, Omegle poses as the perfect platform for other uses as well. At Omegle, you  can advertise your content, website, products and more for free. With access to about 40,000 strangers using Omegle at any given time, you can benefit if your ideas/advertisement is seen by potential customers. But of course, we can't advertise to each guy we meet again and again. I mean, come on, that's a lot of hard work, even if your message consists of a few words. But worry not, since that's where our Omegle Bot  comes in play. This bot works on a pre-determined set of messages that are to be conveye

Predicting Stock Prices using Machine Learning (XGBoost)

  Today, I'll show you how to create a machine learning program to predict stock prices. Machine learning is used in a variety of fields, and we can utilize its ability to learn and predict from data to predict useful variables, with minimal error. Therefore in theory, we can apply the same on stocks to predict the next closing price, so that we can make a killing gain. However, stock markets are highly unstable. Their price movement often depends on decisions taken by the company, favor and reaction of investors, social impact, human emotions, and price movements of some other related stocks. These types of data cannot be made available to a program. The prediction can be close only if the market remains relatively stable. You don't actually need to learn machine learning using python to understand how this program works, if not minimal. I'll describe each machine learning process. And don't forget to download the source code of this program, link provided at the end.

English Dictionary With Python And Tkinter!

English Dictionary on Python Have you ever tried to read from the small Oxford Dictionaries? Those small yet bulky books have thousands of words cramped on a page the length of your finger! And on top of that, not to forget the hassle of flipping across the pages, searching for your word, God knows where is hiding on which page! Nowadays though, hardly does anyone ever use the classic dictionaries. With the access of technology on our fingertips, one tap on Google, and you would be on with your way.  But how about making a program of your own which can do the same for you? Sounds interesting? Such a program can enable you to run it and keep it opened, while you are reading a book, so that you can search the definition of a new word you encountered. What if you are writing a book, perhaps a report? You know what you are supposed to write, but you choose to get a word for it, so that you sound professional. So you just search for a short definition and the program find the word with matc