Creating a simple Hangman game using Python
Preface
This is just one of the billions of other ways to create a hangman game. And this might not be the very version of hangman you want. However, through this project, I was for the first time able to create an interactive code using python. I hope you people will feel the same way.
Before getting started
For sound effects, I've created a folder called sound and put some sound effects called good.wav for ringing and bad.wav for buzzing. You can freely download these .wav sound effects from freesound.org.
Also, to have your hangman game be able to create random words, you need to create a csv file. In a folder called resource, I've created a csv file called word_list.csv in a format like this:
Name,Hint
strawberry,fruit
grape,fruit
melon,fruit
orange,fruit
wagon,type of vehicle
van,type of vehicle
sedan,type of vehicle
You are free to add more words and hints, as long as it stays in that format.
Import modules
In this hangman, I'll be using four modules: time, csv, random, and winsound.
The time module will be used for fancy loading screens, csv for reading files with hints and words, random to create random questions, and winsound to have sound effects.
import time
import csv
import random
import winsound
Beginning
Let's ask the user for his/her name and make some loading screens and stuff like that.
# Greeting
name = input("What is your name? ")
print("Hi!, " + name, "Time to play hangman game!")
print()
# wait for 1 second (loading effect)
time.sleep(1)
print("Start loading...")
print()
# wait for 0.5 second
time.sleep(0.5)
Creating a word (question) randomly
# creating a list called words
words = []
with open('./resource/word_list.csv', 'r') as f:
reader = csv.reader(f)
next(reader)
for c in reader:
words.append(c)
# the answer
random.shuffle(words)
q = random.choice(words)
# removes unnecessary characters & creates a word
word = q[0].strip()
Guesses and turns
guesses = ''"
turns = 10
The crucial function
while turns > 0:
# fails
failed = 0
# repeating the answer word
for char in word:
# if the char in word was in guesses
if char in guesses:
# prints the predicted/correct char
print (char, end=' ')
else:
# if else print _ instead
print ("_", end=' ')
# +fail
failed += 1
# if failed==0 (all chars are predicted correctly) => success
if failed == 0:
print()
print()
winsound.PlaySound('./sound/good.wav', winsound.SND_FILENAME)
print("Congratulations! The Guesses is correct.")
# break
break
print()
# predicting the char
print()
print('Hint : {}'.format(q[1].strip()))
guess = input("guess a character:")
# adding the char to a list
guesses += guess
# if the char was not in the word
if guess not in word:
# reduces turns
turns -= 1
# messege
print("Oops! Wrong")
# prints remaining turns
print("You have", + turns, 'more guesses!')
# if turns are all used up
if turns == 0:
# failure messege
winsound.PlaySound('./sound/bad.wav', winsound.SND_FILENAME)
print("You hangman game failed. Bye!")
Hope you enjoy it!



