# Arup Guha
# 6/22/2016
# Example using a list of lists - draws boxes on a pygame screen

import pygame, sys
import random
from pygame.locals import *

# Constants for indexes into list
BOX_IDX = 0
CLR_IDX = 1
DX_IDX = 2
DY_IDX = 3

# Set up our screen
pygame.init()
DISPLAYSURF = pygame.display.set_mode((1000, 600)) 
pygame.display.set_caption("Drawing boxes!") 

black = pygame.Color(0,0,0)

# Moves the box mybox on the screen with dimensions screenW, screenH
def moveBox(mybox, screenW, screenH):

    # Moves one frame.
    mybox[BOX_IDX][0] += mybox[DX_IDX]
    mybox[BOX_IDX][1] += mybox[DY_IDX]

    # Go from right to left.
    if mybox[BOX_IDX][0] > screenW:
        mybox[BOX_IDX][0] = 0

    # Go from left to right.
    if mybox[BOX_IDX][0] < -mybox[BOX_IDX][2]:
        mybox[BOX_IDX][0] = screenW

    # Go from bottom to top.
    if mybox[BOX_IDX][1] > screenH:
        mybox[BOX_IDX][1] = 0

    # Go from top to bottom.,
    if mybox[BOX_IDX][1] < -mybox[BOX_IDX][3]:
        mybox[BOX_IDX][1] = screenH

# Main function
def main():

    # Set up our boxes list.
    boxes = []
    n = int(input("How many boxes will you enter?\n"))

    # Read in each box
    for loop in range(n):

        # Stuff we read in.
        width = int(input("What is the width of this box?\n"))
        height = int(input("What is the height of this box?\n"))
        topleftx = int(input("What is the x coordinate of the top left?\n"))
        toplefty = int(input("What is the y coordinate of the top left?\n"))

        # Just generate a random color.
        myr = random.randint(0, 255)
        myg = random.randint(0, 255)
        myb = random.randint(0, 255)
        myColor = pygame.Color(myr, myg, myb)

        # Set random movement either to the right or down.
        dx = 0
        dy = 0
        if random.randint(0,1) == 0:
            dx = random.randint(1,5)
        else:
            dy = random.randint(1,5)

        # Now we can create and add this box.
        thisBox = [[topleftx, toplefty, width, height], myColor, dx, dy]
        boxes.append(thisBox)

    # Game Loop
    while True: 

        # Just lets us x out the window.
        for event in pygame.event.get():

            if event.type == QUIT:
                pygame.quit()
                sys.exit()

        # This moves each box.
        for mybox in boxes:
            moveBox(mybox, 1000, 600)

        DISPLAYSURF.fill(black)

        # Draw each box and display.
        for i in range(len(boxes)):
            pygame.draw.rect(DISPLAYSURF, boxes[i][CLR_IDX], boxes[i][BOX_IDX], 0)
        pygame.display.update()

main()
