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

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

1. StartProgramming-3-2 Demo

We got WhoaBall working pretty well, but it is still a hassle having to go in to the interpreter, import your module, make a ball, and run its path.

我们使得 WhoaBall 工作的非常好,但是它仍然有打扰解释器,导入你的组建,生产一个球,并且运行它的路径。

Let's use the pygsear.Game module to make this a bit easier:

让我们利用 pygsear.Game 组件使这件事很简单:

   1 from pygsear.Game import Game
   2 
   3 from Ball import WhoaBall
   4 
   5 class BallGame(Game):
   6     def initialize(self):
   7         ball = WhoaBall()
   8         self.sprites.add(ball)
   9 
  10 if __name__ == '__main__':
  11     g = BallGame()
  12     g.mainloop()

Here we inherit from pygsear.Game.Game to get more functionality. Save this as BallGame.py and run the program:

在这儿我们继承了pygsear.Game.Game并获得了更多的功能。以文件名BallGame.py保存这些并且运行这个程序:

python BallGame.py

Your code to set the game up goes in the initialize method.

你的程序用预置的方法使得程序运行起来。

The odd looking section at the bottom is an idiom which says "if this program is being run (and not just imported) do this:" It creates an instance of your game and starts the main loop.

在下面的奇怪部分是一种习语,它的意思是“如果这个程序开始运行了(不是被导入的)运行这些:”它创建了游戏的一个实例并且开始主循环。

Now let's make a few changes to WhoaBall so that we can improve BallGame too.

现在让我们为WhoaBall做一点小改动以便我们也能够改进BallGame

   1 class WhoaBall(Circle):
   2     def __init__(self, v=150):
   3         Circle.__init__(self, color='random')
   4         self.path.set_velocity(vx=v)
   5         self.path.set_gravity(gy=400)

I only show the method which needs to be changed.

我只展示了需要被改变的方法。

This makes each WhoaBall a random color, and makes WhoaBall take a parameter to set the initial horizontal velocity.

这样使得每个WhoaBall有随机的颜色,并且使WhoaBall有一个设置开始水平速度的参数。

Now your game can make a few balls each with different velocities:

现在你的游戏能够生产少数的每个都有不同速度的球:

   1 import random
   2 
   3 from pygsear.Game import Game
   4 
   5 from Ball import WhoaBall
   6 
   7 class BallGame(Game):
   8     def initialize(self):
   9         for b in range(5):
  10             vx = random.uniform(100, 200)
  11             ball = WhoaBall(vx)
  12             self.sprites.add(ball)
  13 
  14 if __name__ == '__main__':
  15     g = BallGame()
  16     g.mainloop()

One last change to WhoaBall and this will make a nice demo:

这是WhoaBall的最后一次改动,并且这次将有一个很好的演示:

   1 class WhoaBall(Circle):
   2     def bounce(self):
   3         if not self.onscreen(bottom=0, jail=1):
   4             self.path.vy = -self.path.vy * 0.85
   5             if abs(self.path.vy) <= 20:
   6                 self.path.set_velocity(vy=-800)
   7                 print 'FLING!'

Again, I only show the method which needs to be changed.

我再次只显示了那些需要改动的方法。

This does two things:

这次做了两件事:

First, on each bounce the velocity is cut to 85% of the previous bounce, to make the bounces decay more quickly.

首先,每次弹起的高度切到前一次的85%,使弹起更快的衰竭

Second, when the bounce goes below a certain height, the ball is re-energized and flinged back in to the air.

第二,当弹起低于某个高度,球被重新激起并冲回天上去。

Here are the final versions of the two files. You can download the files by clicking on the link:

这是这两个文件的最后版本。你可以点击连接下载这些文件:

   1 # Ball.py
   2 from pygsear.Drawable import Circle
   3 
   4 class WhoaBall(Circle):
   5     def __init__(self, v=150):
   6         Circle.__init__(self, color='random')
   7         self.path.set_velocity(vx=v)
   8         self.path.set_gravity(gy=400)
   9 
  10     def walls(self):
  11         if not self.onscreen(left=0, right=0, jail=1):
  12             self.path.vx = -self.path.vx
  13 
  14     def bounce(self):
  15         if not self.onscreen(bottom=0, jail=1):
  16             self.path.vy = -self.path.vy * 0.85
  17             print 'boing!'
  18             if abs(self.path.vy) <= 20:
  19                 self.set_color('random')
  20                 self.path.set_velocity(vy=-800)
  21                 print 'FLING!'
  22 
  23     def move(self):
  24         self.bounce()
  25         self.walls()
  26         Circle.move(self)

   1 # BallGame.py
   2 import random
   3 
   4 from pygsear.Game import Game
   5 
   6 from Ball import WhoaBall
   7 
   8 class BallGame(Game):
   9     def initialize(self):
  10         for b in range(5):
  11             vx = random.uniform(100, 200)
  12             ball = WhoaBall(vx)
  13             self.sprites.add(ball)
  14 
  15 if __name__ == '__main__':
  16         g = BallGame()
  17         g.mainloop()