以經典《Invent with Python》範例為基礎,本文呈現兩個完整的文字互動遊戲:files.py 猜測好壞檔案、digitcode.py 三位數密碼破解。展示 random、time、input、迴圈控制、條件判斷、遊戲迴圈等基礎程式設計概念。
############################################################
# My version on the game "Dragon Realm"
# taken from "Invent with Python" by Al Sweigart
# Python 3
############################################################
import random
import time
print('\n\n[--system--] one file is bad the other is good ..guess the right one.\n')
print('\nconnecting....')
time.sleep(1)
print('....')
time.sleep(1)
print('....')
time.sleep(1)
print('....')
time.sleep(1)
print('\nconnection established')
def displayIntro():
print('------------')
print('SYSTEM FILES')
print('------------\n')
print('1.) file.')
print('2.) file.\n')
def chooseOption():
option = ''
while option != '1' and option != '2':
print('which file to download? 1 or 2')
option = input('user:> ')
return option
def checkOption(chosenOption):
print('\ninitialising download....')
time.sleep(1)
print('accessing file....')
time.sleep(1)
print('downloading....')
time.sleep(1)
print('....')
time.sleep(1)
print('....')
time.sleep(1)
goodfile = random.randint(1, 2)
if chosenOption == str(goodfile):
print('\ndownload complete.')
print('\nGAME OVER')
else:
print('\nfile corrupt')
print('system infected.')
print('\nGAME OVER')
playAgain = 'yes'
while playAgain == 'yes':
displayIntro()
optionNumber = chooseOption()
checkOption(optionNumber)
print('\ndownload again? .... (yes or no)')
playAgain = input('user:> ')
############################################################
# My version of the game "guess the number"
# taken from "Invent with Python" by Al Sweigart
# Python 3
############################################################
import random
import time
guessesTaken = 0
print('\n\n\n[--system--] enter code in 15 trys to avoid lockout\n')
print('\nconnecting....')
time.sleep(1)
print('....')
time.sleep(1)
print('....')
time.sleep(1)
print('....')
time.sleep(1)
print('connection established\n')
print('---------------------')
print(' MAINFRAME - LOGIN ')
print('---------------------')
print('\nenter 3 digit access code..')
number = random.randint(0, 999) # 000-999
while guessesTaken < 15:
print()
guess = input('user:> ')
guess = int(guess)
guessesTaken += 1
if guess < number:
print('\nACCESS - DENIED - code too low')
if guess > number:
print('\nACCESS - DENIED - code too high')
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print('\nverifying ....')
time.sleep(1)
print('nauthenticating ....')
time.sleep(1)
print('....')
time.sleep(1)
print('....')
time.sleep(1)
print('\nACCESS - GRANTED')
print('\nGAME OVER\n')
exit(0)
if guess != number:
number = str(number)
print('\n....')
time.sleep(1)
print('\n....')
time.sleep(1)
print('\nSYSTEM LOCKED - the code was ' + number)
print()
exit(0)
| 概念 | 遊戲一應用 | 遊戲二應用 |
|---|---|---|
| 隨機數生成 | random.randint(1,2) 決定好檔 | random.randint(0,999) 生成密碼 |
| 輸入驗證迴圈 | while option not in ('1','2') | 隱含在 15 次限制中 |
| 條件分支 | if chosen == goodfile | if guess < number / > / == |
| 計數器 | 無限玩 (playAgain) | guessesTaken 限制 15 次 |
| 延遲效果 | time.sleep(1) 戲劇張力 | time.sleep(1) 模擬驗證過程 |
| 函式分解 | displayIntro/chooseOption/checkOption | 單一主流程 |
json 或 sqlite3 持久化abs(guess - number) < 10 顯示「很接近了」Game 基類 + GuessGame/FileGame 子類checkOption 邏輯獨立性