开始编程之旅 翻译自Lee Harr的Start Programming

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

StartProgramming-1-1 交互Interact

解释器Interpreter

Eventually, your program will look like other programs. It will have an icon which people click on and which makes it run.

终于,你的程序看起来和别的程序差不多了。当人们双击它的的图标时它就可以运行。

python_icon.png python_icon.png python_icon.png python_icon.png python_icon.png

For now, though, we will be working from the command line -- interactive mode.

虽然暂时我们还必须工作在命令行会话模式下面。

Note: If you are working on a system with a reasonable text terminal (BSD, Linux, etc) you may want to use that instead of interact.py for these interactive sessions. Simply open a terminal in the examples directory, type python then type from penguin import *

注意:如果你工作在正当的文本终端(BSD,Linux,etc)上,你也许希望终端可以替代interact.py来做交互式的会话。在examples目录中打开一个终端,运行python然后键入from penguin import *

After that everything should be much the same as with using interact.py except you will have better text editing capabilities.

之后所有的看起来就好像运行interact.py一样了,除非你希望有一个好的编辑器。

The interactive interpreter is a way to type in python commands and get results immediately.

这个交互的解释器是你在python中键入命令和立即获取结果的桥梁。

Look in the pygsear examples directory, and run the program called interact.py. You should get a new window with a little penguin in the middle of the screen.

进入pygsear的examples目录,并且运行interact.py这个程序。你会看到一个中间带有小企鹅的新窗口。

interact.png

The >>> in the lower left corner is called the prompt. It is the Python interpreter waiting for you to tell it what to do.

在左下角的>>>叫做提示符。它是Python的翻译器等候你要他做什么。

海龟The Turtle

We are going to start with something called "turtle" graphics. Oddly enough, our turtle will look like a penguin.

我们将以某些东西开始称为“海龟”图形。说也奇怪,我们的海龟看起来像一只企鹅。

pete.png pete.png pete.png pete.png pete.png

The penguin is called pete.

这个企鹅叫做pete。

Let's start out by having pete draw a line. Type in this line and press [ENTER]:

我们以让pete画一条线作为开始。输入下面的这行语句然后按[ENTER回车]:

pete.forward(100)

This statement is a function call or method call. It tells pete to move forward 100 pixels.

这个语句是一个函数调用或者方法调用。它通知pete前进100像素。

You can put any number you want inside the parentheses (even negative numbers), but keep in mind that the window that pete is drawing in is probably only about 600 pixels wide and 500 pixels high.

你可以用任何的数字代替圆括号内的数字(甚至是负数),但是要记住在这个窗口中pete只能前进600像素宽和500像素高。

We can also make pete turn:

我们也可以让pete转弯:

pete.right(30)

pete understands turning in degrees, so when we tell pete right(30), he turns 30 degrees to his right.

pete会记住所转过的角度,所以当我们再次通知pete right(30),它会从它的右面再转30度。

penguin_turn.png

Notice that the amount is relative to where pete is looking when you ask him to turn. That means that if you say pete.right(30) again, pete will turn a bit more to the right, and if you do pete.right(30) one more time he will have turned a total of 90 degrees and be looking over at the right side of the screen.

注意你每次调用pete转角时,每次转的角度和的比较。也就是说如果你再次调用pete.right(30),pete将会转向接近右面,当你第三次调用pete.right(30),你会发现pete总共转了90度,从屏幕上看是向右的方向了。

Reset the penguin graphics now by typing:

让企鹅重新绘图键入:

reset()

See if you can make pete go around in a square. How can you do that?

如果你想让pete画一个正方形。该怎么办?

How about like this:

可以像这样做:

First, go forward again. Notice that by using the up and down arrows on your keyboard you can retrieve lines you typed previously and save yourself some typing. So, hit the up arrow until the line looks like pete.forward(100) again and hit [ENTER]

首先,再次前进。注意可以利用向上键和向下键找回你前面键入的命令。所以,按下向上键直到找到pete.forword(100)这条命令然后按[ENTER]

Of course a square is just four equal sides, and four right angles, so make pete turn right 90 degrees:

当然,一个正方形有四条相等的边和四个直角,所以让pete右转90度:

pete.right(90)

Now the simplest way to continue is just to keep hitting up-arrow twice and [ENTER] until pete is back to where he started.

现在用简单的方法用向上键两次和[ENTER]键直到pete回到起始点。

Remember, if you make a mistake, or things get confused, don't worry. Just type:

记住,如果你犯了错误,或者弄得乱七八糟,不要着急。只要键入:

reset()

pete will go back to where he started and clear the screen in the process.

pete将回到起始点并且清除掉前面画的图形。

You can also use home() to bring pete back to where he started without changing anything drawn on the screen, or cls() to erase everything but without making pete move at all.

你还可以用home()命令让pete回到起始点而不清除任何在屏幕上的图形,或者用cls()命令清除所有的图形但是pete却不移动位置。

squares.png

StartProgramming-1-1 (last edited 2009-12-25 07:13:54 by localhost)