# This class represents each block that will get knocked out by the ball# It derives from the "Sprite" class in PygameclassBlock(pygame.sprite.Sprite):# Constructor. Pass in the color of the block, and its x and y positiondef__init__(self,color,x,y,setspeed,setscore):# Call the parent class (Sprite) constructorpygame.sprite.Sprite.__init__(self)# Class variables to hold the height and width of the blockself.width=20self.height=15self.speed=setspeedself.score=setscore# Create the image of the block of appropriate size# The width and height are sent as a list for the first parameter.self.image=pygame.Surface([self.width,self.height])# Fill the image with the appropriate colorself.image.fill(color)# Fetch the rectangle object that has the dimensions of the imageself.rect=self.image.get_rect()# Move the top left of the rectangle to x,y.# This is where our block will appear..self.rect.topleft=(x,y)

# This class represents the ball # It derives from the "Sprite" class in PygameclassBall(pygame.sprite.Sprite):# Constructor. Pass in the color of the block, and its x and y positiondef__init__(self):# Call the parent class (Sprite) constructorpygame.sprite.Sprite.__init__(self)# Class attributes for width and heightself.width=10self.height=10self.speed=.2# A list with the x and y of our ball. (This is the starting position)self.mypos=([0.0,180.0])# Direction in degrees (zero is straight up)self.direction=200# Speed in pixels per cycleself.speed=0.3# Create the image of the ballself.image=pygame.Surface([self.width,self.height])# Color the ballself.image.fill((white))# Get a rectangle object that shows where our image isself.rect=self.image.get_rect()# Get attributes for the height/width of the screenself.screenheight=pygame.display.get_surface().get_height()self.screenwidth=pygame.display.get_surface().get_width()

# This function will bounce the ball off a horizontal surface (not a verticle one)defbounce(self,diff):self.direction=(180-self.direction)%360self.direction-=diff

# Update the position of the balldefupdate(self):# Sine and Cosine work in degrees, so we have to convert themdirection_radians=math.radians(self.direction)# Change the position (x and y) according to the speed and directionself.mypos[0]+=self.speed*math.sin(direction_radians)self.mypos[1]-=self.speed*math.cos(direction_radians)# Update the rectangle of the ball so that it is at myposself.rect.left=self.mypos[0]self.rect.top=self.mypos[1]# Do we bounce off the top of the screen?ifself.mypos[1]<=0:self.bounce(0)self.mypos[1]=1# Do we bounce off the left of the screen?ifself.mypos[0]<=0:self.direction=(360-self.direction)%360self.mypos[0]=1# Do we bounce of the right side of the screen?ifself.mypos[0]>self.screenwidth-self.width:self.direction=(360-self.direction)%360self.mypos[0]=self.screenwidth-self.width-1

ifself.mypos[1]>600:returnTrueelse:returnFalse

# This class represents the bar at the bottom that the player controlsclassPlayer(pygame.sprite.Sprite):# Constructor functiondef__init__(self):# Call the parent's constructorpygame.sprite.Sprite.__init__(self)

# Update the playerdefupdate(self):# Get where the mouse ispos=pygame.mouse.get_pos()# Set the left side of the player bar to the mouse positionself.rect.left=pos[0]# Make sure we don't push the player paddle off the right side of the screenifself.rect.left>self.screenwidth-self.width:self.rect.left=self.screenwidth-self.width

# This is the main function where our program beginsdefmain():score=0# Call this function so the Pygame library can initialize itselfpygame.init()# Create an 800x600 sized screenscreen=pygame.display.set_mode([800,600])# Set the title of the windowpygame.display.set_caption('Breakout')# Enable this to make the mouse dissappear when over our window#pygame.mouse.set_visible(0)# This is a font we use to draw text on the screen (size 36)font=pygame.font.Font(None,36)

# Create a surface we can draw onbackground=pygame.Surface(screen.get_size())# Used for converting color maps and suchbackground=background.convert()# Fill the screen with a black backgroundbackground.fill(black)

# Create the player paddle objectplayer=Player()# Create the ballball=Ball()# Create a group of 1 ball (used in checking collisions)balls=pygame.sprite.Group()balls.add(ball)

# Create a group that will hold the blocks we are about to createblocks=pygame.sprite.RenderPlain()# The top of the block (y position)top=80# Number of blocks to createblockcount=40

ifnotdone:# Update the player and ball positionsplayer.update()done=ball.update()

# If we are done, print game overifdone:text=font.render("Game Over",1,(200,200,200))textpos=text.get_rect(centerx=background.get_width()/2)textpos.top=300background.blit(text,textpos)print"Game Over"

# See if the ball hits the player paddleifpygame.sprite.spritecollide(player,balls,False):# The 'diff' lets you try to bounce the ball left or right depending where on the paddle you hit itdiff=(player.rect.left+player.width/2)-(ball.rect.left+ball.width/2)# Set the ball's y position in case we hit the ball on the edge of the paddleball.rect.top=screen.get_height()-player.rect.height-ball.rect.height-1ball.bounce(diff)

# Check for collisions between the ball and the blocksdeadblocks=pygame.sprite.spritecollide(ball,blocks,False)# For every block we hit:forbindeadblocks:# Speed up the ball if we hit a block that causes ball speed upifball.speed<b.speed:ball.speed=b.speed# Add to our scorescore+=b.score# Remove the block from the list of blocksblocks.remove(b)# If we actually hit a block, bounce the ball and play a soundiflen(deadblocks)>0:block_hit_sound.play()ball.bounce(0)