# Arup Guha
# 7/15/2015
# Split File Version of Fruit Game - Stores Fruit Functions and Constants

import random
import pygame, sys
from pygame.locals import *

# Useful Constants
SCREEN_W = 1000
SCREEN_H = 600
X = 0
Y = 1
DX = 2
DY = 3
F_ID = 4

# 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"))

# This function handles moving each item listed in items.
def move(items):
    for item in items:
        item[X] += item[DX]
        item[Y] += item[DY]

# 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[Y] > 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[X] < f[X] or mypos[Y] < f[Y]:
        return False
        
    if mypos[X] >= f[X] + pics[f[F_ID]].get_width():
        return False

    return mypos[Y] < f[Y] + pics[f[F_ID]].get_height()

# Creates a new fruit and returns it.
def makeNewFruit():
    x = random.randint(1, SCREEN_W)
    which = random.randint(0,3)
    mydx = random.randint(-2, 2)
    mydy = random.randint(3, 8)
    return [x,0,mydx,mydy,which]

# Draws text w/font, color on surface at position x, y.
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)

    # Draw one image onto another. Source and destination are passed. Source is textobj and it will be written onto textrect,
    surface.blit(text, textrect)


