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

## 1. StartProgramming-4-2 Ball

We have experience with a ball from the WhoaBall class.

We can draw on that experience, but I am not going to inherit from WhoaBall for the Pong ball. It is just too different.

```   1 from pygsear.Drawable import Square
2
3 class Ball(Square):
4     def __init__(self):
5         Square.__init__(self, size=15)
6         self.center()
7         self.path.set_velocity(vx=150, vy=100)
8
9     def walls(self):
10         vx, vy = self.path.get_velocity()
11         if not self.onscreen(top=-5, bottom=-5, jail=1)
12             self.path.set_velocity(vy=-vy)
13         if not self.onscreen(right=-5, jail=1)
14             self.path.set_velocity(vx=-vx)
15
16     def move(self):
17         self.walls()
18         Square.move(self)
```

Now we can add the new Ball in to our Pong game and see what happens.

```   1 class Pong(Game):
2     def initialize(self):
5         ball = Ball()
```

Here is our progress so far. We have the paddle and the ball:

```   1 # Pong-0.2.py
2 from pygsear.Drawable import Rectangle, Square
3 from pygsear.Game import Game
4 from pygsear.Event import KEYDOWN_Event, KEYUP_Event
5
6 from pygame.locals import K_UP, K_DOWN
7
8
10     def __init__(self):
11         Rectangle.__init__(self, width=15, height=50)
12         self.center(x=10)
13
14         self.up_pressed = 0
15         self.down_pressed = 0
16
17     def up(self, ev):
18         self.up_pressed = 1
19
20     def noup(self, ev):
21         self.up_pressed = 0
22
23     def down(self, ev):
24         self.down_pressed = 1
25
26     def nodown(self, ev):
27         self.down_pressed = 0
28
29     def setVel(self):
30         if self.up_pressed and not self.down_pressed:
31             self.path.set_velocity(vy=-100)
32         elif self.down_pressed and not self.up_pressed:
33             self.path.set_velocity(vy=100)
34         else:
35             self.path.set_velocity(vy=0)
36
37     def move(self):
38         self.setVel()
39         Rectangle.move(self)
40         self.onscreen(top=-5, bottom=-5, jail=1)
41
42
43 class Ball(Square):
44     def __init__(self):
45         Square.__init__(self, side=15)
46         self.center()
47         self.path.set_velocity(vx=150, vy=100)
48
49     def walls(self):
50         vx, vy = self.path.get_velocity()
51         if not self.onscreen(top=-5, bottom=-5, jail=1):
52             self.path.set_velocity(vy=-vy)
53         if not self.onscreen(right=-5, jail=1):
54             self.path.set_velocity(vx=-vx)
55
56     def move(self):
57         self.walls()
58         Square.move(self)
59
60
61 class Pong(Game):
62     def initialize(self):
65         ball = Ball()
67
70
73
74
75 if __name__ == '__main__':
76     game = Pong()
77     game.mainloop()
```

You can run the new game with:

`python Pong-0.2.py`

The only problem is that the ball passes right through the paddle!

To fix this we need to introduce collisions.

StartProgramming-4-2 (last edited 2009-12-25 07:09:43 by localhost)