Python can be used in many ways and one of the ways is to make games. Games in Python can either be simple like Rock-Paper-Scissor or it can be complex like snake game.
Today what we are building is a simple guess-the-word game called Hangman. Hangman is a game where you guess the word that has been asked and if your guessed word is wrong a part of the body of a man will be hanged.
Making the Hangman game
In the hangman game we need to import a single function called choice from random class for and create file for the artwork and the stages to be used.
NOTE: The artwork for the stages and the word list’s zip file can be downloaded here
Step 1: First import choice from random, and then import the artwork and the wordList
Step 2: Create the whole game
#In main.py
# importing random and other files
from random import choice
import artwork
import wordList
#Chosing the random word from the given list
chosen_word = choice(wordList.word_list)
word_length = len(chosen_word)
lives = 6
#Printing the logo
print(artwork.logo)
#Create blanks
display = []
for _ in range(word_length):
display += "_"
while True:
guess = input("Guess a letter: ").lower()
if guess in display:
print(f"You have already guessed the this {guess} letter ")
#Check guessed letter
for position in range(word_length):
letter = chosen_word[position]
if letter == guess:
display[position] = letter
#Check if user is wrong.
if guess not in chosen_word:
print(f'The letter you have guessed "{guess}" is not the right word ')
lives -= 1
if lives == 0:
print("You lose.")
break
#Join all the elements in the list and turn it into a String.
print(f"{' '.join(display)}")
#Check if user has got all letters.
if "_" not in display:
print("You win.")
break
#Print all the stages of hangman
print(artwork.stages[lives])