# Arup Guha
# 6/1/2016
# Asteroids Framework - Fill in the designated functions so that this game works as intended.
#                       After you finish that, feel free to improve the game in any way you see fit.

import random
import math
import time
import pygame, sys
from pygame.locals import *

# This function handles moving each item listed in items.
def move(items):

    # For each item, move its x by dx and its y by dy
    for item in items:
        item[0] += item[2]
        item[1] += item[3]

# This function removes all items that will never be visible again.
def removeUseless(items):
    cnt = 0

    # Due to linear movement, once an object is off the screen, it will never come back again.
    for item in items:
        if item[1] > 600 or item[1] < 0 or item[0] > 1000 or item[0] < 0:
            items.remove(item)
            cnt += 1

    # Return the number of items removed
    return cnt

# Calculates which rocks are hit by at least one bullet, removes those rocks and returns the score
# for all of the rocks hit. (The radius of the rock is the # of pts awarded for each rock hit.)
def getCollisions(rocks, bullets):

    ''' FILL IN THIS FUNCTION

    Set the score for the frame to 0
    
    Go through each rock
    For each rock, see if any bullets intersect it
    If you find  one (or more), remove the rock AND add the radius of the rock to the score for the frame

    Return the score
    '''

# Returns the distance between item1 and item2 using the distance formula
def dist(item1, item2):
    return math.sqrt( (item1[0]-item2[0])**2 + (item1[1]-item2[1])**2 )

# All of our initial stuff.
pygame.init()
DISPLAYSURF = pygame.display.set_mode((1000, 600))
pygame.display.set_caption("Asteroids!")
DY = 5
RADIUS = 5
BLACK = pygame.Color(0,0,0)
BLUE = pygame.Color(0,0,255)
clock = pygame.time.Clock()

# Rocks and bullets need to be lists.
rocks = []
bullets = []

# These need to all be initialized here.
myAngle = math.pi/2
myScore = 0
lost = 0

# Defines the important information for my gun.
centerT = (500, 585)
triR = 8

while True:

    # Game ends when we let 100 asteroids go.
    if lost >= 100:
        break
    
    for event in pygame.event.get():

        # You quit...
        if event.type == QUIT:
            print("Your final score is",myScore)
            pygame.quit()
            sys.exit()

        # Process a key for the game.
        elif event.type == KEYDOWN:

            # Rotate our gun to the left, 10 degrees.
            if event.key == K_LEFT:
                ''' Change myAngle to move the gun to the left by 10 degrees '''

            # Rotate our gun to the right, 10 degrees.
            elif event.key == K_RIGHT:
                ''' Change myAngle to move the gun to the right by 10 degrees '''

            # Shoots a bullet by adding it to the bullet list. The direction is based on where the gun is pointing. Velocity is always 20.
            elif event.key == K_SPACE:
                bullets.append([centerT[0]+triR*math.cos(myAngle), centerT[1]+triR*math.sin(myAngle),20*math.cos(myAngle), -20*math.sin(myAngle), 5])

    # We have a 2% chance of generating a rock each iteration.
    randNum = random.randint(1, 50)

    # We add an asteroid!
    if randNum == 1:
        x = random.randint(1, 1000)
        mydy = random.randint(1, 10)
        mysize = random.randint(10, 50)
        rocks.append([x, 0, 0, mydy, mysize]);
     
    DISPLAYSURF.fill(BLACK)

    # Draw each rock individually.
    for item in rocks:
        pygame.draw.ellipse(DISPLAYSURF, pygame.Color(random.randint(0,255),random.randint(0,255),random.randint(0,255)), (item[0], item[1], item[4], item[4]), 0)

    # Draw each bullet individually.
    for item in bullets:
        pygame.draw.ellipse(DISPLAYSURF, pygame.Color(255,0,0), (item[0], item[1], item[4], item[4]), 0)

    # Drawing this triangle for the gun is annoying.
    ptAx = centerT[0]+triR*math.cos(myAngle)
    ptAy = centerT[1]-triR*math.sin(myAngle)
    ptBx = centerT[0]+triR*math.cos(myAngle+2*math.pi/3)
    ptBy = centerT[1]-triR*math.sin(myAngle+2*math.pi/3)
    ptCx = centerT[0]+triR*math.cos(myAngle-2*math.pi/3)
    ptCy = centerT[1]-triR*math.sin(myAngle-2*math.pi/3)
    triPt = [(ptAx,ptAy),(ptBx,ptBy),(ptCx,ptCy)]
    pygame.draw.polygon(DISPLAYSURF, pygame.Color(0,0,255), triPt)

    pygame.display.update()

    # Move the rocks and bullets for the next iteration and remove useless ones.
    move(rocks)
    lost += removeUseless(rocks)
    move(bullets)
    removeUseless(bullets)

    # Calculate collisions and adjust our score.
    myScore += getCollisions(rocks, bullets)
    
    clock.tick(30)

# You lose because you let 100 asteroids go.
print("Your final score is",myScore)
pygame.quit()
sys.exit()
