# 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 fruitfunc
import pygame, sys
from pygame.locals import *

# Useful Constants
SCREEN_W = 1000
SCREEN_H = 600

# Indexes into lists.
X = 0
Y = 1
DX = 2
DY = 3
F_ID = 4

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 = []

    # How much each fruit is worth!
    pts = [50, 75, 100, 150]

    # Initialize variables.
    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 fruitfunc.hit(f, event.pos, fruitfunc.pics):
                            score += pts[f[F_ID]]
                            fruit.remove(f)

        # These images are big, so I only add a drop once every 10 time steps.
        if step%10 == 0:
            fruit.append(fruitfunc.makeNewFruit())
         
        DISPLAYSURF.fill(WHITE)

        # blit allows us to draw a surface onto another surface.
        for item in fruit:
            DISPLAYSURF.blit(fruitfunc.pics[item[F_ID]],(item[X], item[Y]))

        # Add game to score.
        fruitfunc.draw("Score: "+str(score), pygame.font.SysFont("ComicSansMS", 80), pygame.Color("black"), DISPLAYSURF, SCREEN_W//2, 50)

        pygame.display.update()

        # Move the drops for the next iteration and remove useless ones.
        fruitfunc.move(fruit)
        dropped += fruitfunc.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()


