Hangman Game Python Project
THE HANGMAN GAME is a popular word guessing game that may be played by two or more people. A word or a list of words is specified or defined by one player, and the other player(s) must guess correctly after a particular number of tries. It's fast, simple, and informative, and it usually just needs a piece of paper and the ability to accurately spell. It's also possible to play it on a computer (as the one which will be built in this article). A life or trial in the game is lost for each incorrectly guessed word, and a "hanging-man" starts to emerge, piece by piece. Before the hangman dies, you must solve the riddle and guess the proper word(s).
This traditional game has grown in popularity to the point that, like other games, there are a few winning tips, techniques, and strategies.
RULES OF THE GAME:
In order to integrate this game into a code-based solution, we must first comprehend the game's rules—how does it operate? Only then will we be able to effectively convert to code.
- A word or a collection of words is chosen by an individual.
- A player makes an educated estimate as to which letter may begin or end the word or list of words.
- If the letter appears in the word/list of words, the player receives a point and is given another chance to predict a letter.
- If the letter isn't in the word, the player loses a try(tries)/point and a piece of the hangman emerges.
- The hangman emerges in bits and pieces for each incorrectly guessed letter until the whole picture is created.
- Similarly, the letters are put on the screen for each correctly predicted letter until the word is finished and the player wins.
Hangman Game Python Project
So the plan for this series is to start with a very basic version and then update and increase the functionality in subsequent installments. In the same way as the last series. We won't be utilizing the visual display in this case; instead, we'll use a basic logic control to regulate how the game operates.
I wouldn't utilize user-defined modules in this project, but I invite you to try it out and see if you can come up with a different approach to do it. Take a step back and review all of the principles you've learned so far, then use them to create something unique. Because this project may be tough and intricate, we'll start with a simplified version first.
Source Code
import random
import time
# Initial Steps to invite in the game:
print("\nWelcome to Hangman game by IT SOURCECODE\n")
name = input("Enter your name: ")
print("Hello " + name + "! Best of Luck!")
time.sleep(2)
print("The game is about to start!\n Let's play Hangman!")
time.sleep(3)
# The parameters we require to execute the game:
def main():
global count
global display
global word
global already_guessed
global length
global play_game
words_to_guess = ["january","border","image","film","promise","kids","lungs","doll","rhyme","damage"
,"plants"]
word = random.choice(words_to_guess)
length = len(word)
count = 0
display = '_' * length
already_guessed = []
play_game = ""
# A loop to re-execute the game when the first round ends:
def play_loop():
global play_game
play_game = input("Do You want to play again? y = yes, n = no \n")
while play_game not in ["y", "n","Y","N"]:
play_game = input("Do You want to play again? y = yes, n = no \n")
if play_game == "y":
main()
elif play_game == "n":
print("Thanks For Playing! We expect you back again!")
exit()
# Initializing all the conditions required for the game:
def hangman():
global count
global display
global word
global already_guessed
global play_game
limit = 5
guess = input("This is the Hangman Word: " + display + " Enter your guess: \n")
guess = guess.strip()
if len(guess.strip()) == 0 or len(guess.strip()) >= 2 or guess <= "9":
print("Invalid Input, Try a letter\n")
hangman()
elif guess in word:
already_guessed.extend([guess])
index = word.find(guess)
word = word[:index] + "_" + word[index + 1:]
display = display[:index] + guess + display[index + 1:]
print(display + "\n")
elif guess in already_guessed:
print("Try another letter.\n")
else:
count += 1
if count == 1:
time.sleep(1)
print(" _____ \n"
" | \n"
" | \n"
" | \n"
" | \n"
" | \n"
" | \n"
"__|__\n")
print("Wrong guess. " + str(limit - count) + " guesses remaining\n")
elif count == 2:
time.sleep(1)
print(" _____ \n"
" | | \n"
" | |\n"
" | \n"
" | \n"
" | \n"
" | \n"
"__|__\n")
print("Wrong guess. " + str(limit - count) + " guesses remaining\n")
elif count == 3:
time.sleep(1)
print(" _____ \n"
" | | \n"
" | |\n"
" | | \n"
" | \n"
" | \n"
" | \n"
"__|__\n")
print("Wrong guess. " + str(limit - count) + " guesses remaining\n")
elif count == 4:
time.sleep(1)
print(" _____ \n"
" | | \n"
" | |\n"
" | | \n"
" | O \n"
" | \n"
" | \n"
"__|__\n")
print("Wrong guess. " + str(limit - count) + " last guess remaining\n")
elif count == 5:
time.sleep(1)
print(" _____ \n"
" | | \n"
" | |\n"
" | | \n"
" | O \n"
" | /|\ \n"
" | / \ \n"
"__|__\n")
print("Wrong guess. You are hanged!!!\n")
print("The word was:",already_guessed,word)
play_loop()
if word == '_' * length:
print("Congrats! You have guessed the word correctly!")
play_loop()
elif count != limit:
hangman()
main()
hangman()
Output
Summary
The Game of Hangman A basic understanding of Python is required for this project, which involves creating functions and handling for/while loops. The functions we utilize here have arguments that are declared in a global scope and may be used in other methods to enhance the quality of the game. It may also be utilized to give distinct stages when the for and while loops are needed to execute on certain situations.