Python - 需要显示Hangman游戏的图形

时间:2016-05-29 19:40:59

标签: python python-3.5

我正在编写一个Python教程来编写一个Hangman游戏。我在我的计算机上安装了Python 3.5,但教程使用的是Python 2.7除了显示图像外,游戏还可以工作。这是一个语法问题,还是有不同的方法从我学习的方法来解决这个问题?这是我的代码......

#!/usr/bin/env python 2

from random import *

player_score = 0
computer_score = 0

# Define the graphics that will be used in the game
def hangedman(hangman):
    graphic = [
        """
            +-------+
            |
            |
            | 
            |
            |
         ==============
        """
            ,
        """
            +-------+
            |       |
            |       0
            | 
            |
            |
         ==============
        """
            ,
        """
            +-------+
            |       |
            |       0
            |       |
            |
            |
         ==============
        """
            ,
        """
            +-------+
            |       |
            |       0
            |      -|
            |
            |
         ==============
        """
            ,
        """
            +-------+
            |       |
            |       0
            |      -|-
            |
            |
         ==============
        """
            ,
        """
            +-------+
            |       |
            |       0
            |      -|-
            |      /
            |
         ==============
        """
            ,
        """
            +-------+
            |       |
            |       0
            |      -|-
            |      / \
            |
         ==============
        """]

def start():
    print ("Let's play a game of Linux Hangman")
    while game():
        # pass will exit the loop if the player is done
        pass
    scores()

def game():
    # Create a set of words to play the game with
    dictionary = ["cat", "dog", "bird", "nest", "sun", "tree"]
    # Use the choice function from random mod to select a word
    word = choice(dictionary)
    word_length = len(word)
    # Create a clue with the number of underscores
    # equal to the word's length
    clue = word_length * ["_"]
    tries = 6
    letters_tried = ""
    guesses = 0
    letters_right = 0
    letters_wrong = 0
    global computer_score, player_score

    # Set up a loop that continues until player wins or loses
    while (letters_wrong != tries) and ("".join(clue) != word):
        letter = guess_letter()
        # Make sure entry is numeric and only 1 character long
        if len(letter) == 1 and letter.isalpha():
            if letters_tried.find(letter) != -1:
                print ("You've already picked", letter)
            else:
                letters_tried = letters_tried + letter
                # Search the word for the entered letter
                first_index = word.find(letter)
                # If letter is correct, let user know
                if first_index == -1:
                    letters_wrong += 1
                    print ("Sorry,",letter,"isn't what we're looking for.")
                else:
                    print ("Congratulations, ",letter," is correct.")
                    # Loop through the word and change the specific letter
                    # that is correct
                    for i in range(word_length):
                        if letter == word[i]:
                            clue[i] = letter
        else:
            print ("Choose another.")

        # Display the graphic
        hangedman(letters_wrong)
        # Print what the clue currently looks like
        print (" ".join(clue))
        print ("Guesses: ", letters_tried)

        # Check if the game is over
        if letters_wrong == tries:
            print ("Game Over")
            print ("The word was", word)
            computer_score += 1
            break
        if "".join(clue) == word:
            print ("You win!")
            print ("The word was", word)
            player_score += 1
            break
    return play_again()

# Get user input, sanitize, display it, and return it to be used
def guess_letter():
    letter = input("Take a guess at our mystery word: ")
    letter.strip()
    letter.lower()
    print (letter)
    return letter

# Ask to play again
def play_again():
    answer = input("Would you like to play again? y/n: ")
    if answer in ("y", "Y", "yes", "Yes", "Of course!"):
        return answer
    else:
        print ("Thank you very much for playing!")

def scores():
    global player_score, computer_score
    print ("HIGH SCORES")
    print ("Player: ", player_score)
    print ("Computer: ", computer_score)

# Used to execute in command line or import
# into another Python script. This will prevent
# the code from being executed when being imported.
if __name__ == '__main__':
    start()

1 个答案:

答案 0 :(得分:2)

您的hangedman方法似乎实际上并未打印任何内容或使用硬编码的图形列表。尝试在graphic方法末尾的hangedman列表下添加此行代码:

print(graphic[hangman])