This is my first project I am attempting in Python, and my first time on Stack Exchange, so please be kind.
The game uses a text file with lots of words. I was wondering how to improve the game by having different difficultly levels. E.g. easy for 1-4 letters, medium for 5-8 letters, hard for 9+ letters in the word.
All the code in print_graphics()
was pre-made, so I have to figure out a way to incorporate it into the code I was writing.
I was also thinking it might look neater if it didn’t keep printing new iterations of the hangman after each body part is added, but I’m not sure how I could do that without massively changing the text.
I couldn’t figure out how to attach the input file so I’ve linked it instead.
import random
from IPython.display import clear_output
from termcolor import colored
def print_graphics(wrong_guesses):
# list of possible body parts
body_parts = (' O |', ' | |',' /| |', ' /| |', ' / |', ' / |')
# how many lines to print
lines = 4 if wrong_guesses != 0 else 5
# check number provided is usable
if 0 <= wrong_guesses <= 6:
print(' +-----+') # print top of frame
print(' | |')
# print the correct body parts for current state
if wrong_guesses > 0:
if wrong_guesses > 1:
print(body_parts(0))
lines -= 1
if wrong_guesses > 4:
print(body_parts(3))
lines -= 1
print(body_parts(wrong_guesses-1))
for i in range(lines):
print(' |') # print the lines
print('==========') # print the floor
with open("wordlist.txt") as file:
words = file.read().split()
word = random.choice(words)
guessed_word = ()
guessed_letters = ()
max_guesses = 5
length_word = len(word)
alphabet = "abcdefghijklmnopqrstuvwxyz"
letter_storage = ()
body_parts = (' O |', ' | |',' /| |', ' /| |', ' / |', ' / |')
def start():
print(" 33(1;34m"+"Welcome to hangman! You have 6 tries to guess the correct word, enter a letter to get started", " 33(0;30m")
start()
def intro():
for character in word:
guessed_word.append("_")
print("The word you are guessing has", length_word, "characters")
print_graphics(0)
intro()
def play(word):
wrong_guesses = 0
while wrong_guesses <6:
guess = input("Guess a letter or word: ")
if guess in guessed_letters:
print(" 33(1;31m"+"Whoops! You have already guessed this letter. Don't worry, you still have", 6-wrong_guesses, "guesses remaining" " 33(0;30m")
if guess not in alphabet:
print(" 33(1;31m"+"Character invalid. Please enter a letter", " 33(0;30m")
elif guess in alphabet and guess not in guessed_letters:
if guess in word:
print(" 33(1;3;32m"+"Well done", guess.upper(), "is correct!", " 33(0;30m")
guessed_letters.append(guess)
for i in range(0, length_word):
if word(i) == guess:
guessed_word(i) = guess
print(guessed_word)
if not "_" in guessed_word:
print(" 33(1;3;32m"+"Congratulations, you beat the Hangman!", " 33(0;30m")
break
elif guess not in word:
wrong_guesses += 1
print(print_graphics(wrong_guesses))
print(guessed_word)
print(" 33(1;31m"+guess.upper(), "is not correct. You have", 6-wrong_guesses, "guesses left", " 33(0;30m")
guessed_letters.append(guess)
if wrong_guesses == 6:
print(" 33(1;31m"+"Bad luck! You have been beaten by the Hangman. The word you were trying to guess was", word, " 33(0;30m")
play(word)
print(" 33(1;3;35m"+"Game over, thank you for playing :)")