hangman game
hangman就是我們常常在玩的猜單字遊戲(小時候上英文課時常常玩)
遊戲結束的方式有兩種:
1.猜對完整單字,人沒被完整畫出來
2.人被完整畫出來
那我們今天就是要用python把遊戲寫出來啦!
大概構思:
首先 當然要先有單字,能讓使用者去猜,一開始測試時,可以用list裝幾個單字
第二步 就是從list中,隨機選取一個單字,這時候就要使用random模組的choice
第三步 讓使用者輸入一個字母,可以做防呆機制,例如:把使用者輸入的字母,都轉換成小寫或大寫
第四步 用迴圈去檢查使用者輸入的字母,是否存在我們隨機挑出來的字母中
第五步 去判斷上面迴圈跑的結果。
hangman_art模組程式碼:
stages = ['''
+---+
| |
O |
/|\ |
/ \ |
|
=========
''', '''
+---+
| |
O |
/|\ |
/ |
|
=========
''', '''
+---+
| |
O |
/|\ |
|
|
=========
''', '''
+---+
| |
O |
/| |
|
|
=========''', '''
+---+
| |
O |
| |
|
|
=========
''', '''
+---+
| |
O |
|
|
|
=========
''', '''
+---+
| |
|
|
|
|
=========
''']
logo = '''
_
| |
| |__ __ _ _ __ __ _ _ __ ___ __ _ _ __
| '_ \ / _` | '_ \ / _` | '_ ` _ \ / _` | '_ \
| | | | (_| | | | | (_| | | | | | | (_| | | | |
|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
__/ |
|___/ '''
hangman_words模組程式碼:
word_list = [
'abruptly',
'absurd',
'abyss',
'affix',
'askew',
'avenue',
'awkward',
'axiom',
'azure',
'bagpipes',
'bandwagon',
'banjo',
'bayou',
'beekeeper',
'bikini',
'blitz',
'blizzard',
'boggle',
'bookworm',
'boxcar',
'boxful',
'buckaroo',
'buffalo',
'buffoon',
'buxom',
'buzzard',
'buzzing',
'buzzwords',
'caliph',
'cobweb',
'cockiness',
'croquet',
'crypt',
'curacao',
'cycle',
'daiquiri',
'dirndl',
'disavow',
'dizzying',
'duplex',
'dwarves',
'embezzle',
'equip',
'espionage',
'euouae',
'exodus',
'faking',
'fishhook',
'fixable',
'fjord',
'flapjack',
'flopping',
'fluffiness',
'flyby',
'foxglove',
'frazzled',
'frizzled',
'fuchsia',
'funny',
'gabby',
'galaxy',
'galvanize',
'gazebo',
'giaour',
'gizmo',
'glowworm',
'glyph',
'gnarly',
'gnostic',
'gossip',
'grogginess',
'haiku',
'haphazard',
'hyphen',
'iatrogenic',
'icebox',
'injury',
'ivory',
'ivy',
'jackpot',
'jaundice',
'jawbreaker',
'jaywalk',
'jazziest',
'jazzy',
'jelly',
'jigsaw',
'jinx',
'jiujitsu',
'jockey',
'jogging',
'joking',
'jovial',
'joyful',
'juicy',
'jukebox',
'jumbo',
'kayak',
'kazoo',
'keyhole',
'khaki',
'kilobyte',
'kiosk',
'kitsch',
'kiwifruit',
'klutz',
'knapsack',
'larynx',
'lengths',
'lucky',
'luxury',
'lymph',
'marquis',
'matrix',
'megahertz',
'microwave',
'mnemonic',
'mystify',
'naphtha',
'nightclub',
'nowadays',
'numbskull',
'nymph',
'onyx',
'ovary',
'oxidize',
'oxygen',
'pajama',
'peekaboo',
'phlegm',
'pixel',
'pizazz',
'pneumonia',
'polka',
'pshaw',
'psyche',
'puppy',
'puzzling',
'quartz',
'queue',
'quips',
'quixotic',
'quiz',
'quizzes',
'quorum',
'razzmatazz',
'rhubarb',
'rhythm',
'rickshaw',
'schnapps',
'scratch',
'shiv',
'snazzy',
'sphinx',
'spritz',
'squawk',
'staff',
'strength',
'strengths',
'stretch',
'stronghold',
'stymied',
'subway',
'swivel',
'syndrome',
'thriftless',
'thumbscrew',
'topaz',
'transcript',
'transgress',
'transplant',
'triphthong',
'twelfth',
'twelfths',
'unknown',
'unworthy',
'unzip',
'uptown',
'vaporize',
'vixen',
'vodka',
'voodoo',
'vortex',
'voyeurism',
'walkway',
'waltz',
'wave',
'wavy',
'waxy',
'wellspring',
'wheezy',
'whiskey',
'whizzing',
'whomever',
'wimpy',
'witchcraft',
'wizard',
'woozy',
'wristwatch',
'wyvern',
'xylophone',
'yachtsman',
'yippee',
'yoked',
'youthful',
'yummy',
'zephyr',
'zigzag',
'zigzagging',
'zilch',
'zipper',
'zodiac',
'zombie',
]
主程式碼:
#模組
from replit import clear
import random
import hangman_art
import hangman_words
#隨機選單字
chosen_word = random.choice(hangman_words.word_list)
#取得單字長度
word_length = len(chosen_word)
#while loop 條件
end_of_game = False
#預設生命6,以配合ascii的圖輸出
lives = 6
#logo
print(hangman_art.logo)
#用來裝單字的list
display = []
#單字目前都是"_"狀態,因為使用者還沒猜
for _ in range(word_length):
display += "_"
while not end_of_game:
guess = input("Guess a letter: ").lower()
#清理版面,保持乾淨
clear()
#提醒使用者,如果猜到重複的字母時
if guess in display:
print("You have already enter %s ,and it is in the word."%(guess))
#比對猜的字母,是否存在單字中
for position in range(word_length):
letter = chosen_word[position]
#猜對就替換"_"變成使用者所猜的字母
if letter == guess:
display[position] = letter
#如果猜錯字母要做的事
if guess not in chosen_word:
print("You guess %s,but %s is not in the word."%(guess,guess))
lives -= 1
if lives == 0:
end_of_game = True
print("You lose.")
print(f"{' '.join(display)}")
#檢查是否還有"_"
if "_" not in display:
end_of_game = True
print("You win.")
#目前hangman的進度
print(hangman_art.stages[lives])
留言
張貼留言