# Allison Obourn
# CS 115, Autumn 2021
# Lecture 16

# This program draws a purple bird that moves around the screen in random
# directions continuing in a straight line until it hits the edge. The
# edge contains a door represented by a black rectangle on the lefthand side
# If the bird hits the door it will fly off the screen and disappear. If it
# hits the wall anywhere else it will bounce off in a different random direction
# moving at a random speed. A messge will be printed to the console whenever the
# bird hits the door. 

from ECGUI import *
from random import *

BIRD_WIDTH = 60
BIRD_HEIGHT = 70
PANEL_WIDTH = 500
PANEL_HEIGHT = 500

DOOR_X = 0
DOOR_Y = 100
DOOR_HEIGHT = 200

x = 100
y = 100
x_change = 10
y_change = 10

def repeat():
    panel.clear()
    global x
    global y
    global x_change
    global y_change

    # draw the door
    panel.fill_rect(DOOR_X, DOOR_Y, 10, DOOR_HEIGHT);
    
    # draw the bird
    panel.fill_oval(x + 5, y, BIRD_WIDTH - 10, BIRD_HEIGHT, "#990099")
    panel.fill_oval(x + 40, y + 25, 20, 40, "#770077")
    panel.fill_oval(x, y + 25, 20, 40, "#770077")
    panel.fill_oval(x + 20, y + 10, 5, 5, "black")
    panel.fill_oval(x + 35, y + 10, 5, 5, "black")
    panel.fill_rect(x + 28, y + 18, 4, 8, "#eecc00")

    # alter the bird position variables so that it will appear in
    # a different spot next time it is drawn
    x = x + x_change
    y = y + y_change

    
    # check if we are going off the sides (left/right) of the screen
    if x + BIRD_WIDTH >= PANEL_WIDTH: # right
        x_change = -1 * randint(1, 10)
    elif x <= 0: # going off left side
        # check if we are in the door by making sure we are below the top
        # of the door and above the bottom of the door
        if DOOR_Y + DOOR_HEIGHT >= y >= DOOR_Y:
            print("in the door")
        else:
            # when hitting the left somewhere other than the door
            # turn around
            x_change = randint(1, 10)

    # check if we are going off the sides (top/bottom) of the screen
    # and turn around if we are
    if y + BIRD_HEIGHT >= PANEL_HEIGHT: # bottom
        y_change = -1 * randint(1, 10)
    elif y <= 0: # top
        y_change = randint(1, 10)

    panel.draw_string("x: " + str(x) + " ---- y: " + str(y), 0, 0)

    

panel = drawing_panel(PANEL_WIDTH, PANEL_HEIGHT)
panel.animate(50, repeat)
