Python 猜字游戏



刽子手是一个猜词游戏,玩家尝试通过建议字母来猜出一个单词。对于每个错误的猜测,都会画出刽子手的一部分。当玩家正确猜出单词或刽子手完成时,游戏结束。

创建刽子手游戏的 Python 代码

让我们从刽子手游戏的基本结构开始:

import random

def get_word(word_list):

   # Function to randomly select a word from the list word = random.choice(word_list)
   return word.upper()

def display_hangman(tries):

   # Function to display the hangman based on the number of tries 
   stages = [ '''

      ------
      |	  |

      |

      |

      |
 
      |

      =========''',

      '''

      ------

      |	   |

      |	   O

      |

      |

      |

      =========''',

      '''

      --------

      |	   |

      |	   O

      |	   |

      |

      |

      =========''',

      '''

      -------

      |	   |

      |	   O
 
      |	  /|

      |

      |

      =========''',

      '''

      -------

      |	   |

      |	   O

      |	  /|\\

      |

      |

      =========''',

      '''

      --------

      |	   |

      |	   O

      |	  /|\\

      |	   |

      |

      =========''',

      '''

      ------
 
      |	  |

      |	  O

      |	 /|\\

      |	  |

      |	  /

      =========''',

      '''

      --------

      |	   |

      |	   O

      |	  /|\\

      |	   |

      |	  / \\

      ========='''

   ]

   return stages[tries]

def play_hangman():

   word_list = ["PYTHON", "HANGMAN", "GUESS", "CODE"]

   word = get_word(word_list)

   word_letters = set(word) # Letters in the word alphabet = set('ABCDEFGHIJKLMNOPǪRSTUVWXYZ')
 
   used_letters = set() # What user has guessed


lives = 6


# Getting user input

while len(word_letters) > 0 and lives > 0: 
   print(display_hangman(lives))
   word_list_display = [letter if letter in used_letters else '_' for letter in word]
   print('Current word: ', ' '.join(word_list_display))
   user_letter = input('Guess a letter: ').upper()

   if len(user_letter) != 1:

      print("\nPlease enter a single letter.") 
   elif user_letter in alphabet - used_letters:
      used_letters.add(user_letter) 
      if user_letter in word_letters:
         word_letters.remove(user_letter) 
      else:
         lives -= 1

         print('\nLetter is not in the word.') 
   elif user_letter in used_letters:
      print('\nYou have already guessed that letter.')
 
   else:

      print('\nInvalid character.')


# Game over 
if lives == 0:
   print(display_hangman(lives)) 
   print('You died. The word was', word)
else:

   print('You guessed the word!')

play_hangman() 

输出

|	|
|	O
|	/|\
|	|
|	/
=========
Current word: _ _ _ _
Guess a letter: a


Letter is not in the word.

输出

|	|
|	O
|	/|\
|	|
|
=========
Current word: _ _ _ _
Guess a letter: s


Letter is not in the word.




|	|
|	O
|	/|\
|
|
=========
Current word: _ _ _ _
Guess a letter: a


You have already guessed that letter.




|	|
 
|	O
|	/|\
|
|
=========
Current word: _ _ _ _
Guess a letter: e




|	|
|	O
|	/|\
|
|
=========
Current word: _ _ _ E Guess a letter: n

Letter is not in the word.




|	|
|	O
|	/|
|
|
 
=========
Current word: _ _ _ E Guess a letter: w

Letter is not in the word.




|	|
|	O
|	|
|
|
=========
Current word: _ _ _ E Guess a letter: d




|	|
|	O
|	|
|
|
=========
Current word: _ _ D E Guess a letter: m
 
Letter is not in the word.




|	|
|	O
|
|
|
=========
Current word: _ _ D E Guess a letter: p

Letter is not in the word.




|	|
|
|
|
|
=========
You died. The word was CODE

代码解释

  • **导入 random** 导入 random 模块用于选择随机单词
  • **get_word 函数** 从提供的单词列表中选择一个随机单词,并以小写形式返回它
  • **display_hangman 函数** 根据错误猜测的次数显示刽子手
  • play_hangman 函数
    • 创建一个单词列表,选择一个随机单词,并初始化单词字母、字母表和已用字母的集合。
    • 将生命值设置为
    • 游戏循环持续到所有字母都被猜到或生命值达到 0。
    • 显示刽子手、当前单词并提示用户输入。
    • 检查猜测的字母是正确、错误还是已使用。
    • 相应地更新生命值并显示消息。

游戏结束后,显示最终结果。

python_projects_from_basic_to_advanced.htm
广告