I got the idea from this course. I already know the basics of Python from studying for my university exams. However, I go through different courses I’ve purchased to deepen my understanding and gain more hands-on experience. My results may differ from those shown in the course, because I haven’t actually compared them.
Main.py: The Game's Core
The start of my Hangman game is straightforward: First, I retrieve a random English word from an API as a JSON object. Using `response.json()`, I then convert this data into a Python object.
Next, `functions.py` comes into play. Here, a loop iterates over the selected word and appends each individual character into the `word_list`. This is the foundation that allows the program to check if the player's input is contained within the word.
The game's flow is controlled by a `while` loop. Initially, the condition was that the loop would run as long as the number of lives was greater than 0. This became impractical because it was difficult to break the loop from various points in the code—for example, when the player had correctly guessed the word.
That's why I switched to using a boolean value called `is_game_over` as the loop condition. This makes it easy to set the game state to `True` from anywhere in the code to end the game.
During testing, I also added a few features to improve the player experience:
The actual gameplay is simple: the player enters a single character. A `for` loop then checks if that character is present in the word.
If the character is in the word, another loop is used to replace the corresponding placeholder (`_`) in the `display_word` list with the correct letter. If the character is not in the word, the player loses a life. A suitable ASCII art graphic is also displayed, which changes with each lost life until the player has no lives left.
All ASCII art and the API I used are transparently credited in the README file of my GitHub repository.
import random
import requests
from functions import char_in_word
from art import hangman_pics, hangman_intro, hangman_game_over
try:
response = requests.get('https://random-word-api.herokuapp.com/word')
data = response.json()
secret_word = data[0]
except requests.exceptions.RequestException as e:
print(f"Fehler bei der Anfrage: {e}")
word_list = []
word_char = char_in_word(word = secret_word, list_append = word_list)
life = 6
display_word = ['_'] * len(secret_word)
length_word = len(secret_word)
is_game_over = False
player_inputs = []
print(hangman_intro)
print(f"Welcome to Hangman!\nThe secret word is {length_word} letters long.")
print (hangman_pics[0])
while is_game_over == False:
print(display_word)
print(f"These are your guesses: {player_inputs}")
player_guess = input("Guess a character: ").lower().strip()
if player_guess in player_inputs:
print("You already guessed that character.")
continue
elif player_guess not in player_inputs:
player_inputs.append(player_guess)
if player_guess in word_list:
for i in range(len(secret_word)):
char = secret_word[i]
if player_guess == char:
display_word[i] = char
if "_" not in display_word:
result = "".join(display_word)
print(f"You did it, well done! The secret word was: {secret_word}")
is_game_over = True
else:
continue
else:
life -=1
print(f"Life: {life}")
if life == 5:
print (hangman_pics[1])
elif life == 4:
print (hangman_pics[2])
elif life == 3:
print (hangman_pics[3])
elif life == 2:
print(hangman_pics[4])
elif life == 1:
print(hangman_pics[5])
elif life == 0:
print(hangman_pics[6])
print(hangman_game_over)
print(f"The secret word was: {secret_word}")
is_game_over = True
def char_in_word(word, list_append):
for word in word:
for char in word:
list_append.append(char.lower())