# Arup Guha
# 7/13/2015
# Shows an example of two moving objects, one controlled by the keyboard,
# in pygame.

import pygame, sys
import random
import math
from pygame.locals import *

pygame.init()
DISPLAYSURF = pygame.display.set_mode((1000, 600))
pygame.display.set_caption("Really Crappy Tennis Practice!")

BLACK = pygame.Color(0,0,0)
RED = pygame.Color(255,0,0)
BLUE = pygame.Color(0,0,255)

clock = pygame.time.Clock()

x = 990
y = 295
r = 10
dx = -10
dy = 0
score = 0

paddleX = 5
paddleY = 275
paddleLength = 50
paddleWidth = 10
paddleDY = 0

while True:

    # Event loop for each iteration
    for event in pygame.event.get():

        # To handle exiting the game.     
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
            
        # Here are the key presses we respond to.
        if event.type == KEYDOWN:
            if event.key == K_DOWN:
                paddleDY += 5
            if event.key == K_UP:
                paddleDY -= 5
                
    # Move the ball
    x += dx
    y += dy

    # Move the paddle
    if paddleY + paddleDY < 550 and paddleY + paddleDY >= 0:
        paddleY += paddleDY

    # Check to see if we hit the ball with the paddle
    if x < 15 and y + r >= paddleY and y <= paddleY+paddleLength:
        score += 1
        x = 990
        y = random.randint(10,590)

    # We missed the ball, so the game is over.
    if x < 0:
        print("Sorry, you lose, your score is",score)
        pygame.quit()
        sys.exit()
        break

    # Display everything for this iteration.
    DISPLAYSURF.fill(BLACK)
    pygame.draw.ellipse(DISPLAYSURF, RED, (x, y, 2*r, 2*r), 0)
    pygame.draw.rect(DISPLAYSURF, BLUE, (paddleX, paddleY, paddleWidth, paddleLength), 0)

    pygame.display.update()
    pygame.time.wait(max(0,60-10*score))


