#IMPORT LIBRARIES--------------------
import board
import digitalio
import time

#DEFINE PINS-------------------------


push1 = digitalio.DigitalInOut(board.D0)
push1.direction = digitalio.Direction.INPUT
push1.pull = digitalio.Pull.DOWN
push2 = digitalio.DigitalInOut(board.D1)
push2.direction = digitalio.Direction.INPUT
push2.pull = digitalio.Pull.DOWN

led1 = digitalio.DigitalInOut(board.D2)
led1.direction = digitalio.Direction.OUTPUT
led2 = digitalio.DigitalInOut(board.D3)
led2.direction = digitalio.Direction.OUTPUT
led3 = digitalio.DigitalInOut(board.D4)
led3.direction = digitalio.Direction.OUTPUT
led4 = digitalio.DigitalInOut(board.D5)
led4.direction = digitalio.Direction.OUTPUT
led5 = digitalio.DigitalInOut(board.D6)
led5.direction = digitalio.Direction.OUTPUT
led6 = digitalio.DigitalInOut(board.D7)
led6.direction = digitalio.Direction.OUTPUT
led7 = digitalio.DigitalInOut(board.D8)
led7.direction = digitalio.Direction.OUTPUT
led8 = digitalio.DigitalInOut(board.D9)
led8.direction = digitalio.Direction.OUTPUT
led9 = digitalio.DigitalInOut(board.D10)
led9.direction = digitalio.Direction.OUTPUT

#VARIABLES--------------------------------
flag1 = 1       #flag for the buttons
flag2 = 1


cont = 0        #main counter to turn on leds
cont_vel = 1    #counter to determine speed
state = 1       #counter increases(1) or decreases(0)
game = 0        #start game
cont_red = 0    #count when you reach the color red

while True:
  #START GAME----------------------------------------
    #press the two buttons to start, small blink and I enter the game loop
    if push1.value == True and push2.value == True :
        game = 1
        print("-----------START-------------")
        led1.value = True
        led2.value = True
        led3.value = True
        led4.value = True
        led5.value = True
        led6.value = True
        led7.value = True
        led8.value = True
        led9.value = True
        time.sleep(.05)
        led1.value = False
        led2.value = False
        led3.value = False
        led4.value = False
        led5.value = False
        led6.value = False
        led7.value = False
        led8.value = False
        led9.value = False
        time.sleep(.05)
        state=1
    #GAME LOOP---------------------------------
    while game == 1:

        #game speed
        for x in range(0,100-cont_vel):
            time.sleep(0.001)

        
        #first button with flag, counter increases 
        and speed increases
        if push1.value == True and flag1 == True:
            state = 1
            cont_vel = cont_vel + 1
            flag1 = 0
        if push1.value == False and flag1 == False:
            flag1 = 1

        #second button with flag, counter decreases  
        #and speed increases
        if push2.value == True and flag2 == True:
            state = 0
            cont_vel = cont_vel + 1
            flag2 = 0
        if push2.value == False and flag2 == False:
            flag2 = 1

        #counter increases or decreases
        if state == 1:
            cont = cont + 1
        if state == 0:
            cont = cont - 1

        #print counter
        print(cont_red)

        #negative limit of counter
        if cont <= -1:
            state = 1
            cont = 1

        #cases for the accountant
        if cont == 0:
            #blink and we give up the game
            for x in range(0,6):
                led1.value = True
                led2.value = True
                led3.value = True
                led4.value = True
                led5.value = True
                led6.value = True
                led7.value = True
                led8.value = True
                led9.value = True
                time.sleep(.05)
                led1.value = False
                led2.value = False
                led3.value = False
                led4.value = False
                led5.value = False
                led6.value = False
                led7.value = False
                led8.value = False
                led9.value = False
                time.sleep(.05)
            game = 2

        #LED1 turns on and network increases
        if cont == 1:
            led1.value = True
            led2.value = False
            led3.value = False
            led4.value = False
            led5.value = False
            led6.value = False
            led7.value = False
            led8.value = False
            led9.value = False
            cont_red = cont_red + 1

        #LED2 turns on and network increases
        if cont == 2:
            led1.value = False
            led2.value = True
            led3.value = False
            led4.value = False
            led5.value = False
            led6.value = False
            led7.value = False
            led8.value = False
            led9.value = False

        #LED3 turns on and network increases
        if cont == 3:
            led1.value = False
            led2.value = False
            led3.value = True
            led4.value = False
            led5.value = False
            led6.value = False
            led7.value = False
            led8.value = False
            led9.value = False

        #LED4 turns on and network increases
        if cont == 4:
            led1.value = False
            led2.value = False
            led3.value = False
            led4.value = True
            led5.value = False
            led6.value = False
            led7.value = False
            led8.value = False
            led9.value = False

        #LED5 turns on and network increases
        if cont == 5:
            led1.value = False
            led2.value = False
            led3.value = False
            led4.value = False
            led5.value = True
            led6.value = False
            led7.value = False
            led8.value = False
            led9.value = False

        #LED6 turns on and network increases
        if cont == 6:
            led1.value = False
            led2.value = False
            led3.value = False
            led4.value = False
            led5.value = False
            led6.value = True
            led7.value = False
            led8.value = False
            led9.value = False

        #LED7 turns on and network increases
        if cont == 7:
            led1.value = False
            led2.value = False
            led3.value = False
            led4.value = False
            led5.value = False
            led6.value = False
            led7.value = True
            led8.value = False
            led9.value = False

        #LED8 turns on and network increases
        if cont == 8:
            led1.value = False
            led2.value = False
            led3.value = False
            led4.value = False
            led5.value = False
            led6.value = False
            led7.value = False
            led8.value = True
            led9.value = False

        #LED9 turns on and network increases
        if cont == 9:
            led1.value = False
            led2.value = False
            led3.value = False
            led4.value = False
            led5.value = False
            led6.value = False
            led7.value = False
            led8.value = False
            led9.value = True
            cont_red = cont_red + 1

        if cont == 10:
            #blink and we give up the game
            for x in range(0,6):
                led1.value = True
                led2.value = True
                led3.value = True
                led4.value = True
                led5.value = True
                led6.value = True
                led7.value = True
                led8.value = True
                led9.value = True
                time.sleep(.05)
                led1.value = False
                led2.value = False
                led3.value = False
                led4.value = False
                led5.value = False
                led6.value = False
                led7.value = False
                led8.value = False
                led9.value = False
                time.sleep(.05)
            game = 2

    #SCORE AND RESET---------------------------------
    #In this oart we show the results and reset the counters,
    #we return to the beginning of the game
    if game == 2:
        cont_red=cont_red-2
        print("---------GAME OVER-----------")
        print("Speed: ")
        print(cont_vel)
        print("Red: ")
        print(cont_red)
        print(" ")
        print("SCORE")
        print(cont_red*cont_vel+cont_vel)
        
        game = 0
        cont = 0
        cont_red = 0
        cont_vel =0