趣学Python游戏编程
作者: 何青 著
出版时间: 2020年版
内容简介
《趣学Python游戏编程》是高校教师多年开发经验的结晶之作,深入浅出地讲解使用Python语言进行游戏开发,帮助读者快速掌握游戏设计的基本原理和方法,同时提高应用Python语言的编程能力。《趣学Python游戏编程》精选十个游戏案例,涵盖不同的游戏类型,每一章围绕一个经典游戏案例展开,并突出一个游戏编程的主题。《趣学Python游戏编程》涉及的主要知识点有游戏循环的原理、鼠标及键盘事件处理、碰撞检测及处理、随机数的运用、定时器的使用、游戏场景的滚动、角色动画的播放、音效及音乐的播放、缓动效果、游戏关卡设计、游戏人工智能的原理及运用等。《趣学Python游戏编程》将Python语法知识及常用的编程技巧糅合在各个游戏案例中介绍,为读者展示Python语言的实际运用场景。《趣学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