##language:zh #pragma section-numbers on ''' [[StartProgramming| (首页)开始编程之旅]] 翻译自Lee Harr的[[http://staff.easthighschool.net/lee/computers/book/Start_Programming.html|Start Programming]] 本文是使用[[http://www.nongnu.org/pygsear/|pygsear]]+[[http://pygame.org|pygame]]作为开发环境,以初级用户角度来分步分阶段学习[[http://www.python.org|PYTHON]]基本概念,并以小游戏开发项目为具体案例,介绍的十分详细。编写风格清新朴实,没有象一般教科书那样枯燥,极其适合初级用户来激发兴趣时使用。 ''' <> == 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 ,这有太多的差异。''' {{{#!python from pygsear.Drawable import Square class Ball(Square): def __init__(self): Square.__init__(self, size=15) self.center() self.path.set_velocity(vx=150, vy=100) def walls(self): vx, vy = self.path.get_velocity() if not self.onscreen(top=-5, bottom=-5, jail=1) self.path.set_velocity(vy=-vy) if not self.onscreen(right=-5, jail=1) self.path.set_velocity(vx=-vx) def move(self): self.walls() Square.move(self) }}} Now we can add the new Ball in to our Pong game and see what happens. '''现在我们可以把新 Ball 添加到我们的 Pong 游戏中并且看看发生了什么。''' {{{#!python class Pong(Game): def initialize(self): paddle = Paddle() self.sprites.add(paddle) ball = Ball() self.sprites.add(ball) }}} Here is our progress so far. We have the paddle and the ball: '''这是我们目前的进程。我们有了 paddle 和 ball:''' {{{#!python # Pong-0.2.py from pygsear.Drawable import Rectangle, Square from pygsear.Game import Game from pygsear.Event import KEYDOWN_Event, KEYUP_Event from pygame.locals import K_UP, K_DOWN class Paddle(Rectangle): def __init__(self): Rectangle.__init__(self, width=15, height=50) self.center(x=10) self.up_pressed = 0 self.down_pressed = 0 def up(self, ev): self.up_pressed = 1 def noup(self, ev): self.up_pressed = 0 def down(self, ev): self.down_pressed = 1 def nodown(self, ev): self.down_pressed = 0 def setVel(self): if self.up_pressed and not self.down_pressed: self.path.set_velocity(vy=-100) elif self.down_pressed and not self.up_pressed: self.path.set_velocity(vy=100) else: self.path.set_velocity(vy=0) def move(self): self.setVel() Rectangle.move(self) self.onscreen(top=-5, bottom=-5, jail=1) class Ball(Square): def __init__(self): Square.__init__(self, side=15) self.center() self.path.set_velocity(vx=150, vy=100) def walls(self): vx, vy = self.path.get_velocity() if not self.onscreen(top=-5, bottom=-5, jail=1): self.path.set_velocity(vy=-vy) if not self.onscreen(right=-5, jail=1): self.path.set_velocity(vx=-vx) def move(self): self.walls() Square.move(self) class Pong(Game): def initialize(self): paddle = Paddle() self.sprites.add(paddle) ball = Ball() self.sprites.add(ball) self.events.add(KEYDOWN_Event(key=K_UP, callback=paddle.up)) self.events.add(KEYUP_Event(key=K_UP, callback=paddle.noup)) self.events.add(KEYDOWN_Event(key=K_DOWN, callback=paddle.down)) self.events.add(KEYUP_Event(key=K_DOWN, callback=paddle.nodown)) if __name__ == '__main__': game = Pong() 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. '''要修复这个问题我们需要介绍碰撞。'''