Hangman Part 1

This lesson will teach you how to set up and display the hangman game.

Setting Up



    During this lesson you will use what you already know and learn new concepts to make a hangman game like this:



    To get started, download the Hangman starter file.

    Open Python IDLE and click File > Open. Navigate to where you saved the file and double click on it to open it.

    This code imports the random module which the program will use to choose a random word from a list.

    The code also has a list of strings that represent each state of the hangman.

    These are special types of strings called multi-line strings which use three single quotes and allow the string to go across several lines instead of just one.

Split List Function



    We need a list of words that the program can choose from. Instead of writing a list, we can just write a string of words and use the split() function at the end of the list.

    The split() function splits the string into a list with each word in the string as a single list item.

    The split occurs wherever there's a space in the string. The spaces aren't included in the list.

    words = 'ant badger bat bear deer dog'.split()
    
    

    Above is an example of using split on a string of 10 words. However you can write your own set of words, just make sure you write at least 5 to choose from and that you seperate each word with a space.

Random Word



    Now that we have a list of words, we want to create a function that chooses a random one.

    We can do this using a the choice() function from the random module.

    words = 'ant badger bat bear deer dog'.split()
    
    def get_random_word(word_list):
      return random.choice(word_list)
    
    

    The function has one parameter: a list of words.

    random.choice() then returns a random item from the list.

Displaying the Board



    We need to display the hangman board to the player.

    The screen should also show how many letters the player has guessed correctly or incorrectly.

    Add a new function called display_board() which has four parameters:

    hangman_pics - A list of multi-line strings that will display the state of the hangman on the board.

    missed_letters - A string of the letters the player has guessed that are not in the secret word.

    correct_letters - A string of the letters the player has guessed that are in the secret word.

    secret_word - A string of the secret word that the player is trying to guess.

    def get_random_word(word_list):
      return random.choice(word_list)
    
    def display_board(hangman_pics, missed_letters, correct_letters, secret_word):
      print(hangman_pics[len(missed_letters)])
      print()
    
    

    The first print() function will display the board. The hangman_pics[0] through hangman_pics[6] are the images that the board will show.

    Which index of hangman_pics is shown depends on the number of misses. So 0 misses displays hangman_pics[0], 2 misses displays hangman_pics[2], and so on until the game reaches hangman_pics[6] and the game is over.

    We calculate the misses by using len(missed_letters) to return the number of missed letters. The len() function will return the length of the list it is given. This is useful for finding how long a given list is when it changes often.

Creating Variables



    Now that we've made our display_board() function, we need to create the main variables that will be altered throughout the game. This is called Initialization.

    We can also print the title of the game here, which is done on line 13 of the code below.

    The variables we need are: missed_letters, correct_letters, secret_word and game_is_done.

    Notice that these are the same variables that our display_board() function has as parameters.

    def display_board(hangman_pics, missed_letters, correct_letters, secret_word):
      print(hangman_pics[len(missed_letters)])
      print()
    
    print('HANGMAN')
    
    missed_letters = ''
    correct_letters = ''
    secret_word = get_random_word(words)
    game_is_done = False
    
    

    The variables missed_letters and correct_letters are initially set to two empty single quotes. This represents an empty string, meaning that the string is blank.

    These strings will be updated as the player makes guesses.

    Next we set the secret_word to a random word from our words list using get_random_word().

    We then set the game_is_done boolean variable to False. We will use this variable to check if the game is finished. If it is true, we can close the program.

While Loops



    While loops are like if statements and will branch code based on a condition.

    If the condition is true, then the code will repeat until it is false.

    Example:

    num = 1
    
    while num < 6:
      print(num)
      num += 1
    
    # Output:
    # 1
    # 2
    # 3
    # 4
    # 5
                    

    Here we use the assignment operator += to increment the value of num by 1 each time the loop runs.

    We will be using a while loop to keep our game running. This loop is commonly called the main game loop.

    while True:
     #run the game
                    
    When we say while True we are telling the loop to continue forever because the condition is always true.

    This is called an infinite loop because it runs forever. However we never want to have infinite loops as it can break our game.

    To stop it from being infinite we can add a break statement, which will break out of the loop regardless of the condition.

    while True:
      #run the game
      break
                  

Main Game Loop



    We need to create a loop so that the game continues to run until we tell it to stop.

    In this loop we can call all the functions that control the game. First we will display the board.

    secret_word = get_random_word(words)
    game_is_done = False
    
    while True:
      display_board(HANGMANPICS, missed_letters, correct_letters, secret_word)
    
    

    In the display_board function we can put in the variables we created earlier.

    Later we will tell the loop to end when game_is_done is True, but for now we will just add a break.

    while True:
      display_board(HANGMANPICS, missed_letters, correct_letters, secret_word)
      break
    
    

    Now if we try and run the game, the board should display in the shell window.