Differences between revisions 1 and 2
Revision 1 as of 2006-12-29 02:59:23
Size: 3107
Editor: thinker
Comment:
Revision 2 as of 2009-12-25 07:14:53
Size: 3116
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 4: Line 4:
[:StartProgramming: (首页)开始编程之旅]
翻译自Lee Harr的[http://staff.easthighschool.net/lee/computers/book/Start_Programming.html Start Programming]
[[StartProgramming| (首页)开始编程之旅]]
翻译自Lee Harr的[[http://staff.easthighschool.net/lee/computers/book/Start_Programming.html|Start Programming]]
Line 7: Line 7:
本文是使用[http://www.nongnu.org/pygsear/ pygsear]+[http://pygame.org pygame]作为开发环境,以初级用户角度来分步分阶段学习[http://www.python.org PYTHON]基本概念,并以小游戏开发项目为具体案例,介绍的十分详细。编写风格清新朴实,没有象一般教科书那样枯燥,极其适合初级用户来激发兴趣时使用。 本文是使用[[http://www.nongnu.org/pygsear/|pygsear]]+[[http://pygame.org|pygame]]作为开发环境,以初级用户角度来分步分阶段学习[[http://www.python.org|PYTHON]]基本概念,并以小游戏开发项目为具体案例,介绍的十分详细。编写风格清新朴实,没有象一般教科书那样枯燥,极其适合初级用户来激发兴趣时使用。
Line 9: Line 9:
[[TableOfContents]] <<TableOfContents>>

(首页)开始编程之旅 翻译自Lee Harr的Start Programming

本文是使用pygsear+pygame作为开发环境,以初级用户角度来分步分阶段学习PYTHON基本概念,并以小游戏开发项目为具体案例,介绍的十分详细。编写风格清新朴实,没有象一般教科书那样枯燥,极其适合初级用户来激发兴趣时使用。

1. StartProgramming-2-1 引入Import

At the top of most Python programs, you will see import statements like this:

在大部分的Python程序的开头,你都会看到 import 语句:

   1 import random
   2 import os.path
   3 from math import sqrt
   4 from penguin 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:

如果你想要你的游戏表现的很随机,你可能想要选择不同的攻击策略:

   1 import random
   2 strategies = ['aggressive', 'cautious', 'defensive']
   3 strategy = random.choice(strategies)

Or, if your game needs to know the size of a file:

或者,假如你的游戏需要知道文件的大小:

   1 import os.path
   2 os.path.getsize('.')

Or, if you need to know the distance between two objects:

或者,假如你想要知道两个物体之间的距离:

   1 from math import sqrt
   2 x0, y0 = (150, 75)
   3 x1, y1 = (275, 300)
   4 distance = sqrt((x0 - x1)**2 + (y0 - y1)**2)

This distance formula (the Pythagorean Theorem) is so useful that it is included in the math module:

这个距离运算(毕达格拉斯定理)是如此的有用,所以它已经被收录到 math 组件当中了:

   1 import math
   2 distance2 = math.hypot((x0 - x1), (y0 - y1))

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:

或者,假如你正想使用企鹅绘图组件:

   1 from penguin import *
   2 pete.star() 

StartProgramming-2-1 (last edited 2009-12-25 07:14:53 by localhost)