(首页)开始编程之旅 翻译自Lee Harr的Start Programming
本文是使用pygsear+pygame作为开发环境,以初级用户角度来分步分阶段学习PYTHON基本概念,并以小游戏开发项目为具体案例,介绍的十分详细。编写风格清新朴实,没有象一般教科书那样枯燥,极其适合初级用户来激发兴趣时使用。
Contents
1. StartProgramming-4-2 Ball
We have experience with a ball from the WhoaBall class.
我们已经从 WhoaBall 获得了一个球的经验。
We can draw on that experience, but I am not going to inherit from WhoaBall for the Pong ball. It is just too different.
我们可以从那个经验开始画起,但是我不打算从 WhoaBall 继承到 Pong ball ,这有太多的差异。
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.
现在我们可以把新 Ball 添加到我们的 Pong 游戏中并且看看发生了什么。
Here is our progress so far. We have the paddle and the ball:
这是我们目前的进程。我们有了 paddle 和 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
9 class Paddle(Rectangle):
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):
63 paddle = Paddle()
64 self.sprites.add(paddle)
65 ball = Ball()
66 self.sprites.add(ball)
67
68 self.events.add(KEYDOWN_Event(key=K_UP, callback=paddle.up))
69 self.events.add(KEYUP_Event(key=K_UP, callback=paddle.noup))
70
71 self.events.add(KEYDOWN_Event(key=K_DOWN, callback=paddle.down))
72 self.events.add(KEYUP_Event(key=K_DOWN, callback=paddle.nodown))
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!
唯一的问题是 ball 正常穿越了 paddle !
To fix this we need to introduce collisions.
要修复这个问题我们需要介绍碰撞。