
- Python 基礎
- Python - 首頁
- Python - 概述
- Python - 歷史
- Python - 特性
- Python vs C++
- Python - Hello World 程式
- Python - 應用領域
- Python - 直譯器
- Python - 環境搭建
- Python - 虛擬環境
- Python - 基本語法
- Python - 變數
- Python - 資料型別
- Python - 型別轉換
- Python - Unicode 系統
- Python - 字面量
- Python - 運算子
- Python - 算術運算子
- Python - 比較運算子
- Python - 賦值運算子
- Python - 邏輯運算子
- Python - 位運算子
- Python - 成員運算子
- Python - 身份運算子
- Python - 運算子優先順序
- Python - 註釋
- Python - 使用者輸入
- Python - 數字
- Python - 布林值
- Python 控制語句
- Python - 控制流
- Python - 決策
- Python - If 語句
- Python - If else
- Python - 巢狀 If
- Python - Match-Case 語句
- Python - 迴圈
- Python - for 迴圈
- Python - for-else 迴圈
- Python - While 迴圈
- Python - break 語句
- Python - continue 語句
- Python - pass 語句
- Python - 巢狀迴圈
- Python 函式與模組
- Python - 函式
- Python - 預設引數
- Python - 關鍵字引數
- Python - 僅關鍵字引數
- Python - 位置引數
- Python - 僅位置引數
- Python - 可變引數
- Python - 變數作用域
- Python - 函式註解
- Python - 模組
- Python - 內建函式
- Python 字串
- Python - 字串
- Python - 字串切片
- Python - 修改字串
- Python - 字串連線
- Python - 字串格式化
- Python - 跳脫字元
- Python - 字串方法
- Python - 字串練習
- Python 列表
- Python - 列表
- Python - 訪問列表元素
- Python - 修改列表元素
- Python - 新增列表元素
- Python - 刪除列表元素
- Python - 迴圈遍歷列表
- Python - 列表推導式
- Python - 排序列表
- Python - 複製列表
- Python - 合併列表
- Python - 列表方法
- Python - 列表練習
- Python 元組
- Python - 元組
- Python - 訪問元組元素
- Python - 更新元組
- Python - 解包元組
- Python - 迴圈遍歷元組
- Python - 合併元組
- Python - 元組方法
- Python - 元組練習
- Python 集合
- Python - 集合
- Python - 訪問集合元素
- Python - 新增集合元素
- Python - 刪除集合元素
- Python - 迴圈遍歷集合
- Python - 合併集合
- Python - 複製集合
- Python - 集合運算子
- Python - 集合方法
- Python - 集合練習
- Python 字典
- Python - 字典
- Python - 訪問字典元素
- Python - 修改字典元素
- Python - 新增字典元素
- Python - 刪除字典元素
- Python - 字典檢視物件
- Python - 迴圈遍歷字典
- Python - 複製字典
- Python - 巢狀字典
- Python - 字典方法
- Python - 字典練習
- Python 陣列
- Python - 陣列
- Python - 訪問陣列元素
- Python - 新增陣列元素
- Python - 刪除陣列元素
- Python - 迴圈遍歷陣列
- Python - 複製陣列
- Python - 反轉陣列
- Python - 排序陣列
- Python - 合併陣列
- Python - 陣列方法
- Python - 陣列練習
- Python 檔案處理
- Python - 檔案處理
- Python - 寫入檔案
- Python - 讀取檔案
- Python - 重新命名和刪除檔案
- Python - 目錄
- Python - 檔案方法
- Python - OS 檔案/目錄方法
- Python - OS 路徑方法
- 面向物件程式設計
- Python - OOPs 概念
- Python - 類與物件
- Python - 類屬性
- Python - 類方法
- Python - 靜態方法
- Python - 建構函式
- Python - 訪問修飾符
- Python - 繼承
- Python - 多型
- Python - 方法重寫
- Python - 方法過載
- Python - 動態繫結
- Python - 動態型別
- Python - 抽象
- Python - 封裝
- Python - 介面
- Python - 包
- Python - 內部類
- Python - 匿名類和物件
- Python - 單例類
- Python - 包裝類
- Python - 列舉
- Python - 反射
- Python 錯誤與異常
- Python - 語法錯誤
- Python - 異常
- Python - try-except 塊
- Python - try-finally 塊
- Python - 丟擲異常
- Python - 異常鏈
- Python - 巢狀 try 塊
- Python - 使用者自定義異常
- Python - 日誌記錄
- Python - 斷言
- Python - 內建異常
- Python 多執行緒
- Python - 多執行緒
- Python - 執行緒生命週期
- Python - 建立執行緒
- Python - 啟動執行緒
- Python - 執行緒連線
- Python - 執行緒命名
- Python - 執行緒排程
- Python - 執行緒池
- Python - 主執行緒
- Python - 執行緒優先順序
- Python - 守護執行緒
- Python - 執行緒同步
- Python 同步
- Python - 執行緒間通訊
- Python - 執行緒死鎖
- Python - 中斷執行緒
- Python 網路程式設計
- Python - 網路程式設計
- Python - 套接字程式設計
- Python - URL 處理
- Python - 泛型
- Python 庫
- NumPy 教程
- Pandas 教程
- SciPy 教程
- Matplotlib 教程
- Django 教程
- OpenCV 教程
- Python 雜項
- Python - 日期與時間
- Python - 數學
- Python - 迭代器
- Python - 生成器
- Python - 閉包
- Python - 裝飾器
- Python - 遞迴
- Python - 正則表示式
- Python - PIP
- Python - 資料庫訪問
- Python - 弱引用
- Python - 序列化
- Python - 模板
- Python - 輸出格式化
- Python - 效能測量
- Python - 資料壓縮
- Python - CGI 程式設計
- Python - XML 處理
- Python - GUI 程式設計
- Python - 命令列引數
- Python - 文件字串
- Python - JSON
- Python - 傳送郵件
- Python - 擴充套件
- Python - 工具/實用程式
- Python - GUIs
- Python 高階概念
- Python - 抽象基類
- Python - 自定義異常
- Python - 高階函式
- Python - 物件內部
- Python - 記憶體管理
- Python - 元類
- Python - 使用元類進行超程式設計
- Python - 模擬和存根
- Python - 猴子補丁
- Python - 訊號處理
- Python - 型別提示
- Python - 自動化教程
- Python - Humanize 包
- Python - 上下文管理器
- Python - 協程
- Python - 描述符
- Python - 診斷和修復記憶體洩漏
- Python - 不可變資料結構
- Python 有用資源
- Python - 問答
- Python - 線上測驗
- Python - 快速指南
- Python - 參考
- Python - 速查表
- Python - 專案
- Python - 有用資源
- Python - 討論
- Python 編譯器
- NumPy 編譯器
- Matplotlib 編譯器
- SciPy 編譯器
Python - 基於文字的冒險遊戲(使用 pygame)
開發一個簡單的文字冒險遊戲是教授 Python 基礎知識的好方法,尤其是在使用 pygame 庫設計時。
本教程將包含有關如何構建遊戲的詳細分步說明,其中包括在一個房間中搜索鑰匙並開啟圖書館門的步驟。在本教程中,您將學習有關 pygame 的安裝、程式碼每個步驟的解釋以及如何運行遊戲及其輸出的方法。
安裝
首先,檢查您的計算機上是否安裝了 Python。這就是 **pygame** 發揮作用的地方,它是 Python 的一個開源遊戲和多媒體開發庫。
要安裝 **pygame**,您需要在計算機上開啟 cmd 或終端,然後輸入以下指令:
pip install pygame
基於文字的冒險遊戲的程式碼
以下是我們基於文字的冒險遊戲的完整程式碼。遊戲包含幾個房間:大廳、花園、餐廳和圖書館。玩家可以訪問兩個房間以嘗試找到隱藏的鑰匙。如果他們找到鑰匙,他們就可以進入圖書館房間。如果玩家在允許的嘗試次數內未能找到鑰匙,遊戲將結束。
import pygame import sys import random # Initialize pygame pygame.init() # Set up the display width, height = 800, 600 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption('Text Adventure Game') # Define colors BLACK = (0, 0, 0) WHITE = (255, 255, 255) # Set up fonts font = pygame.font.Font(None, 36) # Load images for different rooms room_images = { 'Start': pygame.Surface((width, height)), 'Hall': pygame.transform.scale(pygame.image.load('hall.png'), (width, height)), 'Garden': pygame.transform.scale(pygame.image.load('garden.png'), (width, height)), 'Dining Room': pygame.transform.scale(pygame.image.load('dining_room.png'), (width, height)), 'Library': pygame.transform.scale(pygame.image.load('library.png'), (width, height)), 'Game': pygame.Surface((width, height)) # Blank surface for the Game screen } # Fill the blank surface with black room_images['Game'].fill(BLACK) # Define room choices and chances room_choices = ['Hall', 'Garden', 'Dining Room'] key_room = random.choice(room_choices) # Randomly select which room contains the key chances_left = 2 # Define starting state current_screen = 'Start' current_room = None found_key = False # Define a function to draw the screen def draw_screen(screen_name): screen.blit(room_images[screen_name], (0, 0)) if screen_name == 'Start': start_text = font.render('WELCOME TO THE GAME', True, WHITE) instructions_text = font.render('LOOK: You have only two chances to access the Library room.', True, WHITE) start_button_text = font.render('Start', True, BLACK) exit_button_text = font.render('Exit', True, BLACK) screen.blit(start_text, (20, 20)) screen.blit(instructions_text, (20, 60)) pygame.draw.rect(screen, WHITE, pygame.Rect(300, 200, 200, 50)) screen.blit(start_button_text, (375, 215)) pygame.draw.rect(screen, WHITE, pygame.Rect(300, 300, 200, 50)) screen.blit(exit_button_text, (375, 315)) elif screen_name == 'Game': if current_room: screen.blit(room_images[current_room], (0, 0)) game_text = font.render(f'You have {chances_left} chances left to find the key.', True, WHITE) screen.blit(game_text, (20, 20)) if found_key: key_found_text = font.render('You successfully found the key! Now you can go to the Library room.', True, WHITE) screen.blit(key_found_text, (20, 60)) # Draw the navigation buttons for room, rect in button_rects.items(): if room in room_choices or room == 'Library': pygame.draw.rect(screen, WHITE, rect) text_surface = font.render(button_texts[room], True, BLACK) screen.blit(text_surface, (rect.x + 10, rect.y + 10)) elif screen_name == 'Library': screen.blit(room_images['Library'], (0, 0)) library_text = font.render('Welcome to the Library!', True, WHITE) screen.blit(library_text, (20, 20)) # Define buttons for navigation and initial screen button_rects = { 'Start': pygame.Rect(300, 200, 200, 50), 'Exit': pygame.Rect(300, 300, 200, 50), 'Hall': pygame.Rect(50, 500, 150, 50), 'Garden': pygame.Rect(225, 500, 150, 50), 'Dining Room': pygame.Rect(400, 500, 150, 50), 'Library': pygame.Rect(575, 500, 150, 50), } # Define button text button_texts = { 'Start': 'Start', 'Exit': 'Exit', 'Hall': 'Go to Hall', 'Garden': 'Go to Garden', 'Dining Room': 'Go to Dining Room', 'Library': 'Go to Library', } # Main loop running = True while running: # Clear the screen screen.fill(BLACK) # Draw the current screen draw_screen(current_screen) # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: running = False elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: # Left mouse button if current_screen == 'Start': if button_rects['Start'].collidepoint(event.pos): current_screen = 'Game' elif button_rects['Exit'].collidepoint(event.pos): running = False elif current_screen == 'Game': for room, rect in button_rects.items(): if rect.collidepoint(event.pos): if room in room_choices: current_room = room if room == key_room: found_key = True else: chances_left -= 1 if chances_left <= 0 and not found_key: current_screen = 'Start' # Game over if no chances left elif room == 'Library': if found_key: current_screen = 'Library' else: current_screen = 'Game' # Can't access Library without the key # Update the display pygame.display.flip() # Quit pygame pygame.quit() sys.exit()
使用的圖片
以下是我們在上述程式碼中用於設計基於文字的冒險遊戲的圖片:


這些是我們使用的圖片。您也可以從 Google 下載隨機圖片並使用它們。
運行遊戲的步驟
- **設定** - 首先確保您已安裝 Python 以及 Pygame。
- **準備圖片** - 將所需的房間圖片(hall.png、garden.png、dining_room.png 和 library.png)放在與 Python 指令碼相同的目錄中。
- **執行指令碼** - 透過在您的終端或命令提示符中執行 python your_script_name.py 來執行指令碼。
輸出截圖
1. 初始螢幕
遊戲開始時會顯示一個螢幕,提供“開始”或“退出”選項。

2. 遊戲玩法
點選“開始”後,您將進入遊戲畫面,在這裡您可以選擇房間以搜尋鑰匙。

您可以隨意前往大廳、花園、餐廳等任何房間。我們點選大廳房間:

3. 找到鑰匙
如果您找到鑰匙,頂部會出現一條訊息,指示您現在可以進入圖書館。
4. 訪問圖書館
找到鑰匙後,點選“圖書館”房間以訪問它並完成遊戲。

python_projects_from_basic_to_advanced.htm
廣告