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

## 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.

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

```   1 from pygsear.Game import Game
2
3 from Ball import WhoaBall
4
5 class BallGame(Game):
6     def initialize(self):
7         ball = WhoaBall()
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:

`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.

```   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.

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)
13
14 if __name__ == '__main__':
15     g = BallGame()
16     g.mainloop()
```

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

```   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.

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)