2016-04-12 30 views
0

因此,我正在製作一款遊戲,並且我對python作爲一種編碼語言非常陌生,目前我沒有爲自己學習一門課。我正在製作一個基於文本的遊戲。我想將惠普作爲一個因素,並提供一個選項,以便在遊戲結束後重新啓動遊戲。這是我迄今爲止的代碼。如何實施惠普並在遊戲結束後自動重啓遊戲

print("Welcome to the journey to Camelot. This game is case sensitive, type the answers exactly as shown!") 
print("\nYou wake up and decide today you are going to become a knight of the round table. You are a skinny average joe" 
    " but, that does not bother you. You hunker up, get dressed, and decide what to bring with you. Do you bring a" 
    " 'Bow' or a 'Sword?'") 
SwordBow = input('>') 
if SwordBow == "Bow": 
    print("\nYou pick up your trusty bow and get ready to leave. You walk outside and notice it is raining. " 
     "This makes you lose some of your determination for this trip. Do you 'Go Home' or 'Continue Onward'") 
    GoHomeContinueOnward = input('>') 
    if GoHomeContinueOnward == "Go Home": 
      print("\nYou go home and go to the sleep, game over.") 
      ##Instance of a game over 
    elif GoHomeContinueOnward == "Continue Onward": 
      print("\nFiller") 
elif SwordBow == "Sword": 
    print("\nYou pick up your sturdy sword and prepare to leave. You notice it is raining. This makes you realize your" 
     " sword could rust. Do you 'Stick it under your shirt' or 'Eh keep it sheathed'?") 
    StickitunderyourshirtEhkeepitsheathed = input('>') 
    if StickitunderyourshirtEhkeepitsheathed == "Stick it under your shirt": 
      print("\nFiller") 
      print("\nYou have lost 10HP") 
      ##Here is one of the reasons I want to implement HP 
    elif StickitunderyourshirtEhkeepitsheathed == "Eh keep it sheathed": 
    print("\nFiller") 

我希望你能夠失去HP,如果你得到太低得到遊戲結束,如,如果 HP> 0: 打印(「遊戲結束」) 我打擾對不起你時間與這些簡單的問題,但正如我所說我是非常新的python,這是我試圖編寫的第一個完整的東西。最後,我希望它只是一個基於流程風格文本冒險的選擇。我還希望在遊戲結束後重新啓動該功能。預先感謝您的幫助,我只問,所以我知道如何在未來避免混亂等

回答

0

歡迎的網站:)

我注意到的第一件事是你的變量名的長度。 StickitunderyourshirtEhkeepitsheathed很長很難閱讀;將其重命名爲簡短但具有描述性的內容,可能爲weapon_action,並使用下劃線分隔單詞。

要實現hp,只需在名爲hp的腳本開始處添加一個變量並將其賦予一個默認值;例如hp = 100。然後,在print("\nYou have lost 10HP")之後寫hp -= 10hp = hp - 10(他們的意思是相同的)從hp減去10。

你應該看看一些關於如何編寫函數的教程,因爲這是你需要輕鬆地重新運行你的遊戲。目前,您的腳本將從上到下執行,並且沒有簡單的方法返回到開頭。如果你把所有東西都放在一個函數中, def main():def game():或任何你想調用它,你可以簡單地調用該函數,每次你想重新運行它。

+1

好的,謝謝你的迴應。我曾經在某一點做過def遊戲():在一開始就有一個點,但它是用我的第一個打印命令創建一個奇怪的錯誤,但我將不得不再次嘗試。我很欣賞這種反應,因爲我在Highschool的所有榮譽分類,同時試圖教我自己的代碼,快速解答幫助:) –

+0

@ZacharyArthur酷,不客氣。我也是一名自學者,享受我所做的所有免費作業 - 從來沒有想過我會這樣說:噢無論如何,將代碼放入函數是一種可行的方式。只要你的遊戲規模和複雜度增加,它就會變得混亂,沒有某種封裝。當項目增長得更多時,你可能會想把這些函數放在類中,然後你正在做面向對象的編程:) – jDo