How to Make a Hangman Game in Python
Introduction
Hangman is a classic guessing game that has been enjoyed by people of all ages. It’s a simple yet engaging game that requires strategic thinking and problem-solving skills. In this article, we’ll guide you through the process of creating a hangman game in Python.
Step 1: Plan the Game
Before we start coding, let’s plan the game. We’ll need to decide on the following:
- Game Name: Choose a name for your game, such as "Hangman" or "Word Guess".
- Game Rules: Decide on the rules of the game, such as the number of lives the player has and the maximum number of guesses allowed.
- Game Board: Create a game board to display the word to be guessed. We’ll use a 5×5 grid to represent the word.
Step 2: Create the Game Board
We’ll use a 5×5 grid to represent the word to be guessed. We’ll create a list of 5 strings, each representing a row in the grid.
# Create a 5x5 game board
game_board = [
["_", "_", "_", "_", "_"],
["_", "_", "_", "_", "_"],
["_", "_", "_", "_", "_"],
["_", "_", "_", "_", "_"],
["_", "_", "_", "_", "_"]
]
Step 3: Create the Hangman Figure
We’ll create a hangman figure using a list of strings. We’ll use a loop to create the figure, with each string representing a part of the figure.
# Create the hangman figure
hangman_figure = [
["-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-"]
]
Step 4: Create the Guessing Mechanism
We’ll create a function to check if the player’s guess is correct. We’ll use a loop to compare the player’s guess with the game board.
# Create the guessing mechanism
def check_guess(guess):
global game_board
global hangman_figure
# Check if the guess is in the game board
if guess in game_board:
# Find the index of the guess in the game board
index = game_board.index(guess)
# Check if the guess is correct
if guess == game_board[index][0]:
# Update the game board
game_board[index] = guess + "_"
else:
# Update the hangman figure
for i in range(5):
for j in range(5):
if game_board[i][j] == "_":
if i == index:
hangman_figure[i][j] = "-"
else:
hangman_figure[i][j] = "_"
# Check if the player has won
if "_" not in game_board:
print("Congratulations, you won!")
return True
# Update the hangman figure
for i in range(5):
for j in range(5):
if game_board[i][j] == "_":
hangman_figure[i][j] = "-"
return False
Step 5: Create the Main Game Loop
We’ll create a main game loop that continues until the player wins or runs out of lives.
# Create the main game loop
def play_game():
global game_board
global hangman_figure
# Initialize the game board and hangman figure
for i in range(5):
for j in range(5):
game_board[i][j] = "_"
hangman_figure[i][j] = "-"
# Initialize the number of lives
lives = 6
# Play the game
while True:
# Print the game board
print(" ".join(game_board))
# Ask the player for their guess
guess = input("Enter your guess: ")
# Check if the player has won
if check_guess(guess):
print("Congratulations, you won!")
break
# Update the number of lives
lives -= 1
# Print the number of lives
print(f"Lives remaining: {lives}")
# Check if the player has run out of lives
if lives == 0:
print("Game over! The word was:", game_board[0][0])
break
# Play the game
play_game()
Conclusion
In this article, we’ve created a simple hangman game in Python. We’ve planned the game, created the game board, created the hangman figure, created the guessing mechanism, and created the main game loop. We’ve also included a simple text-based interface to play the game.
Tips and Variations
- To make the game more challenging, you can increase the number of lives or add more complex rules.
- To make the game easier, you can decrease the number of lives or add more hints.
- You can also add more features, such as a timer or a scoring system.
Code
Here’s the complete code:
# Create a 5x5 game board
game_board = [
["_", "_", "_", "_", "_"],
["_", "_", "_", "_", "_"],
["_", "_", "_", "_", "_"],
["_", "_", "_", "_", "_"],
["_", "_", "_", "_", "_"]
]
# Create the hangman figure
hangman_figure = [
["-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-"]
]
# Create the guessing mechanism
def check_guess(guess):
global game_board
global hangman_figure
# Check if the guess is in the game board
if guess in game_board:
# Find the index of the guess in the game board
index = game_board.index(guess)
# Check if the guess is correct
if guess == game_board[index][0]:
# Update the game board
game_board[index] = guess + "_"
else:
# Update the hangman figure
for i in range(5):
for j in range(5):
if game_board[i][j] == "_":
if i == index:
hangman_figure[i][j] = "-"
else:
hangman_figure[i][j] = "_"
# Check if the player has won
if "_" not in game_board:
print("Congratulations, you won!")
return True
# Update the hangman figure
for i in range(5):
for j in range(5):
if game_board[i][j] == "_":
hangman_figure[i][j] = "-"
return False
# Create the main game loop
def play_game():
global game_board
global hangman_figure
# Initialize the game board and hangman figure
for i in range(5):
for j in range(5):
game_board[i][j] = "_"
hangman_figure[i][j] = "-"
# Initialize the number of lives
lives = 6
# Play the game
while True:
# Print the game board
print(" ".join(game_board))
# Ask the player for their guess
guess = input("Enter your guess: ")
# Check if the player has won
if check_guess(guess):
print("Congratulations, you won!")
break
# Update the number of lives
lives -= 1
# Print the number of lives
print(f"Lives remaining: {lives}")
# Check if the player has run out of lives
if lives == 0:
print("Game over! The word was:", game_board[0][0])
break
# Play the game
play_game()
