<s> import kivy <EOL> kivy . require ( '<STR_LIT>' ) <EOL> from kivy . app import App <EOL> from kivy . uix . widget import Widget <EOL> from kivy . properties import NumericProperty , ReferenceListProperty , ObjectProperty <EOL> from kivy . vector import Vector <EOL> from kivy . clock import Clock <EOL> class PongPaddle ( Widget ) : <EOL> score = NumericProperty ( <NUM_LIT:0> ) <EOL> def bounce_ball ( self , ball ) : <EOL> if self . collide_widget ( ball ) : <EOL> vx , vy = ball . velocity <EOL> offset = ( ball . center_y - self . center_y ) / ( self . height / <NUM_LIT:2> ) <EOL> bounced = Vector ( - <NUM_LIT:1> * vx , vy ) <EOL> vel = bounced * <NUM_LIT> <EOL> ball . velocity = vel . x , vel . y + offset <EOL> class PongBall ( Widget ) : <EOL> velocity_x = NumericProperty ( <NUM_LIT:0> ) <EOL> velocity_y = NumericProperty ( <NUM_LIT:0> ) <EOL> velocity = ReferenceListProperty ( velocity_x , velocity_y ) <EOL> def move ( self ) : <EOL> self . pos = Vector ( * self . velocity ) + self . pos <EOL> class PongGame ( Widget ) : <EOL> ball = ObjectProperty ( None ) <EOL> player1 = ObjectProperty ( None ) <EOL> player2 = ObjectProperty ( None ) <EOL> def serve_ball ( self , vel = ( <NUM_LIT:4> , <NUM_LIT:0> ) ) : <EOL> self . ball . center = self . center <EOL> self . ball . velocity = vel <EOL> def update ( self , dt ) : <EOL> self . ball . move ( ) <EOL> self . player1 . bounce_ball ( self . ball ) <EOL> self . player2 . bounce_ball ( self . ball ) <EOL> if ( self . ball . y < self . y ) or ( self . ball . top > self . top ) : <EOL> self . ball . velocity_y *= - <NUM_LIT:1> <EOL> if self . ball . x < self . x : <EOL> self . player2 . score += <NUM_LIT:1> <EOL> self . serve_ball ( vel = ( <NUM_LIT:4> , <NUM_LIT:0> ) ) <EOL> if self . ball . x > self . width : <EOL> self . player1 . score += <NUM_LIT:1> <EOL> self . serve_ball ( vel = ( - <NUM_LIT:4> , <NUM_LIT:0> ) ) <EOL> def on_touch_move ( self , touch ) : <EOL> if touch . x < self . width / <NUM_LIT:3> : <EOL> self . player1 . center_y = touch . y <EOL> if touch . x > self . width - self . width / <NUM_LIT:3> : <EOL> self . player2 . center_y = touch . y <EOL> class PongApp ( App ) : <EOL> def build ( self ) : <EOL> game = PongGame ( ) <EOL> game . serve_ball ( ) <EOL> Clock . schedule_interval ( game . update , <NUM_LIT:1.0> / <NUM_LIT> ) <EOL> return game <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> PongApp ( ) . run ( ) </s>
