To illustrate the power of the Pyglet framework and subclassing, we provide a simple implementation of Pong, the original Atari arcade game.

Two players control the up and down movement of their paddles using the letters W, S, I, and K. The P key pauses or unpauses the game. And Q quits.

As in the simple game of controlling a white square moving, in the previous section, we define a subclass GameWindow of pyglet.window.Window. It maintains a list of currently pressed keys, which is changed whenever a keypress is initiated or released.

Here we introduce a new class, Game, to keep track of all the elements of the game, including the ball, paddles, and walls, plus counters for the score, etc. The Ball, Paddles, and Walls, are all instances of the Sprite class (or, rather, of subclasses of that class). Thus, they have x,y positions within the window and know how to redisplay themselves.

Using schedule_interval, 20 times per second a method is called which advances the state of the game (e.g., moving the ball and paddles by a few pixels).

We use inheritance to make it simpler to program the ball, paddles, and walls. We define a generic GameObject class. Any instance knows how to move itself by a few pixels, based on a direction (angle) and velocity that are stored as instance variables.

Ball, BallDeflector, and Endline are three subclasses of GameObject.

The Ball class, in addition to the movement capability it inherits from GameObject, on each update checks itself against each of the other game objects (the paddles and walls) to see if it is now colliding with that object. If so, it ask that object to handle the collision.

The Endline class, in addition to what it inherits from GameObject, also handles collision with a ball. Instead of deflecting the ball back, it just resets the game, with the ball restarting in the middle of the window.

The BallDeflector class, in addition to what it inherits from GameObject, has the ability to deflect a ball, giving it a new direction (angle). The top and bottom walls are BallDeflectors.

Paddle is a subclass of BallDeflector. In addition to what it inherits from BallDeflector, it sets its direction and velocity based on what keys are currently pressed. For example, if W is pressed for the left paddle, it will set the direction to be straight up; if S is pressed, it sets the direction of movement to be straight down.

Thus, we can summarize the class/subclass relations of all the classes used:

The diagram below shows the structure of the objects that will be created when we run the program. The instance of Game maintains instance variables balls, walls, and paddles that each are lists of object instances, always an instance of the appropriate subclass of GameObject.

We provide the complete code listing below.

__author__='Sam Carton and Paul Resnick'importpygletimportrandomimportmathdebug=Truedefas_cartesian(velocity,angle):ifangleisNone:return0,0else:returnvelocity*math.cos(math.radians(angle)),velocity*math.sin(math.radians(angle))defsign(num):ifnum>=0:return1else:return-1classGameObject(pyglet.sprite.Sprite):def__init__(self,img_file=None,initial_x=0,initial_y=0,game=None):pyglet.sprite.Sprite.__init__(self,img_file,initial_x,initial_y)self.game=gameself.initial_x=initial_xself.initial_y=initial_yself.set_initial_position()defset_initial_position(self):# set_position method is inherited from Sprite classself.set_position(self.initial_x,self.initial_y)self.velocity=0.0self.angle=Nonedefmove(self):''' Move this game object one unit forward in the direction of its velocity. :return: '''x_vel,y_vel=as_cartesian(self.velocity,self.angle)self.set_position(self.x+int(x_vel),self.y+int(y_vel))defupdate(self,pressed_keys):self.move()classBallDeflector(GameObject):defdeflect_ball(self,ball,side_hit):''' Deflect a ball that has collided with this object. :param ball: '''ifside_hit=='RIGHT'orside_hit=='LEFT':ball.angle=(180-ball.angle)%360elifside_hit=='BOTTOM'orside_hit=='TOP':ball.angle=(-ball.angle)%360self.shunt(ball)defshunt(self,ball):# Shunt the ball in its new direction by enough so that it is no longer overlapping with self.# This avoids processing multiple collisions of self and ball before the ball "escapes"whileball.colliding_with(self):ball.move()if(ball.x<0)or(ball.y<0):foobarclassEndLine(BallDeflector):defdeflect_ball(self,ball,side_hit):print"hit an endline"ifside_hit=='LEFT':# ball approached from the left to right wallself.game.reset()elifside_hit=='RIGHT':# ball approached from the rightself.game.reset()else:# Shouldn't happen. Must have miscalculated which side was hit, since this is an endlineraiseException(side_hit)classBall(GameObject):default_velocity=6.0#Number of pixels the ball should move per game cycledefupdate(self,pressed_keys):self.move()ifself.in_play:forgame_objectinself.game.game_objects:side_hit=self.colliding_with(game_object)ifside_hit:game_object.deflect_ball(self,side_hit)defset_initial_position(self):self.set_position(self.initial_x,self.initial_y)self.velocity=self.default_velocityself.angle=self.generate_random_starting_angle()self.in_play=Truedefgenerate_random_starting_angle(self):''' Generate a random angle that isn't too close to straight up and down or straight side to side :return: an angle in degrees '''angle=random.randint(15,75)+90*random.randint(0,3)debug_print('Starting ball angle: '+str(angle)+' degrees')returnangledefcolliding_with(self,game_object):''' self is a ball and game_object is some other game_object. If their bounding boxes (the space they take up on screen) don't overlap, return False. If they do overlap, return one of 'LEFT', 'RIGHT', 'TOP', 'BOTTOM', indicating which edge of game_object the ball has hit. Note: this code is complicated, in part because of the geometric reasoning. You don't have to understand how this method is implemented, but you will need to understand what it does-- figure out which side of the game_object, if any, the ball collided with first. '''# x_distance is difference between rightmost object's left-side (x) and the other's right side (x+width)if(self.x<game_object.x):left,right=self,game_objectelse:left,right=game_object,selfx_distance=right.x-(left.x+left.width)# y_distance is difference between one object's bottom-side (y) and the other's top side (y + height)if(self.y<game_object.y):bottom,top=self,game_objectelse:bottom,top=game_object,selfy_distance=top.y-(bottom.y+bottom.height)if(x_distance>0)or(y_distance>0):# no overlapreturnFalseelse:# figure out which side of game_object self hit# first, special cases of horizontal or vertical approach anglespecial_cases={0:'LEFT',90:'BOTTOM',180:'RIGHT',270:'TOP'}ifself.angleinspecial_cases:returnspecial_cases[self.angle]else:# Decide base on self's y position at the point where they intersected in the x-dimension(x_vel,y_vel)=as_cartesian(self.velocity,self.angle)slope=y_vel/x_vel# go x_distance units either forward or back in x dimension; multiply by slope to get offset in y dimensiony_at_x_collision=self.y-sign(y_vel)*math.fabs(x_distance*slope)if(self.angle<90):# coming from below left, check if top of self was below game_objectify_at_x_collision+self.height<game_object.y:return'BOTTOM'else:return'LEFT'elif(self.angle<180):# coming from below right, check if top of self was below game_objectify_at_x_collision+self.height<game_object.y:return'BOTTOM'else:return'RIGHT'elifself.angle<270:# coming from above right, check if bottom of self was above game_objectify_at_x_collision>game_object.y+game_object.height:return'TOP'else:return'RIGHT'else:# coming from above right, check if bottom of self was above game_objectify_at_x_collision>game_object.y+game_object.height:return'TOP'else:return'LEFT'defdeflect_ball(self,ball,side_hit):# balls don't deflect other ballspassclassPaddle(BallDeflector):default_velocity=4.0def__init__(self,player=None,up_key=None,down_key=None,left_key=None,right_key=None,name=None,img_file=None,initial_x=0,initial_y=0,game=None):BallDeflector.__init__(self,img_file=img_file,initial_x=initial_x,initial_y=initial_y,game=game)self.player=playerself.up_key=up_keyself.down_key=down_keyself.left_key=left_keyself.right_key=right_keyself.name=namedefupdate(self,pressed_keys):self.velocity=self.default_velocityifself.up_keyinpressed_keysandnotself.down_keyinpressed_keys:self.angle=90elifself.down_keyinpressed_keysandnotself.up_keyinpressed_keys:self.angle=270elifself.left_keyinpressed_keysandnotself.right_keyinpressed_keys:self.angle=180elifself.right_keyinpressed_keysandnotself.left_keyinpressed_keys:self.angle=0else:self.velocity=0.0self.angle=Noneself.move()defhit_position(self,ball):''' Returns a number between 0 and 1, representing how far up the paddle the ball hit. If it hit near the top, the number will be close to 1. '''virtual_height=self.height+ball.heighty_dist=ball.y+ball.height-self.ypct=y_dist/float(virtual_height)returnpctclassGame(object):side_paddle_buffer=50# how far away from the side wall a paddle should startaux_paddle_buffer=550# how far away a forward paddle should startdef__init__(self,ball_img=None,paddle_imgs=None,wall_imgs=None,width=800,height=450,game_window=None,wall_width=10,paddle_width=25,brick_height=40):self.score=[0,0]self.width=widthself.height=heightself.game_window=game_windowself.hit_count=0self.balls=[Ball(img_file=ball_img,initial_x=self.width/2,initial_y=self.height/2,game=self)]self.paddles=[Paddle(player=1,up_key=pyglet.window.key.W,down_key=pyglet.window.key.S,name='Player 1',img_file=paddle_imgs[0],initial_x=self.side_paddle_buffer+paddle_width/2,initial_y=self.height/2,game=self),Paddle(player=2,up_key=pyglet.window.key.U,down_key=pyglet.window.key.J,name='Player 2',img_file=paddle_imgs[1],initial_x=self.width-self.side_paddle_buffer-paddle_width/2,initial_y=self.height/2,game=self)]self.walls=[BallDeflector(initial_x=0,#bottominitial_y=0,img_file=wall_imgs[1],game=self),BallDeflector(initial_x=0,#topinitial_y=self.height-wall_width,img_file=wall_imgs[1],game=self),EndLine(initial_x=0,#leftinitial_y=0,img_file=wall_imgs[0],game=self),EndLine(initial_x=self.width-wall_width,#rightinitial_y=0,img_file=wall_imgs[0],game=self),]self.bricks=[]# Not used in this initial versionself.game_objects=self.walls+self.bricks+self.paddles+self.ballsdefupdate(self,pressed_keys):''' Update the game based on the current state of its game objects and the set of keys currently being pressed :param pressed_keys: a set() object containing an int representing each key currently being pressed The matching between numbers and keys is defined by Pyglet. For example, pyglet.window.key.W is equal to 119 :return: '''# debug_print('Updating game state with currently pressed keys : ' + str(pressed_keys))forgame_objectinself.game_objects:game_object.update(pressed_keys)defreset(self,pause=True):# self.score = [0,0]forgame_objectinself.game_objects:game_object.set_initial_position()self.hit_count=0debug_print('Game reset')self.game_window.redraw()ifpause:debug_print('Pausing. Hit P to unpause')self.game_window.pause()defdraw(self):forgame_objectinself.game_objects:game_object.draw()defincrement_hit_count(self):# this method will be used in an exercise in discussion sectionself.hit_count+=1classGameWindow(pyglet.window.Window):def__init__(self,ball_img,paddle_imgs,wall_imgs,width=800,height=450,*args,**kwargs):pyglet.window.Window.__init__(self,width=width,height=height,*args,**kwargs)self.paused=Falseself.game=Game(ball_img,paddle_imgs,wall_imgs,width,height,self)self.currently_pressed_keys=set()#At any given moment, this holds the keys that are currently being pressed. This gets passed to Game.update() to help it decide how to move its various game objectsself.score_label=pyglet.text.Label('Score: 0 - 0',font_name='Times New Roman',font_size=14,x=width-75,y=height-25,anchor_x='center',anchor_y='center')# Decide how often we want to update the game, which involves# first telling the game object to update itself and all its objects# and then rendering the updated game usingself.fps=20#Number of frames per seconds#This tells Pyglet to call Window.update() once every fps-th of a secondpyglet.clock.schedule_interval(self.update,1.0/self.fps)pyglet.clock.set_fps_limit(self.fps)defon_key_press(self,symbol,modifiers):''' This is an overwrite of pyglet.window.Window.on_key_press() This gets called by the pyglet engine whenever a key is pressed. Whenever that happens, we want to add each key being pressed to the set of currently-pressed keys if it isn't already in there That's if the key pressed isn't 'Q' or 'Esc'. If it is, then just quit. :param symbol: a single key identified as an int :param modifiers: I don't know what this is. I am ignoring this. :return: '''ifsymbol==pyglet.window.key.Qorsymbol==pyglet.window.key.ESCAPE:debug_print('Exit key detected. Exiting game...')pyglet.app.exit()elifsymbol==pyglet.window.key.R:debug_print('Resetting...')self.game.reset()elifsymbol==pyglet.window.key.P:ifnotself.paused:self.pause()else:self.unpause()elifnotsymbolinself.currently_pressed_keys:self.currently_pressed_keys.add(symbol)defpause(self):debug_print('Pausing')pyglet.clock.unschedule(self.update)self.paused=Truedefunpause(self):debug_print('Unpausing')pyglet.clock.schedule_interval(self.update,1.0/self.fps)self.paused=Falsedefon_key_release(self,symbol,modifiers):ifsymbolinself.currently_pressed_keys:self.currently_pressed_keys.remove(symbol)defupdate(self,*args,**kwargs):self.game.update(self.currently_pressed_keys)self.redraw()defredraw(self):self.clear()self.game.draw()self.score_label.draw()defredraw_label(self):self.score_label.text='Score: '+str(self.game.score[0])+' - '+str(self.game.score[1])defdebug_print(string):''' A little convenience function that prints the string if the global debug variable is True, and otherwise does nothing :param string: :return: '''ifdebug:printstringdefmain():debug_print("Initializing window...")ball_img=pyglet.resource.image('ball.png')# ball_img = pyglet.resource.image('vertical_wall.png')paddle_imgs=[pyglet.resource.image('paddle1.png'),pyglet.resource.image('paddle2.png')]wall_imgs=[pyglet.resource.image('vertical_wall.png'),pyglet.resource.image('horizontal_wall.png'),pyglet.resource.image('brick.png')]window=GameWindow(ball_img,paddle_imgs,wall_imgs)debug_print("Done initializing window! Initializing app...")pyglet.app.run()if__name__=="__main__":main()