编辑
2026-04-01
undefined
00

目录

Hangman游戏
hangman_words.py
hangman_art.py
main.py

Hangman游戏

Hangman直译为“上吊的人”,是一个猜单词的双人游戏。由一个玩家想出一个单词或短语,另一个玩家猜该单词或短语中的每一个字母。第一个人抽走单词或短语,只留下相应数量的空白与下划线。

hangman_words.py

word_list = ["ardvark", "baboon", "camel"]

hangman_art.py

stages = [ r""" +---+ | | O | /| | / | | ========= """, r""" +---+ | | O | /| | / | | ========= """, r""" +---+ | | O | /| | | | ========= """, r""" +---+ | | O | /| | | | =========""", r""" +---+ | | O | | | | | ========= """, r""" +---+ | | O | | | | ========= """, r""" +---+ | | | | | | ========= """, ] logo = r""" _ | | | |__ __ _ _ __ __ _ _ __ ___ __ _ _ __ | '_ / | '_ / _ | ' _ / _ | '_ | | | | (| | | | | (| | | | | | | (| | | | | || ||__,|| ||, || || |_|,|| || / | |/ """

main.py

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&quot;You've already guessed {guess}&quot;) 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&quot;You guessed {guess}, that's not in the word. You lose a life.&quot;) lives -= 1 # Join all the elements in the list and turn it into a string. print(f&quot;{' '.join(display)}&quot;) print(stages[lives]) if &quot;_&quot; not in display: end_of_game = True print(&quot;You win!&quot;) if lives == 0: end_of_game = True print(&quot;You lose.&quot;)</code></pre>

本文作者:a

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!