萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> 程式語言綜合

Python 文字遊戲實作範例:Dragon Realm 猜檔案與 Guess Number 破解密碼鎖

以經典《Invent with Python》範例為基礎,本文呈現兩個完整的文字互動遊戲:files.py 猜測好壞檔案、digitcode.py 三位數密碼破解。展示 random、time、input、迴圈控制、條件判斷、遊戲迴圈等基礎程式設計概念。

遊戲一:Dragon Realm - 猜檔案

############################################################
# 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:> ')

遊戲二:Guess the Number - 破解密碼鎖

############################################################
# 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 == goodfileif guess < number / > / ==
計數器無限玩 (playAgain)guessesTaken 限制 15 次
延遲效果time.sleep(1) 戲劇張力time.sleep(1) 模擬驗證過程
函式分解displayIntro/chooseOption/checkOption單一主流程

擴充練習建議

  • 加入難度等級:易/中/難對應不同範圍與嘗試次數
  • 記錄高分榜:使用 json 或 sqlite3 持久化
  • 加入提示系統:abs(guess - number) < 10 顯示「很接近了」
  • 重構為類別:Game 基類 + GuessGame/FileGame 子類
  • 加入單元測試:測試 checkOption 邏輯獨立性
copyright © 萬盛學電腦網 all rights reserved