Skip to content

Vickyabiodun/Hangman-Game

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Hangman Game in Python

In this tutorial, we will create a simple hangman game in Python. The game will randomly select a word from a pre-defined list of words and the player will have to guess the word by guessing individual letters. The game will keep track of the number of lives remaining and will display ASCII art corresponding to the number of lives remaining. The game will end when the word has been fully guessed or when the player runs out of lives.

Prerequisites

  • Basic knowledge of Python programming
  • Familiarity with loops and conditional statements

Setting up the project

  1. Create a new Python file and name it hangman.py.
  2. At the top of the file, import the following modules:
    import random
    import hangman_art
    import hangman_words
  3. Next, we will print the logo by printing the logo attribute of the hangman_art module:
    print(hangman_art.logo)

Selecting a word

  1. To select a word, we will use the random.choice() function to choose a random word from the word_list attribute of the hangman_words module:
    chosen_word = random.choice(hangman_words.word_list)
  2. We will also need to know the length of the chosen word, so we will store it in a variable called word_length:
    word_length = len(chosen_word)

Setting up the game loop

  1. Next, we will create a variable called end_of_game and set it to False. This variable will be used to determine when the game should end. We will also create a variable called lives and set it to 6, which will represent the number of lives the player has remaining.
    end_of_game = False
    lives = 6
  2. We will create a list called display to store the current state of the word. Initially, the word will be represented by a series of underscores, one for each letter in the word. We will use a for loop to add underscores to the list:
    display = []
    for _ in range(word_length):
        display += "_"
  3. Now we will set up the main game loop using a while loop and the end_of_game variable.
    1. Inside the game loop, we will prompt the user to guess a letter and store it in a variable called guess. We will also convert the guess to lowercase to make it case-insensitive:
      while not end_of_game:
          guess = input("Guess a letter: ").lower()

    Checking the guess

    1. Inside the game loop, we will check if the letter has already been guessed by the player. If it has, we will print a message to inform the player:
      if guess in display:
          print(f'letter {guess} has been previously entered')
    2. Next, we will loop through the chosen word and check if the guessed letter is present in the word. If it is, we will replace the corresponding underscore in the display list with the letter:
      for position in range(word_length):
          letter = chosen_word[position]
          if letter == guess:
              display[position] = letter
    3. If the guessed letter is not present in the word, we will decrease the number of lives remaining and print a message to inform the player:
      if guess not in chosen_word:
          lives -= 1
          print(f" you entered {guess} and it's not in the word.")
          print(f"You have {lives} lives left")

    Ending the game

    1. If the player runs out of lives, we will set the end_of_game variable to True and print a message to inform the player that they have lost:
      if lives == 0:
          end_of_game = True
          print("You lose, you have no lives left ")
          print(f" The choosen word is {chosen_word}")
    2. We will also check if the player has successfully guessed all the letters in the word. If they have, we will set the end_of_game variable to True and print a message to inform the player that they have won:
      if "_" not in display:
          end_of_game = True
          print("You win.")
          print(f" The choosen word is {chosen_word}")

    Displaying the current state of the game

    1. Inside the game loop, we will print the current state of the word by joining all the elements in the display list and turning it into a string:
      print(f"{' '.join(display)}")
    2. We will also print the corresponding ASCII art for the number of lives remaining by accessing the appropriate element
      1. Inside the game loop, we will print the corresponding ASCII art for the number of lives remaining by accessing the appropriate element in the stages attribute of the hangman_art module:
        print(hangman_art.stages[lives])

      Complete code

      Here is the complete code for the hangman game:

      #import the random module, hangman ascii art and words
      import random
      import hangman_art
      import hangman_words
      

      #print the logo print(hangman_art.logo)

      #make the choosen words random chosen_word = random.choice(hangman_words.word_list)

      #find the length of the choosen word word_length = len(chosen_word)

      #create a variable to use in determining the end of the game end_of_game = False

      #create a variable for the number of lives lives = 6

      #Create a list display = []

      #add a dash to represent each word in the choosen word and add it to the list for _ in range(word_length): display += "_"

      #create a while loop using the end_of_game variable earlier created so that the questions will repeatedly come up while not end_of_game: guess = input("Guess a letter: ").lower() #introduce a conditional statement so that if the word has been previously entered, it will inform them. if guess in display: print(f'letter {guess} has been previously entered')

      #loop through the choosen word to replace the blanks with the correct letter
      for position in range(word_length):
          letter = chosen_word[position]
          if letter == guess:
              display[position] = letter
              
      #this checks if user is wrong and deducts the lives with a feedback message.
      if guess not in chosen_word:
          lives -= 1
          print(f" you entered {guess} and it's not in the word.")
          print(f"You have {lives} lives left")
          if lives == 0:
              end_of_game = True
              print("You lose, you have no lives left ")
              print(f" The choosen word is {chosen_word}")
      
      #Join all the elements in the list and turn it into a String.
      print(f"{' '.join(display)}")
      
      #Check if user has got all letters.
      if "_" not in display:
          end_of_game = True
          print("You win.")
          print(f" The choosen word is {chosen_word}")
      
      #print the corresponding art to the number of lives left
      print(hangman_art.stages[lives])</code></pre>
      

      Conclusion

      In this tutorial, we learned how to create a simple hangman game in Python. We used loops, conditional statements, and various Python features to build the game and make it interactive

About

Hangman Game

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages