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

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

1. StartProgramming-3-1 Ball

All of this abstract knowledge about classes is fine, but what we need is to see something real.

所有的关于类的抽象认识是很好,但是我们需要看到一些真实的东西。

The pygsear modules provide classes which you can use as-is, or which you can subclass to make your own game objects.

pygsear组件提供类让我们可以直接来用,或者你可以用作子类来构造你自己的游戏对象。

Let's go back to the ball, but this time subclass pygsear.Drawable.Circle

让我们回到 ball,但是这次用基类 pygsear.Drawable.Circle

1.1. class WhoaBall

   1 from pygsear.Drawable import Circle
   2 
   3 class WhoaBall(Circle):
   4     def bounce(self):
   5         print 'boing!'

You can put WhoaBall in to Ball.py or start a new file. It is up to you. Just so long as you can find and import your new class:

你可以把 WhoaBall 放到 Ball.py 里或者新建一个文件。它是你的更新版。仅仅只要找到并且导入你的新类中。

   1 import Ball
   2 b = Ball.WhoaBall()

I kept the old bounce() method in there, just so you can see that this is still your ball, but you can also see that by inheriting from Circle we gain a lot of new functions.

我在这里保持了旧的 bounce() 方法,所以这里看起来仍然是 ball 的方法,但是你能够同样的看到那些我们继承 Circle 的许多新方法。

For one thing, when you instantiated your WhoaBall another window should have opened up. If it did (and you did not get an error) you should now (finally) be able to see your ball:

首先,当你实例化你的 WhoaBall 时,其他的窗口将被打开。如果它实现了(并且你没有得到错误)你现在将(终于)能够看到你的球了:

   1 b.udraw()

Remember that if you want to see the methods available for an object, you can use:

记得如果你想看到对象可用的方法时,你可以用:

   1 dir(b)

1.2. a better bounce一个更好弹起

Now that we can actually see the ball, having bounce() just print out to the console is not so interesting. Let's see if we can make it more useful.

现在我们能够实际看到球了,只有 bounce() 打印到控制台不是那么有趣的。让我们看看我们还能够做些什么有用的。

How about if we have the ball change direction if it tries to move off of the bottom of the screen? Let's do that:

怎样做才能当球它尝试离开屏幕的底部时,让我们改变 ball 的方向?让我们这样做:

   1 from pygsear.Drawable import Circle
   2 
   3 class WhoaBall(Circle):
   4     def bounce(self):
   5         if not self.onscreen(bottom=0, jail=1):
   6             self.path.vy = -self.path.vy
   7             print 'boing!'
   8 
   9     def move(self):
  10         self.bounce()
  11         Circle.move(self)

Circle already has a lot of built-in logic for how to move around on screen, which we want to leverage. So, when we want the ball to move, we need to call Circle.move() after we extend the method by calling bounce() first.

Circle 中已经拥有很多我们希望用的内置的逻辑来在屏幕上到处跑。所以当我们希望移动 ball 时,我们需要首先在调用 bounce() 之前调用 Circle.move() 来扩展这个方法。

This onscreen() call is also inherited from Circle and it tells you if the ball has moved off of the bottom of the screen. The jail parameter tells it to move the ball back onscreen if it has moved off.

这个 onscreen() 调用也是从 Circle 继承来的并且它告诉你如果 ball 已经离开屏幕的下方。这个 jail 参数告诉它如果 ball 离开的话就移动它回到屏幕上。

Ok, let's see: 好了,让我们看看吧:

   1 reload(Ball)
   2 b = Ball.WhoaBall()
   3 b.path.set_gravity(gy=400)
   4 b.runPath()

boing! boing! boing!

跳起!跳起!跳起!

(Press ctrl-c to stop the bouncing.)

(请按 ctrl-c 键来停止弹跳。)

1.3. initialize初始化

Now move those steps that set up the WhoaBall in to an __init__ method:

现在利用这些步骤在 __init__ 方法里设置 WhoaBall

   1 from pygsear.Drawable import Circle
   2 
   3 class WhoaBall(Circle):
   4     def __init__(self):
   5         Circle.__init__(self)
   6         self.path.set_gravity(gy=400)
   7 
   8     def bounce(self):
   9         if not self.onscreen(bottom=0, jail=1):
  10             self.path.vy = -self.path.vy
  11             print 'boing!'
  12 
  13     def move(self):
  14         self.bounce()
  15         Circle.move(self)

That makes it very easy to test your ball:

这个使得测试你的 ball 非常简单:

   1 reload(Ball)
   2 b = Ball.WhoaBall()
   3 b.runPath()

Now let's test for going off the sides of the screen so we can give the ball some sideways motion:

现在让我们测试移出屏幕的边,所以我们要给这个 ball 一些向一边方向的动作:

   1 from pygsear.Drawable import Circle
   2 
   3 class WhoaBall(Circle):
   4     def __init__(self):
   5         Circle.__init__(self)
   6         self.path.set_velocity(vx=150)
   7         self.path.set_gravity(gy=400)
   8 
   9     def walls(self):
  10         if not self.onscreen(left=0, right=0, jail=1):
  11             self.path.vx = -self.path.vx
  12 
  13     def bounce(self):
  14         if not self.onscreen(bottom=0, jail=1):
  15             self.path.vy = -self.path.vy
  16             print 'boing!'
  17 
  18     def move(self):
  19         self.bounce()
  20         self.walls()
  21         Circle.move(self)

Here is a shorcut method to see your updated ball:

这里是一个看到你更新的 ball 的快捷方法:

   1 reload(Ball)
   2 Ball.WhoaBall().runPath()