当前位置:   article > 正文

python编程火柴人格斗:简单游戏示例

python火柴人打架代码

119c4d31e5df6b28acc9b1976d27c601.png

这个游戏是我初学python时写的,个人觉得例子非常好,现在分享给大家。

每一行代码不懂得直接评论吧我都会详细解释~,希望能有助于各位的学习。

0a263133381186a6ea399702fef21703.png

游戏如下:

在单词库中给出一系列单词,如果猜错火柴人会一步步形成:

15260e62ffd31a67931539eb11689302.png

如果猜对,则会显示单词,并恭喜您答对单词~

1a82594ce11b3fa4efd9a474ab3d2d70.png

附上源码:

  1. import random
  2. # 构建图像
  3. HANGMAN_PICS = ['''
  4. # +---+
  5. # |
  6. # |
  7. # |
  8. # ===''','''
  9. # +---+
  10. # 0 |
  11. # |
  12. # |
  13. # === ''','''
  14. # +---+
  15. # 0 |
  16. # | |
  17. # |
  18. # === ''','''
  19. # +---+
  20. # 0 |
  21. # /| |
  22. # |
  23. # === ''','''
  24. # +---+
  25. # 0 |
  26. # /| |
  27. # |
  28. # === ''','''
  29. # +---+
  30. # 0 |
  31. # /| |
  32. # / |
  33. # === ''','''
  34. # +---+
  35. # 0 |
  36. # /| |
  37. # / |
  38. # === ''']
  39. # 定义单词库
  40. words = 'fantasy salute tike pisces raven zara stussy'.split()
  41. # 定义选择的单词方法
  42. # wordList为单词列表
  43. def getRandomWord(wordList):
  44. wordIndex = random.randint(0,len(wordList) - 1)
  45. return wordList[wordIndex]
  46. # 定义显示板
  47. # missedLetters correctLetters secretWord 玩家猜过不在神秘单词里面,玩家猜过在神秘单词,神秘单词
  48. def displayBoard(missedLetters,correctLetters,secretWord):
  49. # HANGMAN_PICS为全局变量,即整个火柴的全部内容
  50. print(HANGMAN_PICS[len(missedLetters)])
  51. print()
  52. # 打印出错误的字母,用for 循环在打印出每个字母
  53. print('Missed letters:',end='')
  54. for letter in missedLetters:
  55. print(letter,end='')
  56. print()
  57. # 对__的长度进变化
  58. blank='_'*len(secretWord)
  59. # 对blank中的东西进行替换
  60. for i in range(len(secretWord)):
  61. if secretWord[i] in correctLetters:
  62. # --[:2]=>0,1 [2:]=>2到最后 含头不含尾
  63. blank = blank[:i]+secretWord[i]+blank[i+1:]
  64. for letter in blank:
  65. print(letter,end='')
  66. print()
  67. # 玩家猜测的数据
  68. def getGuess(alreadyGuessed):
  69. while True:
  70. print('Guess a letter.')
  71. guess = input()
  72. guess = guess.lower()
  73. if len(guess)!=1:
  74. print("please enter a single letter")
  75. elif guess in alreadyGuessed:
  76. print("猜对了,请继续")
  77. else:
  78. return guess
  79. # 判断玩家是否还想在玩一次
  80. def playAgian():
  81. print("你还想再玩么 (yes or no)")
  82. return input().lower().startswith('y')
  83. # 程序开始执行并循环
  84. print('H A N G M A N ')
  85. missedLetters = ''
  86. correctLetters = ''
  87. secretWord = getRandomWord(words)
  88. gameisDone = False
  89. while True:
  90. displayBoard(missedLetters,correctLetters,secretWord)
  91. guess = getGuess(missedLetters + correctLetters)
  92. if guess in secretWord:
  93. correctLetters =correctLetters+ guess
  94. foundAllLetters = True
  95. for i in range(len(secretWord)):
  96. if secretWord[i] not in correctLetters:
  97. foundAllLetters = False
  98. break
  99. if foundAllLetters:
  100. print('yes! the secretWord is " '+ secretWord +'"! you have won ')
  101. gameisDone
  102. else:
  103. missedLetters = missedLetters +guess
  104. if len(missedLetters)== len(HANGMAN_PICS) -1:
  105. displayBoard(missedLetters,correctLetters,secretWord)
  106. print('you have run out of guesses! n After '+ str(len(missedLetters)) + 'missed guesses and ' +
  107. str(len(correctLetters)) + 'correct guessed,the word was "' + secretWord + '" ')
  108. gameisDone = True
  109. if gameisDone:
  110. if playAgian():
  111. missedLetters=''
  112. correctLetters=''
  113. gameisDone=False
  114. secretWord = getRandomWord(words)
  115. else:
  116. break

输入win+R=》cmd打开终端,再将文档拖入终端内,即可执行(或者直接用python运行)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/405922
推荐阅读
相关标签
  

闽ICP备14008679号