# Arup Guha
# 7/13/2015 - tennis practice
# Edited in 4/30/2016 for Junior Knights
# Will be a game of catching object falling.

import pygame, sys
import random
import math
from pygame.locals import *

pygame.init()
DISPLAYSURF = pygame.display.set_mode((1000, 600))
pygame.display.set_caption("Catching Babies Game")

BLACK = pygame.Color(0,0,0)
RED = pygame.Color(255,0,0)
BLUE = pygame.Color(0,0,255)

clock = pygame.time.Clock()

baby = pygame.image.load("baby.jpg")
sizelist = baby.get_rect().size
imageW = sizelist[0]
imageL = sizelist[1]

x = 495
y = 0
r = 10
dx = 0
dy = 10
score = 0

paddleX = 495
paddleY = 595
paddleLength = 10
paddleWidth = 50
paddleDX = 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_RIGHT:
                paddleDX += 5
            if event.key == K_LEFT:
                paddleDX -= 5
                
    # Move the ball
    x += dx
    y += dy

    # Move the paddle
    if paddleX + paddleDX < 950 and paddleX + paddleDX >= 0:
        paddleX += paddleDX

    # Check to see if we hit the ball with the paddle
    if y > 590 - imageL  and x + imageW >= paddleX and x <= paddleX+paddleWidth:
        score += 1
        x = random.randint(10,990)
        y = 0

    # We missed the ball, so the game is over.
    if y + imageL > 610:
        print("Sorry, you lose, your score is",score)
        pygame.quit()
        sys.exit()
        break

    # Display everything for this iteration.
    DISPLAYSURF.fill(BLACK)
    DISPLAYSURF.blit(baby,(x, y))
    pygame.draw.rect(DISPLAYSURF, BLUE, (paddleX, paddleY, paddleWidth, paddleLength), 0)

    pygame.display.update()
    pygame.time.wait(60)

