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

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.

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

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

from pygsear.Drawable import Rectangle

def __init__(self):
Rectangle.__init__(self, width=15, height=50)
self.center(x=10)

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

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.

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.

from pygsear.Drawable import 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):
self.up_pressed = 1

def noup(self):
self.up_pressed = 0

def down(self):
self.down_pressed = 1

def nodown(self):
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.

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

def set_vel(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.set_vel()
Rectangle.move(self)

Also, we want the Paddle to be always onscreen:

def move(self):
self.set_vel()
Rectangle.move(self)
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.

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

from pygame.locals import K_UP, K_DOWN

from pygsear.Game import Game
from pygsear.Event import KEYDOWN_Event, KEYUP_Event

class Pong(Game):
def initialize(self):

if __name__ == '__main__':
game = Pong()
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

# Pong-0.1.py
from pygsear.Drawable import Rectangle
from pygsear.Game import Game
from pygsear.Event import KEYDOWN_Event, KEYUP_Event

from pygame.locals import K_UP, K_q, K_DOWN, K_a

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 set_vel(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.set_vel()
Rectangle.move(self)
self.onscreen(top=-5, bottom=-5, jail=1)

class Pong(Game):
def initialize(self):

if __name__ == '__main__':
game = Pong()
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.

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

• Page.execute = 0.094s
• getACL = 0.007s
• init = 0.001s