https://github.com/bonaventureogeto/Learn-Python/blob/main/hangman.py
为流行的刽子手游戏构建 Python 脚本
在这个项目中,您将创建一个实现刽子手游戏的 Python 项目,使玩家能够尝试猜测隐藏的单词。如果您不熟悉该游戏,请在此处查看图形在线游戏。
以下是游戏运作方式的摘要;用户尝试猜测一排破折号中表示的单词,如果玩家猜出单词中存在的一个字母,程序会将其写在所有正确的位置,一旦全部用尽,这些尝试就会减少玩家做出的每一个错误的字符猜测,而没有猜到匹配的单词, 怪物吃掉了玩家。
以下是要遵循的步骤:
- 为您的程序选择一些好的刽子手词。您可以访问此网站并为您的游戏创建单词列表。
- 在游戏开始时,使用随机库从你的刽子手单词中随机选择一个单词,并将该单词的字母显示为破折号而不是字母。
- 现在,应用输入函数以允许用户进行字母猜测。使用 try…except 和 if 语句,以确保只有有效的输入才能使用您的脚本,即用户一次只能选择一个字母,并且用户不能选择之前选择的字母。
- 如果字母在单词中,则打印它来代替相应的破折号。如果单词中没有字母,则计为失败尝试,将用户的失败尝试次数限制为 6 次,以便在第 6 次失败时,游戏结束,脚本打印实际单词和“你输了”。如果用户在不到 6 次失败的尝试中猜出所有正确的字母,请务必告诉他们他们赢了。
- 如果你想把这个项目提升一个档次,你可以使用 Python Turtle Library 来可视化这个网站上的悬挂。这是可选的!
下面是刽子手游戏的示例 Python 脚本:
import random # List of hangman words hangman_words = ['python', 'programming', 'code', 'algorithm', 'development'] # Choose a random word from the list word = random.choice(hangman_words) # Create a list of dashes to represent the hidden word hidden_word = ['_'] * len(word) # Keep track of the number of failed attempts failed_attempts = 0 # Keep track of the letters that have been guessed guessed_letters = [] # Start the game while '_' in hidden_word and failed_attempts < 6: print(' '.join(hidden_word)) print('Guessed letters: ', guessed_letters) letter = input('Guess a letter: ') try: # Check if the input is a single letter if len(letter) != 1: raise ValueError # Check if the letter has already been guessed elif letter in guessed_letters: raise ValueError except ValueError: print('Invalid input. Please enter a single letter that has not been guessed before.') continue guessed_letters.append(letter) if letter in word: for i in range(len(word)): if word[i] == letter: hidden_word[i] = letter else: failed_attempts += 1 print(f'Letter not in word. You have {6 - failed_attempts} attempts remaining.') # Check if the player won or lost if '_' not in hidden_word: print(' '.join(hidden_word)) print('You won!') else: print('You lost.') print('The word was:', word)
您可以通过添加以下代码来使用 Python Turtle Library 来可视化悬挂,就像本网站中一样:
import turtle def draw_hangman(failed_attempts): # Create a turtle object t = turtle.Turtle() t.speed(10) # Draw the gallows t.penup() t.goto(-200, 0) t.pendown() t.forward(400) t.right(90) t.forward(200) t.right(90) t.forward(50) t.right(90) t.forward(50) # Draw the head if failed_attempts > 0: t.right(180) t.circle(25) # Draw the body if failed_attempts > 1: t.left(90) t.forward(100) # Draw the arms if failed_attempts > 2: t.right(90) t.forward(50) t.backward(100) t.forward(50) # Draw the legs if failed_attempts > 3: t.right(90) t.forward(50) t.backward(100) t.forward(50) # Draw the left if failed_attempts > 4: t.penup() t.goto(-20, 50) t.pendown() t.dot(10) # Draw the right eye if failed_attempts > 5: t.penup() t.goto(20, 50) t.pendown() t.dot(10) turtle.done() # Call the draw_hangman function inside the while loop while '_' in hidden_word and failed_attempts < 6: # Code for guessing letters and updating hidden_word draw_hangman(failed_attempts)
您还可以为游戏添加更多功能,例如允许玩家在输或赢后再次玩、跟踪分数等。
GitHub 存储库
查看此 GitHub 存储库,了解 Hangman 游戏的另一个实现。