python學習-遊戲2

# coding:utf-8
#!/usr/bin/python
'''
Created on Jan 4, 2018

@author: wayne
'''
import random #導入模塊
import time

def displayIntro():         #定義一個函數,不執行代碼,在調用的時候才執行
        print('you are in a land full of dragons.In front of you')
        print('you see two caves.In one cave,the dragon is friendly')
        print('and will share his treasure with you the other dragon')
        print('is greedy and hungry,and will eat you on sight.')
        print()

def chooseCave():       #定義一個玩家選擇要進入的1或者2的函數
        cave = ''           #建立一個新的空字符串
        while cave != '1' and cave != '2':
                '''
                                                布爾操做符,and,or,not
                and 表示:表達式全爲true爲true,其中一個值爲False或者全爲False 表達式結果爲False
                >>> True and True
                True
                >>>True and False
                False
                >>> False and False
                False
                or:只要其中一個值爲True 那麼結果就爲True ,只有兩個值都爲False的時候纔是False
                >>>True or False
                True
                >>> True or True
                True
                >>>False or False
                False
                not :只能做用一個值,不能兩個值組合在一塊兒,not是取值的反值
                >>> not True
                False
                >>> not False
                True
                >>> not (1 ==2)
                True
                '''        
                print('which cave will you go into?(1 or 2)')
                cave = input() #接收輸入值,
                return cave
def checkCave(chosenCave):  #繼承chosenCave的函數值
        print('you approach the cave .....')
        time.sleep(2)
        print('it is dark and spooky.....')
        time.sleep(2)
        print('A large dragon jumps out in front of you!he opens his jaws and .....')
        print()
        time.sleep(2)

        friendlyCave = random.randint(1,2)  #隨機產生一個數字,在1,2中間
        if chosenCave == str(friendlyCave): #判斷輸入值和隨機產生的值是否一致,一致則玩家獲勝,不然失敗
                print('Gives you his treasure!')
        else:
                print('Gobbles you down in one bite!')
playAgain = 'yes'       #存儲一個初始值,"yes"

while playAgain == 'yes' or playAgain == 'y': #判斷條件是否爲True
        displayIntro() #調用函數
        caveNumber = chooseCave()   #調用輸入值的函數
        checkCave(caveNumber) #調用checkCave的函數
        print('Do you want to play again!(yes or no)') #判斷玩家是否還要玩
        playAgain = input() #接收鍵盤輸入的值,若是是True則循環開始
相關文章
相關標籤/搜索