簡體   English   中英

如何在Python中制作劊子手游戲

[英]How to make a hangman game in Python

我在一個網站上找到了一個制作劊子手游戲的練習。 這是我所做的:

score = int(5)
word = 'orange'
guess = str(input('Enter your guess: '))

while score > 0 or guess == 'o' and 'r' and 'a' and 'n' and 'g' and 'e' or 'orange'
    if 'o' or 'r' or 'a' or 'n' or 'g' or 'e' or 'orange' == guess:
        print('CORRECT')
    else:
        score = score - 1
        print('Your score is ' + score)
        print('WRONG')

這段代碼是行不通的! 尤其是while loop毀了一切? 如何使它真正起作用。 我已經在 Jupyter 筆記本中編寫了這段代碼。

這是猜謎游戲的一個工作示例,刪除了一些代碼冗余並進行了一些清理。 重要提示:在 if 語句中插入 break 語句以避免無限循環,並在 else 語句中插入新的猜測提示:

score = 5
word = 'orange'
guess = str(input('Enter your guess: '))

while (score > 0) or (guess in ('o', 'r', 'a', 'n', 'g', 'e', 'orange')):
    if guess in ('o', 'r', 'a', 'n', 'g', 'e', 'orange'):
        print('CORRECT')
        break
    else:
        score -= 1
        print('WRONG')
        print('Your score is ' + str(score))
        guess = str(input('Enter your guess: '))

示例運行:

Enter your guess: u
WRONG
Your score is 4
Enter your guess: o
CORRECT

如果您想根據一組不同的選項檢查guess ,請使用in ; 由於優先級不同,您不能使用and or or or like that。

if guess in ('o', 'r', 'a', 'n', 'g', 'e', 'orange'):

會更好地為您工作(盡管這可能不是您代碼中的唯一問題)。

這里有很多事情要做 - 第一,你的代碼只允許一個劊子手游戲,其中要猜的詞是“橙色”。 單場比賽還好,但第二輪沒用,除非所有玩家都有嚴重的 memory 問題。

所以你需要abstract一點——寫下一些偽代碼,想想你的程序需要采取哪些步驟才能執行游戲中所需的操作。

這就像為人類寫下指令,只是形式也接近你的最終編程語言。

所以像:

# choose a word to play the game with, save it to the variable called word
word = 'orange'
# Reserve a list of letters that have been guessed.
# Start off with it empty - do the same to keep track of 
# good and bad guesses
guesses = []
good_guesses = []
bad_guesses = []
# Reserve a number of bad tries allowed before game over
bad_tries_before_game_over = 5
# Reserve a number of bad tries so far
bad_tries_so_far = 0

# Start a loop, we'll come back to this point each time 
# the player makes a new guess.
# There are two conditions when to not loop and those are:
# 1. when the player has won by guessing all the letters or
# 2. when the player has lost by getting too many wrong tries
# we'll use a variable called "finished" to track whether either of these conditions is met
finished = False
while not finished:
    guess = str(input('Enter your guess: '))

    # Check whether the person already guessed this letter
    if guess in guesses:
        print ( "You already guessed that letter.")
        print ( "Try using a letter you've not already guessed.")
        print ( guesses )
    else:
        # if guess is correct, then great, otherwise, add bad tries + 1
        if guess in word:
            print ( "Yes, {g} is in the word.".format(g=guess) )
            guesses.append(guess)
            good_guesses.append(guess)
        else:
            print ( "No, {g} is not in the word.".format(g=guess) )
            guesses.append(guess)
            bad_guesses.append(guess)
            bad_tries_so_far = bad_tries_so_far  + 1

        if bad_tries_so_far > bad_tries_before_game_over:
            print ( "Hangman. Game over. ")
            finished = True

        if set(word)==set(good_guesses):
            print ( "Hooray, you saved the man!.")
            finished = True

隨着時間的推移,您自然會想到python,它會變成自己的偽代碼。 但最好至少先嘗試在紙上或用英語(或任何適合您的語言)將您的想法付諸實踐,以制定您希望游戲具有的邏輯流程。

嗯,這是我做的一個。 它的互動性和現實性稍強一些,但制作文字有點煩人。 如果你想讓它更有趣,嘗試使用隨機詞生成器或類似的東西添加更多詞,讓劊子手更逼真,或者添加游戲模式和 VS 比賽。 我希望這會有所幫助=)

如果您仍然想知道這段代碼是如何工作的,我會盡可能多地嘗試和解釋。 首先,我輸入了時間來暫停,讓它更具戲劇性。 如果你想消除停頓,請成為我的客人。

第一批代碼只是開始而已。 如果需要,您可以刪除/更改它

之后,它會要求您輸入您的姓名。 就像我說的,完全可選。

接下來,它會告訴您輸入一個數字。 每個數字都有一個特定的單詞。

然后,是時候猜測了。 我應該解釋會發生什么。 假設這個詞是秘密的。 那是6個字母。

它看起來像這樣:

_

_

_

_

_

_

然后它要求你開始猜測。 如果你嘗試了 10 次,你就輸了。 假設您設法猜到了一個“s”。 有時候是這樣的:

s

_

_

_

_

_

希望這能解釋一切。 我正在編寫此代碼並計划接下來添加派對模式。

祝你有美好的一天 =)

幻影

import time
    time.sleep(5)
    print('Welcome to... hangman!')
    time.sleep(2)
    print("Are you ready? Ok, let's start.")
    time.sleep(3)
    name =input("What is your name? ")
    time.sleep(1)
    print("Hello, " + name + ". Time to play hangman! I wish you good luck- you'll need it.")
    time.sleep(3)
    darealword=int(input('Please enter a number. 1-30 only> '))
    if darealword==1:
        word='hypothesize'
    elif darealword==2:
        word='tube'
    elif darealword==3:
        word='blow'
    elif darealword==4:
        word='volume'
    elif darealword==5:
        word='parachute'
    elif darealword==6:
        word='biography'
    elif darealword==7:
        word='paragraph'
    elif darealword==8:
        word='abortion'
    elif darealword==9:
        word='exaggerate'
    elif darealword==10:
        word='complain'
    elif darealword==11:
        word='diagram'
    elif darealword==12:
        word='produce'
    elif darealword==13:
        word='abnormal'
    elif darealword==14:
        word='account'
    elif darealword==15:
        word='interactive'
    elif darealword==16:
        word='jump'
    elif darealword==17:
        word='goalkeeper'
    elif darealword==18:
        word='glimpse'
    elif darealword==19:
        word='story'
    elif darealword==20:
        word='coal'
    elif darealword==21:
        word='weave'
    elif darealword==22:
        word='dynamic'
    elif darealword==23:
        word='credibility'
    elif darealword==24:
        word='rhythm'
    elif darealword==25:
        word='trunk'
    elif darealword==26:
        word='admire'
    elif darealword==27:
        word='observation'
    elif darealword==28:
        word='rough'
    elif darealword==29:
        word='representative'
    else:
        word='thought'
    time.sleep(3)
    print("Start guessing... you can do this.")
    time.sleep(1)
    guesses = ''
    turns = 10
    while turns > 0:         
        failed = 0               
        for char in word:      
            if char in guesses:    
                print(char,)
            else:
                print("_",)    
                failed += 1    
        if failed == 0:
            time.sleep(5)        
            print('Aaaaaaand... you...') 
            time.sleep(3)
            print('won!!!!! I congratulate you, '+ name +'.')
            break              
        print
        guess = input("Guess a character> ") 
        guesses += guess                    
        if guess not in word:  
            turns -= 1        
            print("Sorry, this character isn't in the word. Don't get it wrong next time.")    
            # print("You have", + turns, 'more guesses')
            if turns == 0:        
                print("Aaaaaaand... you...")
                time.sleep(3)
                print("lost. I feel bad. Better luck next time. And if you're wondering, the word is "+ word +'.')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM