# Allison Obourn
# CS 115, Autumn 2021
# Lecture 19

# 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. 

# The car drives on a black road with green grass growing closer to the viewer.

# We changed this version to make the car speed up as it drove and to stop
# animating when the car left the screen.
    

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 = 500

# NOTE: this is a variable that changes so its name is lowercase



def main():
    x = 10
    panel = drawing_panel(PANEL_WIDTH, 120, "gray")
    speed = 0

    # continue to animate until the car is off the screen
    reached_edge = False
    while not reached_edge:
        panel.clear()
        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
        # set our variable so we know we should exit the loop
        if x >= PANEL_WIDTH:
            reached_edge = True
        else:
            x = x + 25 # otherwise keep driving
            

        # draws the road along the bottom
        panel.draw_line(0, 95, PANEL_WIDTH, 95, width="6")
        

        # draw 50 blades of grass along the bottom edge of the screen
        count = 0
        while count < PANEL_WIDTH // 5:
            # draw one blade of grass
            panel.draw_line(count * 5, 110, count * 5, 120, "green")
            count = count + 1

        #increase the speed as the car drives so that we appear to go faster
        panel.sleep(500 - speed)
        speed = speed + 30

main()
