king_pong.py
def move_ball(self):
                """
                Move the ball in game state
                it calculates boundaries and it clips
                the ball positioning when it is overlapping
                with walls or paddles

                return rewards when right player makes contact with the ball
                and when ball leaves the game screen on the left side
                """
                reward = 0.0

                # get ball trajectory
                prev_x, prev_y = self.ballx, self.bally
                next_x, next_y = self.ballx + self.ball_speed_x, self.bally + self.ball_speed_y
                ball_trajectory = LineString([(prev_x, prev_y), (next_x, next_y)])

                # get possible collision lines
                upper_wall = LineString([(0, 0),
                                         (SCREEN_WIDTH, 0)])
                bottom_wall = LineString([(0, SCREEN_HEIGHT - BALL_SIZE),
                                          (SCREEN_WIDTH, SCREEN_HEIGHT - BALL_SIZE)])
                left_paddle = LineString([(self.cpux + PADDLE_WIDTH, self.cpuy - BALL_SIZE),
                                          (self.cpux + PADDLE_WIDTH, self.cpuy + PADDLE_HEIGHT)])
                right_paddle = LineString([(self.playerx - BALL_SIZE, self.playery - BALL_SIZE),
                                           (self.playerx - BALL_SIZE, self.playery + PADDLE_HEIGHT)])

                # chop ball trajectory when colliding
                if ball_trajectory.intersects(upper_wall):
                        self.ball_speed_y *= -1
                        upper = ball_trajectory.intersection(upper_wall)
                        self.ballx, self.bally = upper.x, upper.y + 1
                elif ball_trajectory.intersects(bottom_wall):
                        self.ball_speed_y *= -1
                        bottom = ball_trajectory.intersection(bottom_wall)
                        self.ballx, self.bally = bottom.x, bottom.y - 1
                elif ball_trajectory.intersects(left_paddle):
                        left = ball_trajectory.intersection(left_paddle)
                        contact_point = left.y - left_paddle.xy[1][0]
                        if contact_point < PADDLE_UPPER_SECTION or \
                           contact_point > PADDLE_BOTTOM_SECTION:
                                self.flip_and_spin_ball()
                        else:
                                self.flip_and_speed_ball()
                        self.ballx, self.bally = left.x + 1, left.y
                elif ball_trajectory.intersects(right_paddle):
                        reward += 0.1
                        right = ball_trajectory.intersection(right_paddle)
                        contact_point =  right.y - right_paddle.xy[1][0]
                        if contact_point < PADDLE_UPPER_SECTION or \
                           contact_point > PADDLE_BOTTOM_SECTION:
                                self.flip_and_spin_ball()
                        else:
                                self.flip_and_speed_ball()
                        self.ballx, self.bally = right.x - 1, right.y
                else:
                        self.ballx += self.ball_speed_x
                        self.bally += self.ball_speed_y

                return reward

                Move the ball in game state
                it calculates boundaries and it clips
                the ball positioning when it is overlapping
                with walls or paddles

                return rewards when right player makes contact with the ball
                and when ball leaves the game screen on the left side
                
Move the ball in game state
it calculates boundaries and it clips
the ball positioning when it is overlapping
with walls or paddles

return rewards when right player makes contact with the ball
and when ball leaves the game screen on the left side
Move the ball in game state it calculates boundaries and it clips the ball positioning when it is overlapping with walls or paddles return rewards when right player makes contact with the ball and when ball leaves the game screen on the left side
def move_ball(self):
                reward = 0.0
                prev_x, prev_y = self.ballx, self.bally
                next_x, next_y = self.ballx + self.ball_speed_x, self.bally + self.ball_speed_y
                ball_trajectory = LineString([(prev_x, prev_y), (next_x, next_y)])
                upper_wall = LineString([(0, 0),
                                         (SCREEN_WIDTH, 0)])
                bottom_wall = LineString([(0, SCREEN_HEIGHT - BALL_SIZE),
                                          (SCREEN_WIDTH, SCREEN_HEIGHT - BALL_SIZE)])
                left_paddle = LineString([(self.cpux + PADDLE_WIDTH, self.cpuy - BALL_SIZE),
                                          (self.cpux + PADDLE_WIDTH, self.cpuy + PADDLE_HEIGHT)])
                right_paddle = LineString([(self.playerx - BALL_SIZE, self.playery - BALL_SIZE),
                                           (self.playerx - BALL_SIZE, self.playery + PADDLE_HEIGHT)])
                if ball_trajectory.intersects(upper_wall):
                        self.ball_speed_y *= -1
                        upper = ball_trajectory.intersection(upper_wall)
                        self.ballx, self.bally = upper.x, upper.y + 1
                elif ball_trajectory.intersects(bottom_wall):
                        self.ball_speed_y *= -1
                        bottom = ball_trajectory.intersection(bottom_wall)
                        self.ballx, self.bally = bottom.x, bottom.y - 1
                elif ball_trajectory.intersects(left_paddle):
                        left = ball_trajectory.intersection(left_paddle)
                        contact_point = left.y - left_paddle.xy[1][0]
                        if contact_point < PADDLE_UPPER_SECTION or \
                           contact_point > PADDLE_BOTTOM_SECTION:
                                self.flip_and_spin_ball()
                        else:
                                self.flip_and_speed_ball()
                        self.ballx, self.bally = left.x + 1, left.y
                elif ball_trajectory.intersects(right_paddle):
                        reward += 0.1
                        right = ball_trajectory.intersection(right_paddle)
                        contact_point =  right.y - right_paddle.xy[1][0]
                        if contact_point < PADDLE_UPPER_SECTION or \
                           contact_point > PADDLE_BOTTOM_SECTION:
                                self.flip_and_spin_ball()
                        else:
                                self.flip_and_speed_ball()
                        self.ballx, self.bally = right.x - 1, right.y
                else:
                        self.ballx += self.ball_speed_x
                        self.bally += self.ball_speed_y
                return reward
def move_ball ( self ) : reward = 0.0 prev_x , prev_y = self . ballx , self . bally next_x , next_y = self . ballx + self . ball_speed_x , self . bally + self . ball_speed_y ball_trajectory = LineString ( [ ( prev_x , prev_y ) , ( next_x , next_y ) ] ) upper_wall = LineString ( [ ( 0 , 0 ) , ( SCREEN_WIDTH , 0 ) ] ) bottom_wall = LineString ( [ ( 0 , SCREEN_HEIGHT - BALL_SIZE ) , ( SCREEN_WIDTH , SCREEN_HEIGHT - BALL_SIZE ) ] ) left_paddle = LineString ( [ ( self . cpux + PADDLE_WIDTH , self . cpuy - BALL_SIZE ) , ( self . cpux + PADDLE_WIDTH , self . cpuy + PADDLE_HEIGHT ) ] ) right_paddle = LineString ( [ ( self . playerx - BALL_SIZE , self . playery - BALL_SIZE ) , ( self . playerx - BALL_SIZE , self . playery + PADDLE_HEIGHT ) ] ) if ball_trajectory . intersects ( upper_wall ) : self . ball_speed_y *= - 1 upper = ball_trajectory . intersection ( upper_wall ) self . ballx , self . bally = upper . x , upper . y + 1 elif ball_trajectory . intersects ( bottom_wall ) : self . ball_speed_y *= - 1 bottom = ball_trajectory . intersection ( bottom_wall ) self . ballx , self . bally = bottom . x , bottom . y - 1 elif ball_trajectory . intersects ( left_paddle ) : left = ball_trajectory . intersection ( left_paddle ) contact_point = left . y - left_paddle . xy [ 1 ] [ 0 ] if contact_point < PADDLE_UPPER_SECTION or contact_point > PADDLE_BOTTOM_SECTION : self . flip_and_spin_ball ( ) else : self . flip_and_speed_ball ( ) self . ballx , self . bally = left . x + 1 , left . y elif ball_trajectory . intersects ( right_paddle ) : reward += 0.1 right = ball_trajectory . intersection ( right_paddle ) contact_point = right . y - right_paddle . xy [ 1 ] [ 0 ] if contact_point < PADDLE_UPPER_SECTION or contact_point > PADDLE_BOTTOM_SECTION : self . flip_and_spin_ball ( ) else : self . flip_and_speed_ball ( ) self . ballx , self . bally = right . x - 1 , right . y else : self . ballx += self . ball_speed_x self . bally += self . ball_speed_y return reward
Move the ball in game state
it calculates boundaries and it clips
the ball positioning when it is overlapping
with walls or paddles
Move the ball in game state it calculates boundaries and it clips the ball positioning when it is overlapping with walls or paddles
"""
                Move the ball in game state
                it calculates boundaries and it clips
                the ball positioning when it is overlapping
                with walls or paddles

                return rewards when right player makes contact with the ball
                and when ball leaves the game screen on the left side
                """ # get ball trajectory # get possible collision lines # chop ball trajectory when colliding
self