# Arup Guha
# 3/18/2020
# Five bouncing balls.

import pygame, sys
from pygame.locals import *
import random

SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 600
DIAMETER = 30

# Colors I will use.
BLACK = pygame.Color(0,0,0) 

# Creates a random ball and returns the list storing all of its parameters.
# Here is what is stored everywhere: myball[0] = x, myball[1] = y,
# myball[2] = dx, myball[3] = dy, myball[4] = color
def makeRandomBall():
    myball = []
    myball.append(random.randint(0, SCREEN_WIDTH-DIAMETER))
    myball.append(random.randint(0, SCREEN_HEIGHT-DIAMETER))
    myball.append(random.randint(-5,5))
    myball.append(random.randint(-5,5))
    myball.append(pygame.Color(random.randint(0,255), random.randint(0,255), random.randint(0,255)))
    return myball

# Moves ball by 1 frame.
def moveBall(ball):

    # Move the ball for the next frame.
    ball[0] += ball[2]
    ball[1] += ball[3]

    # Bounce off the left or right.
    if ball[0] > SCREEN_WIDTH-DIAMETER or ball[0] < 0:
        ball[2] = -ball[2]

    # Bounce of the top or bottom
    if ball[1] > SCREEN_HEIGHT-DIAMETER or ball[1] < 0:
        ball[3] = -ball[3]

# Initial drawing stuff that I need for pygame.
pygame.init()
DISPLAYSURF = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Bouncing Balls!")


# Store all bouncing balls here.
mylist = []
for i in range(5):
    mylist.append(makeRandomBall())


# Main game loop.
while True:

    # Right now the only event we look for is quitting (x at top right)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    # Draw the black background and the ball.
    DISPLAYSURF.fill(BLACK)

    for i in range(len(mylist)):
        pygame.draw.ellipse(DISPLAYSURF, mylist[i][4], (mylist[i][0], mylist[i][1], DIAMETER, DIAMETER), 0)

    # Update the screen's display and wait.
    pygame.display.update()
    pygame.time.delay(30)

    # Update each ball's position.
    for myball in mylist:
        moveBall(myball)

