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

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

1. StartProgramming-4-1 Paddle

We got a nice demo from WhoaBall and BallGame, but that is not really a game. Let's keep going and see if we can make this in to a real game.

我们从WhoaBallBallGame获得了一个很好的演示,但是那并不是一个真正的游戏。让我们继续下去并且看看我们能否把这些变成一个真正的游戏。

Pong was the first real video game. Let's do that.

Pong 是第一个真正的视频游戏。让我们开始做吧。

First, a paddle.

首先,一个paddle

   1 from pygsear.Drawable import Rectangle
   2 
   3 class Paddle(Rectangle):
   4     def __init__(self):
   5         Rectangle.__init__(self, width=15, height=50)
   6         self.center(x=10)

You can put this and all of the pong code in to one file Pong.py

你可以把这些代码和所有pong的代码写入一个文件Pong.py中

Your Paddle will need some control from the player.

你的Paddle将需要一些控制提供给玩家。

To convert input from the keyboard in to motion of the paddle, we will use events. Events let you create functions which will be called whenever something external happens. We will use the up and down arrow keys to control to movement of the paddle.

要转换输入从键盘到paddle的动作,我们将用事件。事件使你能在当外部活动发生时创建函数。我们将使用向上键和向下键来控制paddle的行动。

In the Paddle we will set up the functions that need to be called, and a couple of flags which will remember the state of the controls.

在Paddle中我们将设置需要调用的函数,和一对能够记住控制状态的标志。

   1 from pygsear.Drawable import Rectangle
   2 
   3 class Paddle(Rectangle):
   4     def __init__(self):
   5         Rectangle.__init__(self, width=15, height=50)
   6         self.center(x=10)
   7 
   8         self.up_pressed = 0
   9         self.down_pressed = 0
  10 
  11     def up(self):
  12         self.up_pressed = 1
  13 
  14     def noup(self):
  15         self.up_pressed = 0
  16 
  17     def down(self):
  18         self.down_pressed = 1
  19 
  20     def nodown(self):
  21         self.down_pressed = 0

We are going through a bit of a roundabout here because we want to do the right thing when the player pushes or holds down both the up and down controls at the same time. Instead of actually setting the velocity of the paddle each time a control is pressed, we change the state of a flag which tells which controls are being pressed.

我们现在做的有些绕弯,因为我们希望当玩家推或者同时压住向上和向下的控制键时做正确的事情。每次控制键按下代替实际设置paddle速度的,当控制键按下时我们改变标志的状态。

We will change the velocity in the move() method.

我们将会在方法move()中改变速度。

   1 class Paddle(Rectangle):
   2     def set_vel(self):
   3         if self.up_pressed and not self.down_pressed:
   4             self.path.set_velocity(vy=-100)
   5         elif self.down_pressed and not self.up_pressed:
   6             self.path.set_velocity(vy=100)
   7         else:
   8             self.path.set_velocity(vy=0)
   9 
  10     def move(self):
  11         self.set_vel()
  12         Rectangle.move(self)

Also, we want the Paddle to be always onscreen:

同样的,我们希望Paddle总是在屏幕上:

   1 class Paddle(Rectangle):
   2     def move(self):
   3         self.set_vel()
   4         Rectangle.move(self)
   5         self.onscreen(top=-5, bottom=-5, jail=1)

This change restricts the motion of the paddle between 5 pixels from the top edge of the screen and 5 pixels from the bottom edge of the screen.

这次改变限制paddle的行动在距离5像素的上边缘和距离5像素的下边缘的屏幕中。

Now let's put the Paddle in to a Game and see what happens. Put this right in the same file:

现在让我们把Paddle做成一个游戏并且看看会发生什么。把这些同样放入同一个文件中:

   1 from pygame.locals import K_UP, K_DOWN
   2 
   3 from pygsear.Game import Game
   4 from pygsear.Event import KEYDOWN_Event, KEYUP_Event
   5 
   6 class Pong(Game):
   7     def initialize(self):
   8         paddle = Paddle()
   9         self.sprites.add(paddle)
  10 
  11         self.events.add(KEYDOWN_Event(key=K_UP, callback=paddle.up))
  12         self.events.add(KEYUP_Event(key=K_UP, callback=paddle.noup))
  13 
  14         self.events.add(KEYDOWN_Event(key=K_DOWN, callback=paddle.down))
  15         self.events.add(KEYUP_Event(key=K_DOWN, callback=paddle.nodown))
  16 
  17 if __name__ == '__main__':
  18     game = Pong()
  19     game.mainloop()

So, here is what we have so far. You can download the code by clicking on the link, and run it with python Pong-0.1.py

这样,这些是我们目前所有的。你可以点击链接下载这些代码,并且用python Pong-0.1.py运行它。

   1 # Pong-0.1.py
   2 from pygsear.Drawable import Rectangle
   3 from pygsear.Game import Game
   4 from pygsear.Event import KEYDOWN_Event, KEYUP_Event
   5 
   6 from pygame.locals import K_UP, K_q, K_DOWN, K_a
   7 
   8 class Paddle(Rectangle):
   9     def __init__(self):
  10         Rectangle.__init__(self, width=15, height=50)
  11         self.center(x=10)
  12 
  13         self.up_pressed = 0
  14         self.down_pressed = 0
  15 
  16     def up(self, ev):
  17         self.up_pressed = 1
  18 
  19     def noup(self, ev):
  20         self.up_pressed = 0
  21 
  22     def down(self, ev):
  23         self.down_pressed = 1
  24 
  25     def nodown(self, ev):
  26         self.down_pressed = 0
  27 
  28     def set_vel(self):
  29         if self.up_pressed and not self.down_pressed:
  30             self.path.set_velocity(vy=-100)
  31         elif self.down_pressed and not self.up_pressed:
  32             self.path.set_velocity(vy=100)
  33         else:
  34             self.path.set_velocity(vy=0)
  35 
  36     def move(self):
  37         self.set_vel()
  38         Rectangle.move(self)
  39         self.onscreen(top=-5, bottom=-5, jail=1)
  40 
  41 
  42 class Pong(Game):
  43     def initialize(self):
  44         paddle = Paddle()
  45         self.sprites.add(paddle)
  46 
  47         self.events.add(KEYDOWN_Event(key=(K_UP, K_q), callback=paddle.up))
  48         self.events.add(KEYUP_Event(key=(K_UP, K_q), callback=paddle.noup))
  49 
  50         self.events.add(KEYDOWN_Event(key=(K_DOWN, K_a), callback=paddle.down))
  51         self.events.add(KEYUP_Event(key=(K_DOWN, K_a), callback=paddle.nodown))
  52 
  53 if __name__ == '__main__':
  54     game = Pong()
  55     game.mainloop()

Notice that in this version, you can also control the paddle with the 'Q' and 'A' keys, in addition to the up and down arrows.

注意在这个版本中,你仍然可以用'Q'和'A'键控制paddle,此外还有向上方向键和向下方向键。