Hangman直译为“上吊的人”,是一个猜单词的双人游戏。由一个玩家想出一个单词或短语,另一个玩家猜该单词或短语中的每一个字母。第一个人抽走单词或短语,只留下相应数量的空白与下划线。
word_list = ["ardvark", "baboon", "camel"]
stages = [ r""" +---+ | | O | /| | / | | ========= """, r""" +---+ | | O | /| | / | | ========= """, r""" +---+ | | O | /| | | | ========= """, r""" +---+ | | O | /| | | | =========""", r""" +---+ | | O | | | | | ========= """, r""" +---+ | | O | | | | ========= """, r""" +---+ | | | | | | ========= """, ] logo = r""" _ | | | |__ __ _ _ __ __ _ _ __ ___ __ _ _ __ | '_ / | '_ / _ | ' _ / _ | '_ | | | | (| | | | | (| | | | | | | (| | | | | || ||__,|| ||, || || |_|,|| || / | |/ """
import random from hangman_art import logo, stages from hangman_words import word_list chosen_word = random.choice(word_list) word_length = len(chosen_word) end_of_game = False lives = 6 print(logo) Create blanks display = [] for _ in range(word_length): display += "_" while not end_of_game: guess = input("Guess a letter: ").lower() if guess in display: print(f"You've already guessed {guess}") continue # Check guessed letter for position in range(word_length): letter = chosen_word[position] if letter == guess: display[position] = letter # Check if user is wrong. if guess not in chosen_word: print(f"You guessed {guess}, that's not in the word. You lose a life.") lives -= 1 # Join all the elements in the list and turn it into a string. print(f"{' '.join(display)}") print(stages[lives]) if "_" not in display: end_of_game = True print("You win!") if lives == 0: end_of_game = True print("You lose.")</code></pre>
本文作者:a
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!