(首页)开始编程之旅 翻译自Lee Harr的Start Programming 本文是使用pygsear+pygame作为开发环境,以初级用户角度来分步分阶段学习PYTHON基本概念,并以小游戏开发项目为具体案例,介绍的十分详细。编写风格清新朴实,没有象一般教科书那样枯燥,极其适合初级用户来激发兴趣时使用。
Contents
1. StartProgramming-2-1 引入Import
At the top of most Python programs, you will see import statements like this:
在大部分的Python程序的开头,你都会看到 import 语句:
The import statement lets you quickly and easily load in python modules.
import 语句可以使我们快速并且简单在python中的加载组件.
Modules are useful code that is already written -- either code which someone else wrote or your own code. Before starting any programming project, it is a good idea to look around and see if there is already a module which will do what you need.
组件是已经写过的有用的代码 -- 在别人写的代码和你的代码之间。在开始一个编程项目之前,看一看有没有你需要用的已经写过的组件是一个不错的主意。
Why would you want to import a module?
你为什么想要导入一个组件?
If you need to make your game behave randomly, you might want to choose from different attack strategies:
如果你想要你的游戏表现的很随机,你可能想要选择不同的攻击策略:
Or, if your game needs to know the size of a file:
或者,假如你的游戏需要知道文件的大小:
Or, if you need to know the distance between two objects:
或者,假如你想要知道两个物体之间的距离:
This distance formula (the Pythagorean Theorem) is so useful that it is included in the math module:
这个距离运算(毕达格拉斯定理)是如此的有用,所以它已经被收录到 math 组件当中了:
You can see the values, and check if they are the same:
你能够看到变量的值,并且检查它们是否相等:
1 print distance, distance2, distance == distance2
Or, if you just want to use the penguin graphics module:
或者,假如你正想使用企鹅绘图组件: