#NEWLINE# def main( ) : #NEWLINE# #INDENT# pygame . init ( ) #NEWLINE# screen = pygame . display . set_mode ( ( width , height ) ) #NEWLINE# clock = pygame . time . Clock ( ) #NEWLINE# running = True #NEWLINE# font = pygame . font . SysFont ( 'Arial' , 16 ) #NEWLINE# space = pymunk . Space ( ) #NEWLINE# static_lines = [ pymunk . Segment ( space . static_body , ( 50 , 50 ) , ( 50 , 550 ) , 5 ) , pymunk . Segment ( space . static_body , ( 50 , 550 ) , ( 550 , 550 ) , 5 ) , pymunk . Segment ( space . static_body , ( 550 , 550 ) , ( 550 , 50 ) , 5 ) ] #NEWLINE# for line in static_lines : #NEWLINE# #INDENT# line . color = THECOLORS [ 'lightgray' ] #NEWLINE# line . elasticity = 1.0 #NEWLINE# #UNINDENT# space . add ( static_lines ) #NEWLINE# bottom = pymunk . Segment ( space . static_body , ( 50 , 50 ) , ( 550 , 50 ) , 5 ) #NEWLINE# bottom . sensor = True #NEWLINE# bottom . collision_type = 1 #NEWLINE# bottom . color = THECOLORS [ 'red' ] #NEWLINE# def remove_first( space , arbiter ) : #NEWLINE# #INDENT# first_shape = arbiter . shapes [ 0 ] #NEWLINE# space . add_post_step_callback ( space . remove , first_shape , first_shape . body ) #NEWLINE# return True #NEWLINE# #UNINDENT# space . add_collision_handler ( 0 , 1 , begin = remove_first ) #NEWLINE# space . add ( bottom ) #NEWLINE# player_body = pymunk . Body ( 500 , pymunk . inf ) #NEWLINE# player_shape = pymunk . Circle ( player_body , 35 ) #NEWLINE# player_shape . color = THECOLORS [ 'red' ] #NEWLINE# player_shape . elasticity = 1.0 #NEWLINE# player_body . position = ( 300 , 100 ) #NEWLINE# move_joint = pymunk . GrooveJoint ( space . static_body , player_body , ( 100 , 100 ) , ( 500 , 100 ) , ( 0 , 0 ) ) #NEWLINE# space . add ( player_body , player_shape , move_joint ) #NEWLINE# global state #NEWLINE# setup_level ( space , player_body ) #NEWLINE# while running : #NEWLINE# #INDENT# for event in pygame . event . get ( ) : #NEWLINE# #INDENT# if ( event . type == QUIT ) : #NEWLINE# #INDENT# running = False #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( event . type == KEYDOWN ) and ( event . key in [ K_ESCAPE , K_q ] ) ) : #NEWLINE# #INDENT# running = False #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( event . type == KEYDOWN ) and ( event . key == K_p ) ) : #NEWLINE# #INDENT# pygame . image . save ( screen , 'breakout.png' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( event . type == KEYDOWN ) and ( event . key == K_LEFT ) ) : #NEWLINE# #INDENT# player_body . velocity = ( ( - 600 ) , 0 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( event . type == KEYUP ) and ( event . key == K_LEFT ) ) : #NEWLINE# #INDENT# player_body . velocity = ( 0 , 0 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( event . type == KEYDOWN ) and ( event . key == K_RIGHT ) ) : #NEWLINE# #INDENT# player_body . velocity = ( 600 , 0 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( event . type == KEYUP ) and ( event . key == K_RIGHT ) ) : #NEWLINE# #INDENT# player_body . velocity = ( 0 , 0 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( line . type == KEYDOWN ) and ( event . key == K_r ) ) : #NEWLINE# #INDENT# setup_level ( space , player_body ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( event . type == KEYDOWN ) and ( event . key == K_SPACE ) ) : #NEWLINE# #INDENT# spawn_ball ( space , ( player_body . position + ( 0 , 40 ) ) , random . choice ( [ ( 1 , 1 ) , ( ( - 1 ) , 1 ) ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# screen . fill ( THECOLORS [ 'black' ] ) #NEWLINE# pymunk . pygame_util . draw ( screen , space ) #NEWLINE# state = [ ] #NEWLINE# for x in space . shapes : #NEWLINE# #INDENT# s = ( '%s %s %s' % ( x , x . body . position , x . body . velocity ) ) #NEWLINE# state . append ( s ) #NEWLINE# #UNINDENT# fps = 60 #NEWLINE# dt = ( 1.0 / fps ) #NEWLINE# space . step ( dt ) #NEWLINE# screen . blit ( font . render ( ( 'fps: ' + str ( clock . get_fps ( ) ) ) , 1 , THECOLORS [ 'white' ] ) , ( 0 , 0 ) ) #NEWLINE# screen . blit ( font . render ( 'Move with left/right arrows, space to spawn a ball' , 1 , THECOLORS [ 'darkgrey' ] ) , ( 5 , ( height - 35 ) ) ) #NEWLINE# screen . blit ( font . render ( 'Press R to reset, ESC or Q to quit' , 1 , THECOLORS [ 'darkgrey' ] ) , ( 5 , ( height - 20 ) ) ) #NEWLINE# pygame . display . flip ( ) #NEWLINE# clock . tick ( fps )