- Setting up a Python project.
- Working with lists, strings, and basic game logic.
- Implementing loops, conditionals, and user input handling.
- Managing game states using a graphical representation of the hangman.
- Basic knowledge of Python (functions, loops, conditionals).
- Python installed on your computer.
- Basic understanding of Git.
First, you'll download a template containing the hangman states. This template will provide the initial setup needed to get started quickly.
-
Open your terminal or command prompt.
-
Navigate to the directory where you want to set up your project.
-
Clone the template repository by running the following command:
git clone https://github.com/your-username/hangman-template.git
-
Change into the project directory:
cd hangman-template
After cloning the template, inspect the initial setup provided.
- Open the
hangman.py
file in your preferred code editor. - You should see the hangman stages defined in the file.
HANGMAN = (
"""
-----
| |
|
|
|
|
|
|
|
--------
""",
# Additional stages will be added here...
)
- The
HANGMAN
variable contains different stages of the hangman drawing. - This template serves as the starting point to develop the full Hangman game.
Let's start by adding a welcome message to the player when they start the game.
def main():
welcome = ['Welcome to Hangman! A word will be chosen at random and',
'you must try to guess the word correctly letter by letter',
'before you run out of attempts. Good luck!']
for line in welcome:
print(line)
- A list
welcome
contains strings for the welcome message. - The loop prints each line of the welcome message to the player.
Allow the player to replay the game without restarting the program.
play_again = True
while play_again:
# Game logic will go here
- A
play_again
variable determines whether the game should start a new round. - The
while
loop continues as long as the player wants to play.
Set up a list of possible words and select one randomly for the game.
words = ["hangman", "chairs", "backpack", "bodywash", "clothing",
"computer", "python", "program", "glasses", "sweatshirt",
"sweatpants", "mattress", "friends", "clocks", "biology",
"algebra", "suitcase", "knives", "ninjas", "shampoo"]
chosen_word = random.choice(words).lower()
player_guess = None
guessed_letters = []
word_guessed = ["-"] * len(chosen_word)
- A list
words
contains potential words for the game. - A random word is chosen using
random.choice()
. - Initialize variables for player's input, guessed letters, and a list of dashes representing unguessed letters.
Use the hangman stages defined in the template to show the player's remaining attempts visually.
print(HANGMAN[0])
attempts = len(HANGMAN) - 1
- Display the initial hangman stage.
- Set the number of attempts based on the number of stages available in
HANGMAN
.
Implement the main game loop where the player guesses letters.
while attempts != 0 and "-" in word_guessed:
print(f"\nYou have {attempts} attempts remaining")
print("".join(word_guessed))
player_guess = input("\nPlease select a letter between A-Z\n> ").lower()
if not player_guess.isalpha() or len(player_guess) > 1 or player_guess in guessed_letters:
print("Invalid guess. Please try again.")
continue
guessed_letters.append(player_guess)
if player_guess in chosen_word:
for i in range(len(chosen_word)):
if player_guess == chosen_word[i]:
word_guessed[i] = player_guess
else:
attempts -= 1
print(HANGMAN[len(HANGMAN) - attempts - 1])
- The loop continues while attempts are left and the word is not fully guessed.
- Player input is validated and compared against the chosen word.
- Correct guesses reveal letters, while incorrect guesses reduce attempts.
After the loop ends, determine whether the player has won or lost.
if "-" not in word_guessed:
print(f"\nCongratulations! {chosen_word} was the word.")
else:
print(f"\nUnlucky! The word was {chosen_word}.")
- Check if there are any dashes left in
word_guessed
to determine if the player guessed the word. - Display a win or loss message accordingly.
Ask the player if they want to play again.
print("\nWould you like to play again?")
response = input("> ").lower()
if response not in ("yes", "y"):
play_again = False
- Prompt the user for their input on whether to play another round.
- Update the
play_again
variable based on their response.
You've successfully built a Hangman game in Python! Feel free to modify the code, add more words, or enhance the game with additional features. Keep practicing, and enjoy coding!