label livello_agilita1:
    scene sfondo8_2

    call livello_minigioco_agilita1

label livello_minigioco_agilita1:

    $ cont = 0 #continue variable
    $ arr_keys = [""] #list of keyboard inputs to be selected from. See https://www.pygame.org/docs/ref/key.html for more keys

    $timeout = 5

    call qte_setupMinigiocoAgilita1(0.5, 0.5, 0.01, renpy.random.choice(arr_keys), renpy.random.randint(1, 9) * 0.1, renpy.random.randint(1, 9) * 0.1)

    # "Function Call" - see label qte_setup for detail on "function"
    # in the above, I randomly select a key from a previously defined set of keys (arr_keys), and randomise the location

    while cont == 1:
        $iterazioni += 1
        if(iterazioni == 10): #chiamo minigioco fino all'iterazione 9, quindi 10 (preincremento)
            #$minigiocoAgilitaCompletato = True
            $cont = 0
        else:
            if(iterazioni > 5):
                call qte_setupMinigiocoAgilita1(0.4, 0.4, 0.01, renpy.random.choice(arr_keys), renpy.random.randint(1, 9) * 0.1, renpy.random.randint(1, 9) * 0.1)
            else:
                call qte_setupMinigiocoAgilita1(0.5, 0.5, 0.01, renpy.random.choice(arr_keys), renpy.random.randint(1, 9) * 0.1, renpy.random.randint(1, 9) * 0.1)

    if(iterazioni < 9):
        #fine minigioco
        $iterazioni = 0;

        show mc_Img sinistra pensieroso at left
        mc"{b}I have to try again..{/b}"
        menu:
            "Try again":
                call livello_agilita1
            "Next time":
                call livello_alberoElfa
    else:
        #fine minigioco
        $iterazioni = 0;
        $livelloAgilita += 1
        scene agilitaUp with fade
        pause
        $livelloEnergia -= 4
        call livello_alberoElfa

image sfondoMinigiocoAgilita ="Luoghi/SfondoMinigiocoAgilita1.png"

label qte_setupMinigiocoAgilita1(time_start, time_max, interval, trigger_key, x_align, y_align):

    $ time_start = time_start
    $ time_max = time_max
    $ interval = interval
    $ trigger_key = trigger_key
    $ x_align = x_align
    $ y_align = y_align

    call screen qte_buttonAgilita1

    $ cont = _return
    # 1 if key was hit in time, 0 if key not

    return

screen qte_buttonAgilita1:

    button:
        action Return(0) #miss
        align 0.5, 0.5
        if(iterazioni > 0):
            if(iterazioni <= 3):
                background "MingiocoAgilita/EntrataElfa/Scena1.png"
            elif(iterazioni <= 6):
                background "MingiocoAgilita/EntrataElfa/Scena2.png"
            elif(iterazioni <= 9):
                background "MingiocoAgilita/EntrataElfa/Scena3.png"
        #to add a click sensor *outside* of button (if player presses outside button area) and return false


    timer interval repeat True action If(time_start > 0.0, true=SetVariable('time_start', time_start - interval), false=[Return(0), Hide('qte_button')])
    #see above

    vbox:
        xalign x_align yalign y_align spacing 25

        button:
            action Return(1)
            xalign 0.5
            xysize 100, 100 #dimensione clip
            background Animation("images/Luoghi/Clip/IconaMinigiocoAgilita.png", 0.5) #change to image, etc
            #same function as the key input

        bar:
            value time_start
            range time_max
            xalign 0.5
            xmaximum 300
            if time_start <= (time_max * 0.25):
                left_bar "#f00"
