# Tests with the Gamepad
# Myro
# Doug Blank
# CS110 Fall 2009

from myro import *

# Create the scene:

win = GraphWin("The Game", 500, 500)

# create a person:

def makePerson(x, y):
    list = []
    circle = Circle(Point(x, y), 10)
    circle.setFill("red")
    circle.draw(win)
    square = Rectangle(Point(circle.p1.x, circle.p2.y),
                       Point(circle.p2.x, circle.p2.y + 42))
    square.setFill("blue")
    square.draw(win)
    square2 = Rectangle(Point(square.p1.x - 20, square.p2.y),
                        Point(square.p2.x + 20, square.p2.y + 30))
    square2.setFill("green")
    square2.draw(win)
    polygon = Polygon(Point(x - 20, y),
                      Point(x, y - 20),
                      Point(x + 20, y))
    polygon.setFill("yellow")
    polygon.draw(win)
    # Polygons don't have p1 and p2
    # But we can add them!
    polygon.p1 = Point(min([p.x for p in polygon.points]),
                       min([p.y for p in polygon.points]))
    polygon.p2 = Point(max([p.x for p in polygon.points]),
                  		   max([p.y for p in polygon.points]))
    return [circle, square, square2, polygon]

person1 = makePerson(getWidth(win)/2, getHeight(win)/2)
person2 = makePerson(100, 100)

# Some support functions:

def minX(list):
    minValue = 100000
    for item in list:
        if item.p1.x < minValue:
            minValue = item.p1.x
    return minValue

def maxX(list):
    maxValue = -100000
    for item in list:
        if item.p2.x > maxValue:
            maxValue = item.p2.x
    return maxValue

def minY(list):
    minValue = 100000
    for item in list:
        if item.p1.y < minValue:
            minValue = item.p1.y
    return minValue

def maxY(list):
    maxValue = -100000
    for item in list:
        if item.p2.y > maxValue:
            maxValue = item.p2.y
    return maxValue

# Functions for person and playGame:

def move(list, x, y):
    for item in list:
        # Need to update p1 and p2:
        if isinstance(item, Polygon):
            item.p1.move(x, y)
            item.p2.move(x, y)
        else:
            item.move(x, y)

def playGame():
    while True:
        axis = getGamepadNow("axis")
        if (minX(person1) + axis[0] > 0 and
            maxX(person1) + axis[0] < getWidth(win) and
            minY(person1) + axis[1] > 0 and
            maxY(person1) + axis[1] < getHeight(win)):
            move(person1, axis[0], axis[1])
            wait(.01)
    
