內容簡介
本書是高校教師多年開發經驗的結晶之作,深入淺出地講解使用 Python語言進行遊戲蒸詢埋舉開發,幫助讀者快速掌握遊戲設計的基本原理和方法,同時提高套用 Python語言的編程能力。 全書精選十個遊戲案例,涵蓋不同的遊戲類型,每一章圍繞一個經典遊戲案例展開,並突出一個遊戲編程的主題。本書涉及的主要知識點有遊戲循環的原理、滑鼠及鍵盤事件處理、碰腿戰灑她撞檢測及處理、隨機數的運用、定時器的使用、遊戲場景的滾奔定墓動、角色動畫的播放、音效及音樂的播放、緩動效果、遊戲關卡設計、遊戲人工智慧的原理及運用等。本書將 Python語法知識及常用的編程技巧糅合在各個遊戲案例中介紹,為讀者展示 Python語言的實際運用場景。
圖書目錄
第 1章 神奇的遊戲循環:彈跳小球 ··· 1
1.1 準備工作 ································· 1
1.1.1 選擇合適的開發工具 ············· 1
1.1.2 設定開發環境請棄 ······················ 2
1.2 從何處開始 ······························ 3
1.2.1 創建程式視窗 ······················· 3
1.2.2 改變視窗大小和顏色 ·············· 5
1.2.3 顯示圖微碑殼像 ····························· 7
1.3 建立遊戲世界 ··························· 9
1.3.1 創建遊戲場景 ······················· 9
1.3.2 創建遊戲角色 ······················10
1.4 移動小球 ································11
1.4.1 改變小球坐標·······································11
1.4.2 遊戲束茅槳循環···················································································11
1.4.3 朝其他方向移動 ···················13
1.4.4 移動得快一些 ······················14
1.5 實現小球反彈 ··························14
1.5.1 檢測小球的位置 ···················14
1.5.2 將小球反彈回來 ···················15
1.6 加入更多的小球 ·······················17
1.6.1 添加兩個小球 ······················18
1.6.2 使用列表 ····························19
1.7 回顧與總結 ·····························21
第 2章用滑鼠控制遊戲:拼圖 ········24 2.1
添加圖片塊 ·····························24
2.1.1 準備圖片資源 ······················24
2.1.2 創建遊戲場景 ······················25
2.1.3 用列表管理圖片塊 ················26
2.2 打亂圖片塊 ·····························27
2.2.1 使用隨機函式 ······················27
2.2.2 將圖片塊顯示出來 ················28
2.3 移動圖片塊 ·····························30
2.3.1 處理滑鼠單擊事件 ················30
2.3.2 選取圖殼辨應片塊 ·························31
2.3.3 判斷圖片塊能否移動 ·············31
2.3.4 採用模組化編程方法 ·············32
2.3.5 改變圖片塊的位置 ················33
2.3.6 減少程式的縮進層級 ·············33
2.4 實現遊戲結束 ··························34
2.4.1 檢查拼圖是否完成 ················35
2.4.2 顯示最後一張圖片 ················36
2.4.3 播放聲音效果 ······················37
2.4.4 顯示文字信息 ······················38
2.5 回顧與總結 ·····························39
第 3章遞歸函式的威力:掃雷 ········42
3.1 創建方塊陣列 ··························42
3.1.1 準備圖片資源 ······················42
3.1.2 創建遊戲場景 ······················43
3.1.3 生成方塊陣列 ······················43
3.1.4 埋設地雷 ····························45
3.2 給方塊插上旗子 ·······················46
3.2.1 使用滑鼠右鍵來操作 ·············46
3.2.2 定義函式執行插旗操作 ··········47
3.3 打開方塊 ································49
3.3.1 完善滑鼠事件處理 ················49
3.3.2 獲取周圍的方格 ···················50
3.3.3 統計地雷數量 ······················52
3.3.4 遞歸調用打開方塊函式 ··········52
3.4 判定遊戲勝負 ··························54
3.4.1 遊戲失敗的處理 ···················54
3.4.2 遊戲勝利的處理 ···················55
3.5 回顧與總結 ·····························57
第 4章用鍵盤控制遊戲:貪食蛇 ·····60
4.1 創建場景和角色 ·······················60
4.1.1 創建遊戲場景 ······················60
4.1.2 創建貪食蛇 ·························61
4.2 移動蛇頭 ································62
4.2.1 處理鍵盤按鍵事件 ················62
4.2.2 讓蛇頭持續移動 ···················63
4.2.3 另一種鍵盤控制方式 ·············64
4.2.4 延緩貪食蛇的移動 ················65
4.2.5 旋轉蛇頭的圖像 ···················66
4.2.6 使用字典類型 ······················68
4.3 添加食物 ································69
4.3.1 讓食物隨機出現 ···················69
4.3.2 讓貪食蛇“吃”食物 ·············70
4.3.3 增長貪食蛇的身體 ················71
4.4 實現遊戲結束 ··························74
4.4.1 判斷貪食蛇碰到視窗邊界 ·······74
4.4.2 判斷蛇頭碰到自己身體 ··········74
4.5 回顧與總結 ·····························76
第 5章隨機數的妙用:打字 ············79
5.1 創建一個字母氣球 ····················79
5.1.1 創建遊戲場景 ······················79
5.1.2 創建氣球角色 ······················80
5.2 添加多個氣球 ··························81
5.2.1 創建多個氣球角色 ················81
5.2.2 隨機生成氣球的坐標 ·············83
5.2.3 隨機生成氣球的速度 ·············85
5.2.4 隨機生成氣球的字母 ·············87
5.3 實現打字功能 ··························89
5.3.1 匹配字母的按鍵 ···················89
5.3.2 消除氣球 ····························89
5.3.3 修補遊戲的 Bug ···················92
5.4 完善遊戲規則 ··························94
5.4.1 添加遊戲積分 ······················94
5.4.2 實現遊戲倒計時 ···················95
5.4.3 判定遊戲結束 ······················97
5.5 回顧與總結 ·····························98
第 6章碰撞檢測及處理:打磚塊 ··· 103
6.1 創建場景及角色 ····················· 103
6.1.1 創建遊戲場景 ···················· 103
6.1.2 創建遊戲角色 ···················· 104
6.2 讓角色動起來 ························ 106
6.2.1 移動擋板 ·························· 106
6.2.2 移動小球 ·························· 107
6.3 處理角色間的碰撞 ·················· 110
6.3.1 碰撞檢測的原理 ················· 110
6.3.2 小球與擋板的碰撞 ·············· 112
6.3.3 小球與磚塊的碰撞 ·············· 114
6.4 完善遊戲規則 ························ 116
6.4.1 設定遊戲積分 ···················· 117
6.4.2 添加生命值 ······················· 117
6.4.3 實現遊戲結束 ···················· 118
6.5 回顧與總結 ··························· 120
第 7章讓遊戲更加生動: Flappy Bird ······················ 124
7.1 創建遊戲場景 ························ 124
7.1.1 設定背景圖像 ···················· 124
7.1.2 滾動背景圖像 ···················· 126
7.2 添加障礙物 ··························· 128
7.2.1 設定地面 ·························· 128
7.2.2 設定水管 ·························· 129
7.3 添加小鳥 ······························ 131
7.3.1 創建小鳥角色 ···················· 131
7.3.2 模擬重力下的飛行 ·············· 132
7.3.3 播放飛行動畫 ···················· 133
7.4 小鳥與障礙物的互動 ··············· 135
7.4.1 小鳥與地面碰撞 ················· 135
7.4.2 小鳥與水管碰撞 ················· 136
7.4.3 小鳥飛越水管 ···················· 137
7.5 設計圖形用戶界面 ·················· 139
7.5.1 顯示 GUI圖像 ··················· 139
7.5.2 單擊“開始”按鈕 ·············· 141
7.5.3 播放背景音樂 ···················· 141
7.6 回顧與總結 ··························· 142
第 8章實現複雜的移動:飛機大戰 ·························· 146
8.1 創建遊戲場景 ························ 146
8.1.1 設定背景圖像 ···················· 146
8.1.2 滾動背景圖像 ···················· 148
8.2 添加英雄戰機 ························ 148
8.2.1 控制戰機移動 ···················· 148
8.2.2 播放戰機動畫 ···················· 149
8.3 添加子彈 ······························ 151
8.3.1 實現子彈射擊 ···················· 151
8.3.2 設定增強道具 ···················· 152
8.3.3 使用三角函式計算坐標 ········ 154
8.4 添加敵機 ······························ 157
8.4.1 設定緩動效果 ···················· 157
8.4.2 敵機與子彈的互動 ·············· 160
8.4.3 敵機與戰機的互動 ·············· 161
8.5 完善遊戲效果 ························ 165
8.5.1 設定 HUD ························ 165
8.5.2 播放背景音樂 ···················· 166
8.6 回顧與總結 ··························· 167
第 9章添加多個遊戲關卡:推箱子 ····························· 173
9.1 創建場景和角色 ····················· 173
9.1.1 設定遊戲關卡 ···················· 173
9.1.2 載入遊戲關卡 ···················· 175
9.2 實現角色的互動 ····················· 177
9.2.1 控制玩家角色的移動 ··········· 177
9.2.2 處理玩家角色的碰撞 ··········· 178
9.2.3 處理箱子角色的碰撞 ··········· 179
9.3 添加新的關卡 ························ 181
9.3.1 從檔案載入關卡 ················· 181
9.3.2 切換關卡 ·························· 184
9.4 完善遊戲功能 ························ 186
9.4.1 重置關卡 ·························· 186
9.4.2 判定遊戲結束 ···················· 187
9.5 回顧與總結 ··························· 188
第 10章人工智慧的奧秘:五子棋 ···························· 193
10.1 創建棋盤和棋子 ···················· 193
10.1.1 繪製棋盤 ························ 193
10.1.2 設定棋子 ························ 195
10.2 執行走棋操作 ······················· 195
10.2.1 使用滑鼠下棋 ·················· 195
10.2.2 交換下棋雙方 ·················· 197
10.3 完善遊戲規則 ······················· 199
10.3.1 判定棋局勝負 ·················· 199
10.3.2 添加悔棋功能 ·················· 202
10.4 實現人機對弈 ······················· 203
10.4.1 遊戲中的人工智慧 ············ 203
10.4.2 讓計算機學會下棋 ············ 204
10.5 回顧與總結 ·························· 209
附錄 A 配置開發環境 ···················· 215
附錄 B Python基礎語法參考 ········· 220
1.6.2 使用列表 ····························19
1.7 回顧與總結 ·····························21
第 2章用滑鼠控制遊戲:拼圖 ········24 2.1
添加圖片塊 ·····························24
2.1.1 準備圖片資源 ······················24
2.1.2 創建遊戲場景 ······················25
2.1.3 用列表管理圖片塊 ················26
2.2 打亂圖片塊 ·····························27
2.2.1 使用隨機函式 ······················27
2.2.2 將圖片塊顯示出來 ················28
2.3 移動圖片塊 ·····························30
2.3.1 處理滑鼠單擊事件 ················30
2.3.2 選取圖片塊 ·························31
2.3.3 判斷圖片塊能否移動 ·············31
2.3.4 採用模組化編程方法 ·············32
2.3.5 改變圖片塊的位置 ················33
2.3.6 減少程式的縮進層級 ·············33
2.4 實現遊戲結束 ··························34
2.4.1 檢查拼圖是否完成 ················35
2.4.2 顯示最後一張圖片 ················36
2.4.3 播放聲音效果 ······················37
2.4.4 顯示文字信息 ······················38
2.5 回顧與總結 ·····························39
第 3章遞歸函式的威力:掃雷 ········42
3.1 創建方塊陣列 ··························42
3.1.1 準備圖片資源 ······················42
3.1.2 創建遊戲場景 ······················43
3.1.3 生成方塊陣列 ······················43
3.1.4 埋設地雷 ····························45
3.2 給方塊插上旗子 ·······················46
3.2.1 使用滑鼠右鍵來操作 ·············46
3.2.2 定義函式執行插旗操作 ··········47
3.3 打開方塊 ································49
3.3.1 完善滑鼠事件處理 ················49
3.3.2 獲取周圍的方格 ···················50
3.3.3 統計地雷數量 ······················52
3.3.4 遞歸調用打開方塊函式 ··········52
3.4 判定遊戲勝負 ··························54
3.4.1 遊戲失敗的處理 ···················54
3.4.2 遊戲勝利的處理 ···················55
3.5 回顧與總結 ·····························57
第 4章用鍵盤控制遊戲:貪食蛇 ·····60
4.1 創建場景和角色 ·······················60
4.1.1 創建遊戲場景 ······················60
4.1.2 創建貪食蛇 ·························61
4.2 移動蛇頭 ································62
4.2.1 處理鍵盤按鍵事件 ················62
4.2.2 讓蛇頭持續移動 ···················63
4.2.3 另一種鍵盤控制方式 ·············64
4.2.4 延緩貪食蛇的移動 ················65
4.2.5 旋轉蛇頭的圖像 ···················66
4.2.6 使用字典類型 ······················68
4.3 添加食物 ································69
4.3.1 讓食物隨機出現 ···················69
4.3.2 讓貪食蛇“吃”食物 ·············70
4.3.3 增長貪食蛇的身體 ················71
4.4 實現遊戲結束 ··························74
4.4.1 判斷貪食蛇碰到視窗邊界 ·······74
4.4.2 判斷蛇頭碰到自己身體 ··········74
4.5 回顧與總結 ·····························76
第 5章隨機數的妙用:打字 ············79
5.1 創建一個字母氣球 ····················79
5.1.1 創建遊戲場景 ······················79
5.1.2 創建氣球角色 ······················80
5.2 添加多個氣球 ··························81
5.2.1 創建多個氣球角色 ················81
5.2.2 隨機生成氣球的坐標 ·············83
5.2.3 隨機生成氣球的速度 ·············85
5.2.4 隨機生成氣球的字母 ·············87
5.3 實現打字功能 ··························89
5.3.1 匹配字母的按鍵 ···················89
5.3.2 消除氣球 ····························89
5.3.3 修補遊戲的 Bug ···················92
5.4 完善遊戲規則 ··························94
5.4.1 添加遊戲積分 ······················94
5.4.2 實現遊戲倒計時 ···················95
5.4.3 判定遊戲結束 ······················97
5.5 回顧與總結 ·····························98
第 6章碰撞檢測及處理:打磚塊 ··· 103
6.1 創建場景及角色 ····················· 103
6.1.1 創建遊戲場景 ···················· 103
6.1.2 創建遊戲角色 ···················· 104
6.2 讓角色動起來 ························ 106
6.2.1 移動擋板 ·························· 106
6.2.2 移動小球 ·························· 107
6.3 處理角色間的碰撞 ·················· 110
6.3.1 碰撞檢測的原理 ················· 110
6.3.2 小球與擋板的碰撞 ·············· 112
6.3.3 小球與磚塊的碰撞 ·············· 114
6.4 完善遊戲規則 ························ 116
6.4.1 設定遊戲積分 ···················· 117
6.4.2 添加生命值 ······················· 117
6.4.3 實現遊戲結束 ···················· 118
6.5 回顧與總結 ··························· 120
第 7章讓遊戲更加生動: Flappy Bird ······················ 124
7.1 創建遊戲場景 ························ 124
7.1.1 設定背景圖像 ···················· 124
7.1.2 滾動背景圖像 ···················· 126
7.2 添加障礙物 ··························· 128
7.2.1 設定地面 ·························· 128
7.2.2 設定水管 ·························· 129
7.3 添加小鳥 ······························ 131
7.3.1 創建小鳥角色 ···················· 131
7.3.2 模擬重力下的飛行 ·············· 132
7.3.3 播放飛行動畫 ···················· 133
7.4 小鳥與障礙物的互動 ··············· 135
7.4.1 小鳥與地面碰撞 ················· 135
7.4.2 小鳥與水管碰撞 ················· 136
7.4.3 小鳥飛越水管 ···················· 137
7.5 設計圖形用戶界面 ·················· 139
7.5.1 顯示 GUI圖像 ··················· 139
7.5.2 單擊“開始”按鈕 ·············· 141
7.5.3 播放背景音樂 ···················· 141
7.6 回顧與總結 ··························· 142
第 8章實現複雜的移動:飛機大戰 ·························· 146
8.1 創建遊戲場景 ························ 146
8.1.1 設定背景圖像 ···················· 146
8.1.2 滾動背景圖像 ···················· 148
8.2 添加英雄戰機 ························ 148
8.2.1 控制戰機移動 ···················· 148
8.2.2 播放戰機動畫 ···················· 149
8.3 添加子彈 ······························ 151
8.3.1 實現子彈射擊 ···················· 151
8.3.2 設定增強道具 ···················· 152
8.3.3 使用三角函式計算坐標 ········ 154
8.4 添加敵機 ······························ 157
8.4.1 設定緩動效果 ···················· 157
8.4.2 敵機與子彈的互動 ·············· 160
8.4.3 敵機與戰機的互動 ·············· 161
8.5 完善遊戲效果 ························ 165
8.5.1 設定 HUD ························ 165
8.5.2 播放背景音樂 ···················· 166
8.6 回顧與總結 ··························· 167
第 9章添加多個遊戲關卡:推箱子 ····························· 173
9.1 創建場景和角色 ····················· 173
9.1.1 設定遊戲關卡 ···················· 173
9.1.2 載入遊戲關卡 ···················· 175
9.2 實現角色的互動 ····················· 177
9.2.1 控制玩家角色的移動 ··········· 177
9.2.2 處理玩家角色的碰撞 ··········· 178
9.2.3 處理箱子角色的碰撞 ··········· 179
9.3 添加新的關卡 ························ 181
9.3.1 從檔案載入關卡 ················· 181
9.3.2 切換關卡 ·························· 184
9.4 完善遊戲功能 ························ 186
9.4.1 重置關卡 ·························· 186
9.4.2 判定遊戲結束 ···················· 187
9.5 回顧與總結 ··························· 188
第 10章人工智慧的奧秘:五子棋 ···························· 193
10.1 創建棋盤和棋子 ···················· 193
10.1.1 繪製棋盤 ························ 193
10.1.2 設定棋子 ························ 195
10.2 執行走棋操作 ······················· 195