
目录
【Pygame 第 1 课】 hello pygame
............................................................................
1
【Pygame 第 2 课】 游戏的本质
................................................................................
4
【Pygame 第 3 课】 游戏中的事件
............................................................................
5
【Pygame 第 4 课】 获取鼠标位置
............................................................................
7
【Pygame 第 5 课】 游戏中的运动
............................................................................
9
【Pygame 第 6 课】 面向对象的游戏设计
..............................................................
14
【Pygame 第 7 课】 多变的宿敌
..............................................................................
17
【Pygame 第 8 课】 火力全开
..................................................................................
21
【Pygame 第 9 课】 一大波飞机
..............................................................................
25
【Pygame 第 10 课】 命中目标
................................................................................
27
【Pygame 第 11 课】 GAME OVER
..............................................................................
29
【Pygame 第 12 课】 屡败屡战
................................................................................
31
【Pygame 第 1 课】 hello pygame
我们已经把 python 的基本内容讲得差不多了,所以从今天起,尝试一下新的方面:pygame -- 用
python 来写游戏。
pygame 是一个 python 的游戏库,借助它,我们可以用 python 写一些小游戏。虽然你想用它写出一
个魔兽世界那样的游戏是不大可能的,但它的确适合 python 学习者入手游戏开发。
安装 pygame
python 标准库里是没有包含 pygame 的,所以我们需要去下载安装它。去 www.pygame.org 上的
downloads 找到对应你 python 版本的安装包下载并安装。Mac 用户要注意一下,可能你 mac 里默认的
python 版本无法于 pygame 兼容,需要去 puthon.org 重新下载安装 python2.7。
安装完之后,可以在你的 python shell 里验证一下:
1. >>>import pygame
2. >>>pygame.ver
3. '1.9.1release'
pygame 的 hello world
照例,我们要用一个 hello world 程序来开始我们的学习。
在写代码之前,先去找一张图片,确定图片的长宽值。我们要用它来做为背景图片。
1. # -*- coding: utf-8 -*-
2. import pygame
3. #导入 pygame 库
4. from sys import exit
5. #向 sys 模块借一个 exit 函数用来退出程序
6. pygame.init()
7. #初始化 pygame,为使用硬件做准备