from turtle import Screen

from paddle import Paddle
from ball import Ball
import time
from scoreboard import ScoreBoard

screen = Screen()
screen.bgcolor('black')
screen.setup(width=800, height=600)
screen.title('pong')

# Turn off animation to show paddle after it has been shifted
screen.tracer(0)

right_paddle = Paddle(350, 0)
left_paddle = Paddle(-350, 0)
ball = Ball()
score = ScoreBoard()

screen.listen()
screen.onkey(right_paddle.go_up, 'Up')
screen.onkey(right_paddle.go_down, 'Down')
screen.onkey(left_paddle.go_up, 'w')
screen.onkey(left_paddle.go_down, 's')

game_is_on = True

while game_is_on:
    time.sleep(ball.ball_speed)
    screen.update()
    ball.move()

    # bounce when the ball hit the wall
    if ball.ycor() > 280 or ball.ycor() < -280:
        ball.bounce_y()

    # detect collision with the paddle
    if (ball.distance(right_paddle) < 50 and ball.xcor() > 320) or (
            ball.distance(left_paddle) < 50 and ball.xcor() < -320):
        ball.bounce_x()

    # detect R paddle miss
    if ball.xcor() > 380:
        ball.reset_pos()
        score.increase_l_point()

    # detect L paddle miss
    if ball.xcor() < -380:
        ball.reset_pos()
        score.increase_r_point()

screen.exitonclick()

[((8, 9, 8, 17), 'turtle.Screen', 'Screen', ({}, {}), '()', False, 'from turtle import Screen\n'), ((16, 15, 16, 29), 'paddle.Paddle', 'Paddle', ({(16, 22, 16, 25): '350', (16, 27, 16, 28): '0'}, {}), '(350, 0)', False, 'from paddle import Paddle\n'), ((17, 14, 17, 29), 'paddle.Paddle', 'Paddle', ({(17, 21, 17, 25): '-350', (17, 27, 17, 28): '0'}, {}), '(-350, 0)', False, 'from paddle import Paddle\n'), ((18, 7, 18, 13), 'ball.Ball', 'Ball', ({}, {}), '()', False, 'from ball import Ball\n'), ((19, 8, 19, 20), 'scoreboard.ScoreBoard', 'ScoreBoard', ({}, {}), '()', False, 'from scoreboard import ScoreBoard\n'), ((30, 4, 30, 31), 'time.sleep', 'time.sleep', ({(30, 15, 30, 30): 'ball.ball_speed'}, {}), '(ball.ball_speed)', False, 'import time\n')]