Differences between revisions 2 and 3
Revision 2 as of 2007-01-19 02:36:45
Size: 4793
Editor: thinker
Comment:
Revision 3 as of 2009-12-25 07:14:54
Size: 4802
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-4 Functions

The interactive python interpreter is very useful if you are testing and typing just a few lines, but anything more than that and you are going to want to save your code in a file.

当你测试并键入少数几行代码时,交互式解释器是非常有用的,但是超过那些你想保存你的代码到文件中。

Saving your code in a file that ends in .py makes it a Python module. Once you have a module, your code can be used from the interpreter, or by code in other files and programs.

把你的文件用以 .py 结尾的文件名保存可以使他成为Python的组件。一旦你拥有了一个组件,你的代码就可以在解释器被用到,或者被别的文件和程序用到。

The Python code that you save to a file will look much like the code you typed directly in to the interpreter. Try it now.

你保存在文件中的Python代码,将会非常像你直接在解释器中键入的一样。现在就试试吧。

Create a new, blank file in your text editor. In that file, type these lines:

在你的文本编辑器中创建一个新的空文件,然后键入下面这些语句:

   1 from pygsear.Drawable import String
   2 
   3 def send(new_msg):
   4     m = String(message=new_msg, fontSize=80)
   5     m.center()
   6     m.udraw()

Save the file in the examples/ directory and call it message.py

把这个文件保存在 examples/ 目录下,并且起名叫 message.py

1.1. import导入

When you try to import a module, Python will look first in the current directory. It looks for a file with the name of the module plus a .py ending.

当你尝试导入一个组件时,Python将首先从你的当前目录找起。它查找文件名是组件名的文件还有以.py结尾。

Start Python in the examples/ directory, and import your new module:

在examples/目录运行Python,并且导入你的新组件:

   1 import message

To use your new module, call the send() function with a text message:

要用你的新组件,用文本信息调用send()函数:

   1 message.send('Penguin Patrol!')

But there is a problem with this. What happens if you call the function again?

但是这儿有一些问题。如果你再次调用这个函数会发生什么?

   1 message.send('Python & Pygame. Oh yea!')
   2 message.send('Take me to your leader')

1.2. global全局

One way to fix the problem of the new message writing over the previous one is to keep a handle on the old String object. That way when the function is called again, we can erase the old message and make a new one.

修复这个问题(让新消息覆盖旧消息)的一个方法是保持一个旧文本对象的句柄。用这种方法使得当函数被再次调用,我们能够擦掉旧消息并产生一个新消息。

Add the highlighted lines to your message.py so it matches this:

添加这些髙亮的行到 message.py 中以匹配这些:

   1 from pygsear.Drawable import String
   2 
   3 msg = None
   4 
   5 def send(new_msg):
   6     global msg
   7     if msg is not None:
   8         msg.uclear()
   9     m = String(message=new_msg, fontSize=80)
  10     m.center()
  11     m.udraw()
  12     msg = m

The variable msg is a module level or global variable. The first time through the function, msg will be None and we will skip the call to uclear()

变量 msg 是组件级别或者全局变量。当第一次运行函数时,msg是空的并且程序会跳过调用uclear()

On any other call, we first clear out the old message, then draw the new one and save it as msg

当其他的调用时,我们首先清除旧消息,然后画上新的并且用msg来保存它。

Using global variables, sort of like from foo import *, is usually considered bad form.

使用全局变量,类似于from foo import *,通常被看作是不好的形式。

A much better solution is to use a class which we will work on next.

一个好的多得解决方法是用一个类,这将在下一课学到。

StartProgramming-2-4 (last edited 2009-12-25 07:14:54 by localhost)