# Arup Guha
# 7/15/2015
# Example to illustrate a mouse event.
# This is the fruit game - click on fruit to get points!!!

import random
import math
import time
import pygame, sys
from pygame.locals import *

# Useful Constants
SCREEN_W = 1000
SCREEN_H = 600

# This function handles moving each item listed in items.
def move(items):
    for item in items:
        item[0] += item[2]
        item[1] += item[3]

# This function removes all items that will never be visible again,
# and returns how many were removed.
def removeUseless(items):
    total = 0
    for item in items:
        if item[1] > SCREEN_H:
            items.remove(item)
            total += 1
    return total

# Returns true iff mypos is within the picture specified by f.
def hit(f, mypos, pics):

    if mypos[0] < f[0] or mypos[1] < f[1]:
        return False
        
    if mypos[0] >= f[0] + pics[f[4]].get_width():
        return False

    return mypos[1] < f[1] + pics[f[4]].get_height()

def main():

    # Basic Set Up
    pygame.init()
    DISPLAYSURF = pygame.display.set_mode((SCREEN_W, SCREEN_H))
    pygame.display.set_caption("Catch the fruit!")
    WHITE = pygame.Color(255,255,255)
    BLUE = pygame.Color(0,0,255)
    clock = pygame.time.Clock()

    # Visible fruit will be stored here.
    fruit = []

    # Store images here.
    pics = []
    pics.append(pygame.image.load("apple.jpg"))
    pics.append(pygame.image.load("strawberry.jpg"))
    pics.append(pygame.image.load("kiwi.jpg"))
    pics.append(pygame.image.load("cherry.jpg"))

    # How much each fruit is worth!
    pts = [50, 75, 100, 150]

    # Initialize stuff.
    curT = time.time()
    score = 0
    dropped = 0
    step = 0

    # Main game loop starts here.
    while True:
        
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            # Looking to see if you tried to get a fruit!
            if event.type == MOUSEBUTTONDOWN:

                # Just look at left mouse button.
                if pygame.mouse.get_pressed()[0]:

                    # Now see which fruit we hit! (I've implemented it so you
                    # could hit more than one in a single click!)
                    for f in fruit:
                        if hit(f, event.pos, pics):
                            score += pts[f[4]]
                            fruit.remove(f)

        # These images are big, so I only add a drop once every 10 time steps.
        if step%10 == 0:
            x = random.randint(1, SCREEN_W)
            which = random.randint(0,3)
            mydx = random.randint(-2, 2)
            mydy = random.randint(3, 8)
            fruit.append([x,0,mydx,mydy,which])
         
        DISPLAYSURF.fill(WHITE)

        # blit allows us to draw a surface onto another surface.
        for item in fruit:
            DISPLAYSURF.blit(pics[item[4]],(item[0], item[1]))

        pygame.display.update()

        # Move the drops for the next iteration and remove useless ones.
        move(fruit)
        dropped += removeUseless(fruit)

        # Game ends!
        if dropped > 20:
            print("Sorry, you have dropped more than 20 fruits.")
            print("The game is over.")
            print("Your score is",score)
            pygame.quit()
            sys.exit()
            
        clock.tick(30)
        step += 1

# Run it!
main()



