
hangman 是一款经典的猜词游戏,非常有趣,对于初学者程序员来说是一个很棒的项目。
在本文中,我们将学习如何用 python 构建一个简单版本的 hangman 游戏。
最后,您将了解如何使用 python 的基本控制结构、函数和列表来创建这个游戏。
hangman 的目标是通过一次建议一个字母来猜测一个秘密单词。
立即学习“Python免费学习笔记(深入)”;
在游戏结束之前,玩家只能做出有限数量的错误猜测。
对于每个错误的猜测,都会绘制“刽子手”图形的一部分,如果在猜出单词之前绘制出完整的图形,则玩家失败。
让我们分解这个并逐步构建游戏。
让我们计划一下游戏及其主要功能:
游戏将包含以下组件:
我们将使用随机模块从列表中随机选择一个单词。
import random
接下来,定义游戏将从中随机选择的单词列表。
您可以添加更多单词,让游戏变得更有趣。
Android文档-开发者指南-第一部分:入门-中英文对照版 Android提供了丰富的应用程序框架,它允许您在Java语言环境中构建移动设备的创新应用程序和游戏。在左侧导航中列出的文档提供了有关如何使用Android的各种API来构建应用程序的详细信息。第一部分:Introduction(入门) 0、Introduction to Android(引进到Android) 1、Application Fundamentals(应用程序基础) 2、Device Compatibility(设备兼容性) 3、
11
word_list = ['python', 'java', 'hangman', 'programming', 'computer']
我们需要一个函数来从单词列表中随机选择一个单词。
def get_random_word(word_list):
return random.choice(word_list)
当玩家猜测字母时,我们需要显示正确猜到的字母和未猜到的字母的占位符 (_)。
def display_word(word, guessed_letters):
display = ''
for letter in word:
if letter in guessed_letters:
display += letter + ' '
else:
display += '_ '
return display.strip()
此函数检查是否已猜出单词的所有字母。
def is_word_guessed(word, guessed_letters):
for letter in word:
if letter not in guessed_letters:
return false
return true
要在基于文本的游戏中显示刽子手形象,您可以使用 ascii 艺术来表示刽子手的不同阶段。
def display_hangman(wrong_guesses):
stages = [
"""
-----
| |
o |
/|\ |
/ \ |
|
--------
""",
"""
-----
| |
o |
/|\ |
/ |
|
--------
""",
"""
-----
| |
o |
/|\ |
|
|
--------
""",
"""
-----
| |
o |
/| |
|
|
--------
""",
"""
-----
| |
o |
| |
|
|
--------
""",
"""
-----
| |
o |
|
|
|
--------
""",
"""
-----
| |
|
|
|
|
--------
"""
]
# reverse the list to display the stages in the correct order
stages.reverse()
return stages[wrong_guesses]
现在我们可以组合游戏的主循环了。这个循环将:
import random
# function to get a random word from the list
def get_random_word(word_list):
return random.choice(word_list)
# function to display the current state of the word
def display_word(word, guessed_letters):
display = ''
for letter in word:
if letter in guessed_letters:
display += letter + ' '
else:
display += '_ '
return display.strip()
# function to check if the word has been guessed
def is_word_guessed(word, guessed_letters):
for letter in word:
if letter not in guessed_letters:
return false
return true
# function to display the hangman figure
def display_hangman(wrong_guesses):
stages = [
"""
-----
| |
o |
/|\ |
/ \ |
|
--------
""",
"""
-----
| |
o |
/|\ |
/ |
|
--------
""",
"""
-----
| |
o |
/|\ |
|
|
--------
""",
"""
-----
| |
o |
/| |
|
|
--------
""",
"""
-----
| |
o |
| |
|
|
--------
""",
"""
-----
| |
o |
|
|
|
--------
""",
"""
-----
| |
|
|
|
|
--------
"""
]
# reverse the list to display the stages in the correct order
stages.reverse()
return stages[wrong_guesses]
# main function to play the game
def play_hangman():
word_list = ['python', 'java', 'hangman', 'programming', 'computer']
word = get_random_word(word_list)
guessed_letters = []
attempts = 6
wrong_guesses = 0
print("welcome to hangman!")
print("guess the word!")
# main game loop
while wrong_guesses < attempts:
print(display_hangman(wrong_guesses))
print(display_word(word, guessed_letters))
guess = input("enter a letter: ").lower()
# check if the guess is valid
if len(guess) != 1 or not guess.isalpha():
print("please enter a single letter.")
continue
# check if the letter has already been guessed
if guess in guessed_letters:
print("you have already guessed that letter.")
continue
guessed_letters.append(guess)
# check if the guess is in the word
if guess in word:
print(f"good guess! {guess} is in the word.")
else:
wrong_guesses += 1
print(f"sorry, {guess} is not in the word.")
print(f"you have {attempts - wrong_guesses} attempts left.")
# check if the word is fully guessed
if is_word_guessed(word, guessed_letters):
print(f"congratulations! you've guessed the word: {word}")
break
else:
print(display_hangman(wrong_guesses))
print(f"game over! the word was: {word}")
# run the game
if __name__ == "__main__":
play_hangman()
要运行游戏,只需执行 python 脚本,假设您创建了一个文件 main.py:
python main.py
游戏会提示您输入字母,并且随着您的进展,它会显示正确猜出的字母的单词。
如果您尝试次数用完,游戏就会结束,您就输了,如下所示:
-----
| |
o |
/| |
/ |
|
--------
game over! the word was: programming
如果你猜对了这个词,你就赢了,就像这样:
-----
| |
|
|
|
|
--------
j a _ a
Enter a letter: v
Good guess! v is in the word.
Congratulations! You've guessed the word: java
这个简单的 hangman 游戏演示了 python 中控制结构、函数、列表和基本输入/输出的使用。
构建此项目时,您可以添加更多功能,例如:
这是一个很棒的项目,可以在您学习更多高级 python 概念时进行扩展。
以上就是如何用 Python 构建 Hangman 游戏:分步指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号