# 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 *
from PicItemFile import picitem

# 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.move()

# 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.rec.y > SCREEN_H:
            items.remove(item)
            total += 1
    return total

# Function to put text onto the screen.
def draw(text, font, color, surface, x, y):
    # Draw text on a new Surface. Title, antialias and color are used.
    text = font.render(text, 1, color)

    # Returns a new rectangle covering the entire surface.
    # This rectangle will always start at (0, 0) with a width and height the same size as the image.
    textrect = text.get_rect()

    # Sets location of text.
    textrect.topleft = (x, y)

    # Go ahead and draw it to the surface.
    surface.blit(text, textrect)

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()

    # Create a Font object from the system fonts. I chose size 100.
    font = pygame.font.SysFont("Arial", 36)

    # 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 f.hit(event.pos):
                            score += f.pts
                            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)
            temp = picitem(x,0,mydx,mydy,pics[which],pts[which])
            fruit.append(temp)
         
        DISPLAYSURF.fill(WHITE)

        # blit allows us to draw a surface onto another surface.
        for item in fruit:
            item.draw(DISPLAYSURF)

        # Display Score
        draw("Score: "+str(score), font, "black", DISPLAYSURF, 0, 0)

        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()



