# Allison Obourn
# CS 115, Autumn 2021
# Lecture 12

# This program opens a window that is wider than it is tall. This
# window has a gray background and contains a black car with red wheels
# and a cyan windsheild facing right. It drives along the panel until it
# is completely out of the window. Then it reappears on the left side. 

from ECGUI import *

# NOTE: you can tell this is a constant because it is at the top and the
#       name is in ALL_CAPS
PANEL_WIDTH = 260

# NOTE: this is a variable that changes so its name is lowercase
x = 10


panel = drawing_panel(PANEL_WIDTH, 120, "gray")


def repeat():
    panel.clear()
    global x   # so we can alter the x variable declare outside our function
    y = 30
    panel.fill_rect(x, y, 100, 50, "black")
    panel.fill_oval(x + 10, y + 40, 20, 20, "red")
    panel.fill_oval(x + 70, y + 40, 20, 20, "red")
    panel.fill_rect(x + 70, y + 10, 30, 20, "cyan")

    # check if we have driven completely off the screen and if we have
    # start back on the left
    # NOTE: we set x to -100 because we want the right end of the car to
    #       appear at the very left edge of the screen so it appears to
    #       drive into view. Since the x is the position of the left side
    #       we need to subtract the width of the car. 
    if x >= PANEL_WIDTH:
        x = -100
    else:
        x = x + 25 # if we add a bigger number we will move faster
        

    # draws the road along the bottom
    #panel.set_stroke(6)
    panel.draw_line(0, 95, PANEL_WIDTH, 95, width="6")
    
    panel.set_stroke(1)
    panel.draw_line(0, 110, PANEL_WIDTH, 110, "green")

panel.animate(50, repeat)
